Skip to content

Commit 33ea540

Browse files
authored
Create Number of Matching Subsequences.java
1 parent 9cf905c commit 33ea540

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int numMatchingSubseq(String s, String[] words) {
3+
int count = 0;
4+
for (String word : words) {
5+
if (isSubsequence(s, word)) {
6+
count++;
7+
}
8+
}
9+
return count;
10+
}
11+
12+
private boolean isSubsequence(String s, String word) {
13+
int prevIdx = 0;
14+
for (char c : word.toCharArray()) {
15+
int idx = s.indexOf(c, prevIdx);
16+
if (idx == -1) {
17+
return false;
18+
}
19+
prevIdx = idx + 1;
20+
}
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)