comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
Medium |
|
Perform the following shift operations on a string:
- Right shift: Replace every letter with the successive letter of the English alphabet, where 'z' is replaced by 'a'. For example,
"abc"
can be right-shifted to"bcd"
or"xyz"
can be right-shifted to"yza"
. - Left shift: Replace every letter with the preceding letter of the English alphabet, where 'a' is replaced by 'z'. For example,
"bcd"
can be left-shifted to"abc" or
"yza"
can be left-shifted to"xyz"
.
We can keep shifting the string in both directions to form an endless shifting sequence.
- For example, shift
"abc"
to form the sequence:... <-> "abc" <-> "bcd" <-> ... <-> "xyz" <-> "yza" <-> ...
.<-> "zab" <-> "abc" <-> ...
You are given an array of strings strings
, group together all strings[i]
that belong to the same shifting sequence. You may return the answer in any order.
Example 1:
Input: strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
Output: [["acef"],["a","z"],["abc","bcd","xyz"],["az","ba"]]
Example 2:
Input: strings = ["a"]
Output: [["a"]]
Constraints:
1 <= strings.length <= 200
1 <= strings[i].length <= 50
strings[i]
consists of lowercase English letters.
We use a hash table a
'. That is,
We iterate through each string. For each string, we calculate its shifted string
Finally, we take out all the values in
The time complexity is
class Solution:
def groupStrings(self, strings: List[str]) -> List[List[str]]:
g = defaultdict(list)
for s in strings:
diff = ord(s[0]) - ord("a")
t = []
for c in s:
c = ord(c) - diff
if c < ord("a"):
c += 26
t.append(chr(c))
g["".join(t)].append(s)
return list(g.values())
class Solution {
public List<List<String>> groupStrings(String[] strings) {
Map<String, List<String>> g = new HashMap<>();
for (var s : strings) {
char[] t = s.toCharArray();
int diff = t[0] - 'a';
for (int i = 0; i < t.length; ++i) {
t[i] = (char) (t[i] - diff);
if (t[i] < 'a') {
t[i] += 26;
}
}
g.computeIfAbsent(new String(t), k -> new ArrayList<>()).add(s);
}
return new ArrayList<>(g.values());
}
}
class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
unordered_map<string, vector<string>> g;
for (auto& s : strings) {
string t;
int diff = s[0] - 'a';
for (int i = 0; i < s.size(); ++i) {
char c = s[i] - diff;
if (c < 'a') {
c += 26;
}
t.push_back(c);
}
g[t].emplace_back(s);
}
vector<vector<string>> ans;
for (auto& p : g) {
ans.emplace_back(move(p.second));
}
return ans;
}
};
func groupStrings(strings []string) [][]string {
g := make(map[string][]string)
for _, s := range strings {
t := []byte(s)
diff := t[0] - 'a'
for i := range t {
t[i] -= diff
if t[i] < 'a' {
t[i] += 26
}
}
g[string(t)] = append(g[string(t)], s)
}
ans := make([][]string, 0, len(g))
for _, v := range g {
ans = append(ans, v)
}
return ans
}