[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: & ...
随机推荐
- angularjs-googleMap googleMap api地址解析与反解析
1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...
- cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务
转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...
- linux 部分命令简单使用介绍-ssh、scp、less、tail、find、grep(持续添加)
ssh 加密的网络协议,提供客户-服务模式. 登录 ssh username@ip ssh ip #不提供用 ...
- hdu_5276
//不管怎么样还是希望天天做笔记把,真是太懒了#include<iostream> #include<cstdio> #include<vector> #inclu ...
- VC++读取资源中文件
//查找目标资源 HRSRC hResource = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MAINPROG), TEXT(& ...
- java中jdk环境配置
配置java环境,俗称jdk环境 首先进入配置环境的目录下:右键鼠标我的电脑->属性->高级系统设置->环境变量,在对应的"系统变量"框下配置一下变量: 规范的配 ...
- java.io.serializable
为什么要实现 java.io.serializable? 简单点:“好处就是将来项目如果要做集群的话,就实现java.io.serializable接口”
- js时间戳与日期格式之间的互转
1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得 ...
- CreateJS第1章 EaselJS基础 (画图)
这章学学EaselJS的基本常用API首先下载createjs库,在项目文件里新建一个js文件夹放里面http://code.createjs.com/ 各种形状 var sp = new creat ...
- 【Windows核心编程】Windows常见数据类型
一,常见数据类型 WORD: 16位无符号整形数据 DWORD: 32位无符号整型数据(DWORD32) DWORD64: 64位无 ...