Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int numMatchingSubseq(String s, String[] words) {
- // 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)
- List<Queue<Character>> wordQueues = new ArrayList<>();
- int ans = 0;
- for(String word: words) { // O(m*k)
- Queue<Character> queue = new LinkedList<>();
- for(char c : word.toCharArray()) {
- queue.add(c);
- }
- wordQueues.add(queue);
- }
- for(char c: s.toCharArray()) { //O(n*m)
- for(Queue<Character> queue : wordQueues) {
- if(!queue.isEmpty() && queue.peek() == c) {
- queue.poll();
- if(queue.isEmpty()) {
- ans++;
- }
- }
- }
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement