方法一:
class Solution {
public int expressiveWords(String s, String[] words) {
int res = 0;
for (String word : words) {
if (word.length() > s.length()) continue;
int idxS = 0, idxW = 0;
boolean isExpand = true;
while (idxS < s.length() || idxW < word.length()) {
if ((idxS == s.length() && idxW < word.length()) || (idxS < s.length() && idxW == word.length())) {
isExpand = false;
break;
}
// 如果此时的字符不同,则一定不满足
if (s.charAt(idxS) != word.charAt(idxW)) {
isExpand = false;
break;
}
char c = s.charAt(idxS);
int countS = 0, countW = 0;
while (idxS < s.length() && s.charAt(idxS) == c) {
idxS++;
countS++;
}
while (idxW < word.length() && word.charAt(idxW) == c) {
idxW++;
countW++;
}
if (countS == countW) continue;
if (countS < countW || countS < 3) {
isExpand = false;
break;
}
}
if (isExpand) res++;
}
return res;
}
}