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. poj1828

    poj1828 [问题的描述]是这样的:程序猿的近亲 猴子(......)最近在进行王位争夺站. 题中使用二维坐标轴上的点(x,y)来代表猴子所占有的位置, 每只猴子占有一个坐标点.并且一个坐标点上面 ...

  2. PDF在线预览

    1.所需插件jquery.media.js或者pdfobject.js 代码: <html> <head> <style type="text/css" ...

  3. block 浅析

    最近讲了一个关于block的例子 block 可以作为一个参数 进行传递 需要注意的地方是 :block 虽然作为一个参数 但是在函数方法执行的时候 block 是不会在定义它的地方调用 除非你在后边 ...

  4. Asp.Net 注册 邮箱激活

    数据库 表的设计 State为用户状态  0为禁用  1为可用  默认为0,下面有个UserGUID,这个字段将来用于激活账户 首先你要写一个表单,验证码神马的,这个我就不写了..直接写处理的 代码在 ...

  5. JavaScript学习笔记--ES6学习(五) 数值的扩展

    ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...

  6. 【BZOJ2049】【LCT】Cave 洞穴勘测

    Description 辉 辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通 道组成,并且每条通道连接了 ...

  7. 导入Android工程源码出现乱码问题的解决方法

    可以尝试着从以下三个方法进行调试,一般情况下会完美解决的: 1.windows->Preferences->General->Content Types->Text->J ...

  8. 关掉PUTTY后,进程仍可以运行。

    如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户之后继续运行相应的进程.no hup就是不挂起的意思( no hang up).该命令 ...

  9. css去除webkit内核的默认样式

    做移动端的朋友应该知道,iphone的默认按钮是个很恶心的圆角,select下拉框也有默认样式无法修改. 这时候可以用到 -webkit-appearance:none //去除默认样 在按钮和sel ...

  10. ubuntu 安装 open in teminal

    sudo apt-get install nautilus-open-terminalnautilus -q