思路:
- 指针i指向s[i],指针j指向t[j],同时遍历
- 如果s[i]==s[j],则记录该字符ch
- 分别统计s串和t串里,该字符出现的个数,s串ch个数为cnti,t串ch的个数为cntj
- 如果t串能拓展成s串,则必有cnti ≥ cntj
- 当cnti < cntj 时,肯定不满足要求
- 当cnti != cntj 时,如果cnti < 3,则肯定不满足要求
- 如果循环结束,i和j指针都超出边界,说明t串可以拓展成s串
class Solution {
public int expressiveWords(String s, String[] words) {
int res=0;
for(String x:words)
if(check(s,x)) res++;
return res;
}
public boolean check(String s,String x)
{
int i=0,j=0;
while(i<s.length()&&j<x.length())
{
if(s.charAt(i)!=x.charAt(j)) return false;
char ch=s.charAt(i);
int cnti=0,cntj=0;
while(i<s.length()&&s.charAt(i)==ch)
{
cnti++;
i++;
}
while(j<x.length()&&x.charAt(j)==ch)
{
cntj++;
j++;
}
if(cnti<cntj) return false;
if(cnti!=cntj&&cnti<3) return false;
}
return i==s.length()&&j==x.length();
}
}