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: & ...
随机推荐
- 调整CodeIgniter错误报告级别
修改位置:CI根目录 index.php 为开发环境与生产环境定义错误报告级别 if (defined('ENVIRONMENT')) { switch (ENVIRONMENT) { case 'd ...
- Elasticsearch同义词词汇单元过滤器
1 简单扩展 "jump,hop,leap" 搜索jump会检索出包含jump.hop或leap的词 1.1 扩展应用在索引阶段 1.2 扩展应用在查询阶段 1.3 对比 2 简单 ...
- 爬虫:Scrapy16 - Spider Contracts
Scrapy 通过合同(contract)的方式来提供了测试 spider 的集成方法. 可以硬编码(hardcode)一个样例(sample)url,设置多个条件来测试回调函数处理 response ...
- Android Canvas类介绍
当我们调整好画笔之后,现在需要绘制到画布上,这就得用Canvas类了.在Android中既然把Canvas当做画布,那么就可以在画布上绘制我们想要的任何东西.除了在画布上绘制之外,还需要设置一些关于画 ...
- Zigzag数组 -- 面试宝典
最近在看面试宝典,其中看到一个题目说:输入一个正整数n,输出它的zigzag数组. 分析:书上给出了数学方面的思考然后给了代码.但是我感觉如果真是面试或者考试遇到的话,我这种笨脑袋肯定是想不出来的,因 ...
- 纪中集训总结 && 新学期目标
于是紧接着又发了第二篇. 关于这次去完纪中以后的感想,写完后总觉得少了些什么,因此就发一篇小目标集合来凑数补充一下吧. Part I:图论 这方面我去之前就是很有自信,事实证明像基础的最短路.生成树什 ...
- [codeforces] 449C Jzzhu and Apples
原题 质因数分解后贪心即可(最后贪2) #include<cstdio> #include<vector> #include<stack> #include< ...
- BZOJ2594 [Wc2006]水管局长数据加强版 【LCT维护最小生成树】
题目 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的 ...
- Google C++编程风格指南 - 中文版
Google C++编程风格指南 - 中文版 from http://code.google.com/p/google-styleguide/ 版本: 3.133原作者: Benjy Weinberg ...
- 【POJ 2752 Seek the Name, Seek the Fame】
Time Limit: 2000MSMemory Limit: 65536K Description The little cat is so famous, that many couples tr ...