-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2273.cpp
More file actions
41 lines (40 loc) · 1.1 KB
/
2273.cpp
File metadata and controls
41 lines (40 loc) · 1.1 KB
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
class Solution {
public:
vector<string> removeAnagrams(vector<string>& words) {
string currStr = "";
vector<string> res;
for (auto word : words) {
string temp = word;
sort(temp.begin(), temp.end());
if (temp == currStr) continue;
currStr = temp;
res.push_back(word);
}
return res;
}
};
class Solution {
public:
int counts[26];
bool isSame(string& a, string& b) {
for (int i = 0; i < 26; ++i) counts[i] = 0;
for (auto& c : a) counts[c - 'a']++;
for (auto& c : b) counts[c - 'a']--;
for (int i = 0; i < 26; ++i) {
if (counts[i] != 0) return false;
}
return true;
}
vector<string> removeAnagrams(vector<string>& words) {
int n = words.size();
int index = 0;
vector<string> res;
while (index < n) {
int left = index;
while (index + 1 < n && isSame(words[index], words[index + 1])) index++;
res.push_back(words[left]);
index++;
}
return res;
}
};