Skip to content

Latest commit

 

History

History
44 lines (39 loc) · 1.38 KB

NumberofMatchingSubsequences.md

File metadata and controls

44 lines (39 loc) · 1.38 KB

Problem

class Solution {
    public int numMatchingSubseq(String s, String[] words) {
        
        Map<Character, List<String>> bucket = new HashMap<>();
        
        for(Character ch : s.toCharArray()){
            bucket.put(ch, new ArrayList<String>());
        }
        
        for(String w : words){
            if(bucket.containsKey(w.charAt(0))){
                bucket.get(w.charAt(0)).add(w);
            }
        }
        
        int result = 0;
        for(Character ch : s.toCharArray())
        {
            if(bucket.containsKey(ch))
            {
                List<String> tmp = bucket.get(ch);
                bucket.put(ch, new ArrayList<String>());
                int index=0;
                for(String str : tmp)
                {
                    if(str.length()==1) {
                        result++;
                    }
                    else {
                        if(bucket.containsKey(str.charAt(1)))
                            bucket.get(str.charAt(1)).add(str.substring(1));
                    }
                }
            }  
        }
        return result;
    }
}
//Time Complexity -> s.length() + each word.length()

Editorial