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"]
]
class Solution {
public:
string help(string word) {
for (int i = , n = word.size(); i < n; i++) {
word[i] = word[i] - word[] + 'a';
while (word[i] < 'a') word[i] += ;
}
word[] = 'a';
return word;
}
vector<vector<string>> groupStrings(vector<string>& strings) {
unordered_map<string, int> dict;
vector<vector<string> > res;
for (int i = , n = strings.size(); i < n; i++) {
string base = help(strings[i]);
if (dict.count(base) == ) {
dict.insert(make_pair(base, res.size()));
vector<string> subGroup(, strings[i]);
res.push_back(subGroup);
}
else res[dict[base]].push_back(strings[i]);
}
return res;
}
};
Group Shifted Strings -- LeetCode的更多相关文章
- [Locked] Group Shifted Strings
Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- LeetCode 249. Group Shifted Strings (群组移位字符串)$
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode – Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- 249. Group Shifted Strings
题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...
- [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- Python zip()函数实现并行迭代
示例1: for i, j in zip(range(0, 10), range(1, 11)): print(i, j) 输出结果: 0 11 22 33 44 55 66 77 88 99 10 ...
- 浅谈 css 之 position用法
在 css中, position 属性有四个值可用: static(默认值).absolute.relative.fixed. relative:相对定位(相对于自身进行在常规流中的位置进行定位,保留 ...
- badboy录制提示当前页面的脚本发生错误
利用badboy录制时,发生了错误: 网上查了查,说badboy默认使用IE浏览器,打开Internet选项—>高级,图中的两个选项不要勾选即可 然鹅,然鹅,并没有作用... 请教了好心的同行, ...
- day06_03 购物车讲解01
1.0 思路 1.1 列表嵌套 # name1 = ['mac','book','bike','kindle'] a = [[1,2,3],'alex',(2,3,4)] print(a[0]) #& ...
- java 继承小结
[code=java] //多态的经典例子 //向上转型后,父类只能调用子类和父类的共同方法和的重写方法(方法名相同,参数也相同),不能调用重载方法(方法名相同,但参数不同) class A { pu ...
- KMP算法-Python版
KMP算法-Python版 传统法: 从左到右一个个匹配,如果这个过程中有某个字符不匹配,就跳回去,将模式串向右移动一位.这有什么难的? 我们可以 ...
- django ORM中的表关系
多对一: 为了方便理解,两个表之间使用ForeignKey连接时,使用ForeignKey的字段所在的表为从表,被ForeignKey连接的表为主表. 使用场景:书和出版社之间的关系,一本书只能由一个 ...
- B - Help Jimmy
B - Help Jimmy Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem ...
- 【BZOJ 2458 最小三角形】
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1551 Solved: 549[Submit][Status][Discuss] Descripti ...
- 【CZY选讲·Triangle】
题目描述 长度为的铁丝,你可以将其分成若干段,并把每段都折成一个三角形.你还需要保证三角形的边长都是正整数并且三角形两两相似,问有多少种不同的分法. 数据范围 1≤≤10^6 题解: ①相 ...