Advertisement
md5kafka

Untitled

Jan 6th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. class Solution {
  2.     public int numMatchingSubseq(String s, String[] words) {
  3.         // Suppose s.length is of size n, words.length is m, and max(words[i].length) is k This alg is O(mk) + O(nm)
  4.         List<Queue<Character>> wordQueues = new ArrayList<>();
  5.         int ans = 0;
  6.         for(String word: words) { // O(m*k)
  7.             Queue<Character> queue = new LinkedList<>();
  8.             for(char c : word.toCharArray()) {
  9.                 queue.add(c);
  10.             }
  11.             wordQueues.add(queue);
  12.         }
  13.         for(char c: s.toCharArray()) { //O(n*m)
  14.             for(Queue<Character> queue : wordQueues) {
  15.                 if(!queue.isEmpty() && queue.peek() == c) {
  16.                     queue.poll();
  17.                     if(queue.isEmpty()) {
  18.                         ans++;
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.         return ans;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement