Number of Matching Subsequences LeetCode solution in Java
1class Solution {2public int numMatchingSubseq(String s, String[] words) {3int count = 0;4Map<String, Integer> map = new HashMap<>();5for (String word : words) {6if (!map.containsKey(word)) {7map.put(word, 1);8} else {9map.put(word, map.get(word) + 1);10}11}12for (String word : map.keySet()) {13if (isSeq(word, s)) {14count += map.get(word);15}16}17return count;18}1920public boolean isSeq(String s1, String s2) {21int s1ind = 0;22int s2ind = 0;23int counter = 0;24if (s1.length() > s2.length()) {25return false;26}27while (s1ind < s1.length() && s2ind < s2.length()) {28if (s1.charAt(s1ind) == s2.charAt(s2ind)) {29counter++;30s1ind++;31s2ind++;32} else {33s2ind++;34}35}36return counter == s1.length();37}38}