-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path249_Group-Shifted-Strings.cpp
More file actions
32 lines (30 loc) · 1007 Bytes
/
249_Group-Shifted-Strings.cpp
File metadata and controls
32 lines (30 loc) · 1007 Bytes
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
class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
vector<vector<string>> res;
unordered_map<string, int> group;
for(const string& s : strings) ++group[s];
while(!group.empty()){
auto it = group.begin();
string curr = it->first;
int count = it->second;
vector<string> groupVec(count, curr);
group.erase(group.begin());
for(int i = 0; i < 25; ++i){
for(char& c : curr){
// right shift each character
c = ((c-'a'+1)%26) + 'a';
}
auto f = group.find(curr);
if(f != group.end()){
for(int j = 0; j < f->second; ++j){
groupVec.emplace_back(curr);
}
group.erase(curr);
}
}
res.emplace_back(groupVec);
}
return res;
}
};