Number of Matching Subsequences LeetCode solution in Java

1
class Solution {
2
public int numMatchingSubseq(String s, String[] words) {
3
int count = 0;
4
Map<String, Integer> map = new HashMap<>();
5
for (String word : words) {
6
if (!map.containsKey(word)) {
7
map.put(word, 1);
8
} else {
9
map.put(word, map.get(word) + 1);
10
}
11
}
12
for (String word : map.keySet()) {
13
if (isSeq(word, s)) {
14
count += map.get(word);
15
}
16
}
17
return count;
18
}
19

20
public boolean isSeq(String s1, String s2) {
21
int s1ind = 0;
22
int s2ind = 0;
23
int counter = 0;
24
if (s1.length() > s2.length()) {
25
return false;
26
}
27
while (s1ind < s1.length() && s2ind < s2.length()) {
28
if (s1.charAt(s1ind) == s2.charAt(s2ind)) {
29
counter++;
30
s1ind++;
31
s2ind++;
32
} else {
33
s2ind++;
34
}
35
}
36
return counter == s1.length();
37
}
38
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0