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. TSQL Beginners Challenge 3 - Find the Factorial

    这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...

  2. Angularjs总结(六) 上传附件

    所用插件:angular-file-upload 这个插件用到的几个指令:nv-file-select(点击选择).uploader(用于绑定控制器中新建的uploader对象) HTML: < ...

  3. 限制UITextField/UITextView的输入字数与中文输入之后的英文换行问题

    要限制一个UITextField/UITextView的输入字数,首先想到的应该是通过UITextFieldDelegate/UITextViewDelegate的代理方法来限制,那么如何来更好的限制 ...

  4. CI框架多目录设置

    1,设置目的,前台与后台实现独立目录管理 2.通过http://www.myci.com  访问前台,通过http://www.myci.com/admin 访问后台,   多目录的意思是指在同一个网 ...

  5. 将fastjson元素转化为String[]

    在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...

  6. 从windows到Linux-ubuntu新手

    版本选择: 经多次实验,Ubuntu个人认为长期支持(LTS)版才值得装. VMware9中测试:Ubuntu10.04开机内存170M,Ubuntu12.04开机内存340M. 个人感觉Ubuntu ...

  7. zoj3839-Poker Face

    #include<cstdio>int n;void P(int i,int j,int n,int f){ if(i==n){ for(int k=1;k<=n;k++)print ...

  8. php之购物车类思路及代码

    <?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...

  9. Socket原理

    一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...

  10. 【技术宅6】把一个无限级分类循环成tree结构

    function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...