题目
题解
二分查找
朴素方法:双指针,i为s指针,j为words中单词指针,如果s[i]==word[j],i,j同时向后走,否则i向后走,j不动,时间复杂度太高
改进:存储s中字符和其对应出现的下标,记数组为pos[];对于word.charAt(i),需要在pos[]中找到对应下标p,如果对于word.charAt(i+1),pos[]中对应下标>p,说明能成子序列,否则不行
class Solution {
public int numMatchingSubseq(String s, String[] words) {
int n=s.length();
//初始化pos,存储每个字符的对应下标
List<Integer>[] pos=new ArrayList[26];
for(int i=0;i<26;i++){
pos[i]=new ArrayList<>();
}
for(int i=0;i<n;i++){
pos[s.charAt(i)-'a'].add(i);
}
//匹配
int res=words.length;
for(String word:words){
int p=-1;
//单词比s还长,直接跳过
if(word.length()>n){
res--;
continue;
}
for(int i=0;i<word.length();i++){
char ch=word.charAt(i);
//s中没有ch 或者 ch的位置在p前(不能构成子序列)
if(pos[ch-'a'].isEmpty()||pos[ch-'a'].get(pos[ch-'a'].size()-1)<=p){
res--;
break;
}
//找ch在s中的存在位置
p=binSearch(pos[ch-'a'],p);
}
}
return res;
}
//二分查找
public int binSearch(List<Integer>list,int target){
int left=0,right=list.size()-1;
while(left<right){
int mid=(left+right)/2;
if(list.get(mid)>target)
right=mid;
else
left=mid+1;
}
return list.get(left);
}
}
时间复杂度: O ( ∑ s i z e i × l o g n ) O(∑size_i×logn) O(∑sizei×logn), s i z e i size_i sizei是words中每个单词的长度。对于每个单词word,查询匹配的时间为 s i z e i × l o g n size_i×logn sizei×logn
空间复杂度: O ( n ) O(n) O(n)
三指针
朴素方法每次都要从头开始遍历s,增加了额外的时间开销。
三指针方法遍历words数组中的字符串,用s的每个字符去匹配所有的word,只需遍历一次s。
具体方法是用队列p,p[i]中的i为s中字符i;p[i]的元素int[],int[0]代表在哪个word中,int[1]代表在word字符串的哪个位置,如果匹配上int[1]就向后移动一位,如果能达到word末尾,则说明这个word是s的子序列。
class Solution {
public int numMatchingSubseq(String s, String[] words) {
int n=s.length();
//初始化p
Queue<int[]>[] p=new Queue[26];
for(int i=0;i<26;i++){
p[i]=new ArrayDeque<>();
}
//p存储每个字符的[在第几个word中,是word的第几个字符]
for(int i=0;i<words.length;i++){
p[words[i].charAt(0)-'a'].offer(new int[]{i,0});
}
//匹配
int res=0;
for(int i=0;i<n;i++){
int len=p[s.charAt(i)-'a'].size();
//用一个s中的字符去匹配所有的word
while(len>0){
int[] t=p[s.charAt(i)-'a'].poll();
if(t[1]==words[t[0]].length()-1){
res++;//如果word能匹配到末尾,说明满足条件
}
else{
t[1]++;//第t[0]个word往后挪一个字符
p[words[t[0]].charAt(t[1])-'a'].offer(t);//加入到p中
}
len--;
}
}
return res;
}
}
时间复杂度: O ( ∑ s i z e i + n ) O(∑size_i+n) O(∑sizei+n), s i z e i size_i sizei是words中每个单词的长度。
空间复杂度: O ( m ) O(m) O(m),m为words[]数组的长度,空间复杂度主要为存储字符串数组中每一个字符串现在的对应匹配指针的空间开销。