[Java] | Simple Intuitive Approach with Comments | Hashmap
\tclass Solution {\n    \n    // Time Complexity: O(n + sum(len(word) for word in words array))   (where n -> no. of characters in String s)\n    // Space Compl
\tclass Solution {\n    \n    // Time Complexity: O(n + sum(len(word) for word in words array))   (where n -> no. of characters in String s)\n    // Space Complexity: O(n)\n    \n    public int numMatchingSubseq(String s, String[] words) {\n        // Edge Case Checking\n        if(s == null || s.length() == 0 || words == null || words.length == 0)\n            return 0;\n        \n        // Creating a map for every character in string "s" --> which contain every character and its indexes\n        Map<Character, List<Integer>> map = new HashMap<>();\n        for(int i = 0; i < s.length(); i++){\n            map.putIfAbsent(s.charAt(i), new ArrayList());\n            map.get(s.charAt(i)).add(i);\n        }\n        \n        \n        /*\n        Idea: for a subsequence the character should be present in the same order from left to right as they are present in String "s"\n            For every word in words:\n                - if the character is present in String s:\n                    - we check the index for each character is greater than the index of its previous character\n                        - if yes --> keep moving ahead and keep updating the current index\n                    - else return false\n                - else return false\n        */\n        \n        int result = 0;\n        for(String word : words){\n            boolean res = isSubseq(map, word);\n            if(res)\n                result++;\n        }\n        \n        return result;\n    }\n    \n    private boolean isSubseq(Map<Character, List<Integer>> map, String word){\n        int curr_index = -1;\n        int n = word.length();\n        int i = 0;\n        \n        while(i < n){\n            char c = word.charAt(i);\n            boolean changed = false;\n            if(map.containsKey(c)){\n                List<Integer> list = map.get(c);\n                for(int index : list){\n                    if(index > curr_index){\n                        curr_index = index;\n                        changed = true;\n                        break;\n                    }\n                }\n                if(!changed)\n                    return false;\n            }else\n                return false;\n            i++;\n        }\n        return true;\n    }\n}
['Java']