forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (35 loc) · 991 Bytes
/
main.cpp
File metadata and controls
47 lines (35 loc) · 991 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Source : https://leetcode.com/problems/group-shifted-strings/description/
/// Author : liuyubobobo
/// Time : 2018-10-03
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/// Using HashMap
/// Time Complexity: O(n * average len of s)
/// Space Compleity: O(n * average len of s)
class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
unordered_map<string, vector<string>> map;
for(const string& s: strings){
string key = getKey(s);
map[key].push_back(s);
}
vector<vector<string>> res;
for(const pair<string, vector<string>>& p: map)
res.push_back(p.second);
return res;
}
private:
string getKey(string s){
int dis = 26 - (s[0] - 'a');
for(int i = 0; i < s.size(); i ++)
s[i] = 'a' + (s[i] + dis) % 26;
return s;
}
};
int main() {
return 0;
}