[Locked] Group Shifted Strings
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" -> ... -> "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"],
Return:
[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]
Note: For the return value, each inner list's elements must follow the lexicographic order.
分析:
由于shift后的字符串只有26种形式,所以思路比较直观,线性遍历一遍,在原集合中再遍历查找这26种形式是否存在
代码:
//根据当前字符串生成按首字母顺序排列的字符串数组
vector<string> generateSS(string str) {
vector<string> vs;
//找到基准字符串与当前字符串的shift步骤差
int i = int('a' - str[]), j = i + ;
for(; i <= j; i++) {
string s = str;
//进行shift操作,注意越界情况需要求余
for(char &c : s)
c = (c + i - 'a') % + 'a';
vs.push_back(s);
}
return vs;
}
vector<vector<string> > shiftedString(vector<string> strings) {
vector<vector<string> > vvs;
//通过map便于O(1)时间查询,以及标志位表明是否已被使用
unordered_map<string, int> hash;
for(string str : strings)
hash.insert(make_pair(str, ));
for(auto h : hash) {
vector<string> vs;
//已被使用则跳过
if(h.second == )
continue;
vector<string> ss = generateSS(h.first);
for(string str : ss) {
auto pos = hash.find(str);
if(pos != hash.end()) {
pos->second = ;
vs.push_back(str);
}
}
vvs.push_back(vs);
}
return vvs;
}
[Locked] Group Shifted Strings的更多相关文章
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- [LeetCode] 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: & ...
- 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: & ...
- 249. Group Shifted Strings把迁移后相同的字符串集合起来
[抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...
- LeetCode – Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings -- LeetCode
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: & ...
随机推荐
- HTML5 文件域+FileReader 分段读取文件(四)
一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...
- javascript DOM小结
一:定义 dom:文档对象模型. dom是针对HTML和XML文档的一个API.dom描绘了一个层次化的节点树,允许开发人员添加.移除.修改页面的某一部分. 1:childNodes(返回当前节点的子 ...
- 解决办法:CMake编译时出现“error in configuration process project files may be invalid”
无论是CMake2.84 还是当前最新的CMake2.87都可能会出现这种错: 查遍国内外的网上都没有给出可行办法,结果还是自己解决了 现把出错原因和解决办法如下:出错原因:因是英文版本,通常安装没有 ...
- javascript——迭代方法
<script type="text/javascript"> //五个迭代方法 都接受两个参数:要在每一项上运行的函数 和 运行该函数的作用域(可选) //every ...
- Android安装 sdk+jdk+Eclipse+Adt开发工具
根据别人提供的手册和安装过程体验加以更新和详细描述 安装Android开发工具 开发Android应用程序的门坎并不高,因为Google已经为Android应用程序开发提供了免费而且跨平台的集成开发环 ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- document模板
http://bce.baidu.com/doc/CDS/GettingStarted.html
- JavaScript生成器+随机数的使用
function* getIndex(indexList){ var len = indexList.length; var m; while(indexList.length > 0){ m ...
- ipython与python的区别
http://mba.shengwushibie.com/itbook/BookChapter.asp?id=8745 http://www.cnblogs.com/yangze/archive/20 ...
- ASP.NET MVC 定义JsonpResult实现跨域请求
1:原理 在js中,XMLHttpRequest是不能请求不同域的数据,但是script标签却可以,所以可以用script标签实现跨域请求.具体是定义一个函数,例如jsonp1234,请求不同域的ur ...