-
Notifications
You must be signed in to change notification settings - Fork 0
/
group-shifted-strings.cpp
65 lines (50 loc) · 1.15 KB
/
group-shifted-strings.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
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// https://leetcode.com/problems/group-shifted-strings/
class Solution {
public:
vector<vector<string>> groupStrings(vector<string> &strings) {
vector<vector<string>> r;
unordered_map<string, vector<string>> hmap;
vector<string> length1s;
if (strings.empty()) {
return r;
}
// O( N )
for (auto &s : strings) {
if (1 == s.size()) {
length1s.push_back(s);
continue;
}
string key;
for (size_t i = 1; i < s.size(); i++) {
key.push_back(s[i] - s[i - 1]);
if (int8_t(key.back()) < 0) {
key.back() += 26;
}
key.back() += 'a';
}
hmap[key].push_back(s);
}
// O( N )
for (auto kv : hmap) {
r.push_back(kv.second);
}
sort(r.begin(), r.end(), [](const auto &lhs, const auto &rhs) {
return lhs.size() > rhs.size();
});
// O(log(N))
if (length1s.size() > 0) {
r.push_back(length1s);
}
return r;
}
};