Group Shifted Strings -- LeetCode

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence:

"abc" -> "bcd" -> ... -> "xyz"

Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.

For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"]
A solution is:

[
  ["abc","bcd","xyz"],
  ["az","ba"],
  ["acef"],
  ["a","z"]
]
 1 class Solution {
 2 public:
 3     string help(string word) {
 4         for (int i = 1, n = word.size(); i < n; i++) {
 5             word[i] = word[i] - word[0] + ‘a‘;
 6             while (word[i] < ‘a‘) word[i] += 26;
 7         }
 8         word[0] = ‘a‘;
 9         return word;
10     }
11     vector<vector<string>> groupStrings(vector<string>& strings) {
12         unordered_map<string, int> dict;
13         vector<vector<string> > res;
14         for (int i = 0, n = strings.size(); i < n; i++) {
15             string base = help(strings[i]);
16             if (dict.count(base) == 0) {
17                 dict.insert(make_pair(base, res.size()));
18                 vector<string> subGroup(1, strings[i]);
19                 res.push_back(subGroup);
20             }
21             else res[dict[base]].push_back(strings[i]);
22         }
23         return res;
24     }
25 };
时间: 2024-07-28 20:37:40

Group Shifted Strings -- LeetCode的相关文章

249.Group Shifted Strings

/* *249.Group Shifted Strings *2016-6-18 by Mingyang *Given a string, we can "shift" each of its letter to its successive letter, *for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: *&q

Leetcode: Group Shifted Strings

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -> &quo

[LeetCode#249] Group Shifted Strings

Problem: Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -

LeetCode – Group Shifted Strings

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -> &quo

[?]*Group Shifted Strings

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -> &quo

Multiply Strings leetcode java

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno

Isomorphic Strings leetcode

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.

Multiply Strings leetcode

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. Subscribe to see which companies asked this question ACM竞赛常考的大数相乘算法,利用字符串来表示大数字进行计算. string m

Encode and Decode Strings -- LeetCode

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { // ... your