Number of Matching Subsequences
PreviousMinimum Absolute Difference QueriesNext889. Construct Binary Tree from Preorder and Postorder Traversal
Last updated
Last updated
class Solution {
public int numMatchingSubseq(String s, String[] words) {
List<Integer>[] mapping = new ArrayList[255];
for (int i = 'a'; i <= 'z'; ++i) {
mapping[i] = new ArrayList<>();
}
for (int i = 0; i < s.length(); ++i) {
mapping[s.charAt(i)].add(i);
}
int ret = 0;
for (String word : words) {
if (check(mapping, word)) {
ret++;
}
}
return ret;
}
// 检查能否对于word中每个字符找到mapping中对应的递增index的序列
private boolean check(List<Integer>[] mapping, String word) {
int last = -1;
for (char c : word.toCharArray()) {
List<Integer> indices = mapping[c];
if (indices.isEmpty()) {
return false;
}
// 二分找大于last的最小index
int left = 0, right = indices.size() - 1;
while (left + 1 < right) {
int mid = (left + right) / 2;
if (indices.get(mid) <= last) {
left = mid;
} else {
right = mid;
}
}
if (indices.get(left) > last) {
last = indices.get(left);
} else if (indices.get(right) > last) {
last = indices.get(right);
} else {
return false;
}
}
return true;
}
}class Solution {
private static class Node {
String s;
int nextIndex;
Node(String s, int nextIndex) {
this.s = s;
this.nextIndex = nextIndex;
}
}
public int numMatchingSubseq(String str, String[] words) {
List<Node>[] mapping = new ArrayList[255];
for (char c = 'a'; c <= 'z'; ++c) {
mapping[c] = new ArrayList<>();
}
// 将每个word按照首字母放入mapping
for (String word : words) {
mapping[word.charAt(0)].add(new Node(word, 0));
}
int ret = 0;
// 从左到右扫描s,同时如果如果当前字母和某些word匹配,则后移word中的index,
// 同时将该word挪到新的地方
for (char c : str.toCharArray()) {
List<Node> matched = mapping[c];
// 新建一个当作更新后的当前c对应的list,避免一边for一边改
List<Node> newMatched = new ArrayList<>();
for (Node word : matched) {
int index = word.nextIndex;
String s = word.s;
if (index == s.length() - 1) {
ret++;
} else if (s.charAt(index) == s.charAt(index + 1)) {
// 仍然是同一个c
newMatched.add(new Node(s, index + 1));
} else {
mapping[s.charAt(index + 1)].add(new Node(s, index + 1));
}
}
mapping[c] = newMatched;
}
return ret;
}
}