-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathis_stretchy.cpp
47 lines (44 loc) · 1.42 KB
/
is_stretchy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
vector<pair<char, int>> compressString(string &s) {
vector<pair<char, int>> charMap;
unordered_map<char, int> count;
char prevChar = s[0];
for(auto &chr : s) {
if(chr == prevChar) {
count[chr]++;
}
else {
charMap.push_back({prevChar, count[prevChar]});
count[chr] = 1;
}
prevChar = chr;
}
charMap.push_back({prevChar, count[prevChar]});
return charMap;
}
bool isStretchy(vector<pair<char, int>> &countS, string &word) {
auto countW = compressString(word);
if(countS.size() != countW.size()) return false;
for(int i = 0; i < countW.size(); i++) {
if(countS[i].first != countW[i].first) {
return false;
}
if(!(countS[i].second == countW[i].second or countS[i].second >= max(countW[i].second, 3))) {
return false;
}
}
return true;
}
public:
int expressiveWords(string s, vector<string>& words) {
if(s.size() == 0) return 0;
int count = 0;
auto countS = compressString(s);
for(auto &word : words) {
if(isStretchy(countS, word)) {
count++;
}
}
return count;
}
};