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 3基础教程14-在文件尾部更新内容
本文介绍在一个已经存在的文件尾部添加内容,还是用到write方法. 这里exampleFile.txt是前面文件创建的文件,里面有两行文字.
- Linux下samba编译与安装(Ubuntu和嵌入式linux)
Ubuntu[i386-linux下安装过程] 1.安装samba $ sudo apt-get install samba $ sudo apt-get install smbfs (可选) $ s ...
- 内存cgroup
内存cgroup的值都是从哪里来的呀 page_counter_charge是增加page_counter的计数, try_charge函数和mem_cgroup_migrate函数是增加普通进程内存 ...
- [CF845G]Shortest Path Problem?
题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...
- 项链 [FFT]
题面 思路 这题很像bzoj4827礼物 还是一样的思路,我们把$y$倍长,$y[i+k]=y[i]+n$ 然后令$f(s,c)$表示从$y$的第$s$个开始匹配,位置偏移量为$c$的答案 可以得到$ ...
- ubuntu 解压
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...
- Java Web 路径问题
可能在做文件上传或者 图片加载,资源加载 时候用到文件相对服务器地址 System.out.println(request.getRemoteUser()); //客户端用户System.out.pr ...
- PEP8特性
Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读, ...
- noi2017 T1 整数 ——线段树
loj.ac上有 题目传送门 不过我还是把题目搬过来吧 整数(integer)[题目背景]在人类智慧的山巅,有着一台字长为 1048576 位的超级计算机,著名理论计算机科 学家 P 博士正用它进行 ...
- 推送通知iOS客户端编写实现及推送服务器端编写
http://blog.csdn.net/tonny_guan/article/details/8963262 1.iOS客户端编程 推送通知技术在Mac OS X和iOS系统上都可以运行,我们本章主 ...