forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (60 loc) · 1.85 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// Source : https://leetcode.com/problems/expressive-words/description/
/// Author : liuyubobobo
/// Time : 2018-03-31
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(len(words) * len(words[i]))
/// Space Complexity: O(max_length_of_words)
class Solution {
public:
int expressiveWords(string S, vector<string>& words) {
vector<string> S_groups = get_groups(S);
int res = 0;
for(const string& word: words)
if(ok(word, S_groups)){
// cout << "ok!" << word << endl;
res ++;
}
return res;
}
private:
bool ok(const string& s, const vector<string>& S_groups){
vector<string> s_groups = get_groups(s);
if(s_groups.size() != S_groups.size())
return false;
for(int i = 0 ; i < s_groups.size() ; i ++){
if(s_groups[i][0] != S_groups[i][0])
return false;
if(s_groups[i].size() > S_groups[i].size())
return false;
if(s_groups[i].size() == S_groups[i].size())
continue;
assert(s_groups[i].size() < S_groups[i].size());
if(S_groups[i].size() <= 2)
return false;
}
return true;
}
vector<string> get_groups(const string& s){
vector<string> res;
int start = 0;
for(int i = start + 1 ; i <= s.size() ; )
if(i == s.size() || s[i] != s[start]){
res.push_back(s.substr(start, i - start));
start = i;
i = start + 1;
}
else
i ++;
return res;
}
};
int main() {
string S1 = "heeellooo";
vector<string> words1 = {"hello", "hi", "helo"};
cout << Solution().expressiveWords(S1, words1) << endl;
return 0;
}