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的更多相关文章

  1. [LeetCode#249] Group Shifted Strings

    Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...

  2. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  3. Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  4. 249. Group Shifted Strings

    题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...

  5. [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  6. 249. Group Shifted Strings把迁移后相同的字符串集合起来

    [抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...

  7. LeetCode – Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. Group Shifted Strings -- LeetCode

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  9. LeetCode 249. Group Shifted Strings (群组移位字符串)$

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

随机推荐

  1. 初识 Angular 体会

    一句话描述:一个前端的类似MVC框架的JS库 刚接触2天,刚一看感觉和asp.net mvc能实现的功能有点重复. 虽然asp.net的表单验证,Razor语法使其在前端开发有较大提升,但要实现比较高 ...

  2. index ffs、index fs原理考究-1109

    h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...

  3. 菜鸟必备教程,ajax与xml交互传输数据。

    今天,公司让学习ajax,然而我并不会,着急到爆炸,boom~~啥卡拉咔.看着教程一步一步摸索,写出来交互页面,写代码真的好惆怅啊. 额,不说废话,下面是源代码. 首先是ajax的代码,注释真的很重要 ...

  4. Jquery 全选、反选

    jQuery 1.9以后用 prop(); 不用attr 等 $(function() { $('#inputCheck').click(function() { $("input[name ...

  5. yii 验证确认密码是否一致 【"compare",'compareAttribute'=>'password'】

    array("surepassword","compare",'compareAttribute'=>'password','message'=>' ...

  6. hdu3652B-number

    Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...

  7. hadoop 异常 INFO ipc.Client: Retrying connect to server:

    // :: INFO ipc.Client: Retrying connect to server: master/. Already tried , sleepTime= SECONDS) // : ...

  8. BZOJ 2423 最长公共子序列

    Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0, ...

  9. JVM系列五:JVM监测&工具

    JVM系列五:JVM监测&工具[整理中]  http://www.cnblogs.com/redcreen/archive/2011/05/09/2040977.html 前几篇篇文章介绍了介 ...

  10. JSP出现中文乱码问题

    今天纠结了好半天,本地运行程序后没有中文乱码,唯独发到服务器后运行出现了乱码. 究其原因,皆因eclipse环境默认的JSP编码是Iso-8859-1,需要将其改为utf-8,与JSP文件中的编码声明 ...