2023年12月第4周面试算法题总结
809. 情感丰富的文字——阅读理解题
1、s = “abcd”; words = [“abc”]; 的情况怎么处理
2、怎么求lens与lenw?(连续出现的字符串长度)
class Solution {
public:
bool isStretchy(const string& s, const string& word) {
int i = 0;
int j = 0;
while (i < s.size() && j < word.size()) {
if (s[i] != word[j]) {
return false;
}
int lens = 0;
int lenw = 0;
while (i + lens < s.size() && s[i + lens] == s[i]) {
lens++;
}
while (j + lenw < word.size() && word[j + lenw] == word[j]) {
lenw++;
}
if ((lens < 3 && lens != lenw) || (lenw == 0 && lens != 0) || (lens < lenw)) {
return false;
}
i += lens;
j += lenw;
}
return i == s.size() && j == word.size();
}
int expressiveWords(string s, vector<string>& words) {
int count = 0;
for (int i = 0; i < words.size(); i++) {
if (isStretchy(s, words[i])) {
count++;
}
}
return count;
}
};