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.

 

这道题让我们重组偏移字符串,所谓偏移字符串,就是一个字符串的每个字符按照字母顺序表偏移相同量得到的另一个字符串,两者互为偏移字符串,注意相同字符串是偏移字符串的一种特殊情况,因为偏移量为0。现在给了一堆长度不同的字符串,让把互为偏移字符串的归并到一起,博主最开始想的是建立字符度和该长度的所有偏移字符串的映射,但是很明显的错误是相同长度的不一定都是偏移字符串,比如 'ab' 和 'ba',所以只能用 HashMap 来建立一个字符串和所有和此字符串是偏移字符串的集合之间的映射,由于题目要求结果是按字母顺序的,所以用 multiset 来保存结果,一来可以保存重复字符串,二来可以自动排序。然后博主还写了一个判断二个字符串是否互为偏移字符串的函数,注意在比较两个字母距离时采用了加 26,再对 26 取余的 trick。遍历给定字符串集,对于遍历到的字符串,再遍历哈希表,和每个关键字调用 isShifted 函数来比较,如果互为偏移字符串,则加入其对应的字符串集,并标记 flag,最后遍历完 HashMap,没有跟任何关键字互为偏移,那么就新建一个映射,最后要做的就是把 multiset 转换为 vector 即可,参见代码如下:

解法一:

// Correct but complicated
class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
vector<vector<string> > res;
unordered_map<string, multiset<string>> m;
for (auto a : strings) {
bool b = false;
for (auto it = m.begin(); it != m.end(); ++it) {
if (isShifted(it->first, a)) {
it->second.insert(a);
b = true;
}
}
if (!b) m[a] = {a};
}
for (auto it = m.begin(); it != m.end(); ++it) {
res.push_back(vector<string>(it->second.begin(), it->second.end()));
}
return res;
}
bool isShifted(string s1, string s2) {
if (s1.size() != s2.size()) return false;
int diff = (s1[] + - s2[]) % ;
for (int i = ; i < s1.size(); ++i) {
if ((s1[i] + - s2[i]) % != diff) return false;
}
return true;
}
};

上面那个方法挺复杂的,其实有更好的方法,网友的智慧无穷啊,上面那个方法的不高效之处在于对于每个遍历到的字符串,都要和 HashMap 中所有的关键字都比较一次,而其实我们可以更加巧妙的利用偏移字符串的特点,那就是字符串的每个字母和首字符的相对距离都是相等的,比如 abc 和 efg 互为偏移,对于 abc 来说,b和a的距离是1,c和a的距离是2,对于 efg 来说,f和e的距离是1,g和e的距离是2。再来看一个例子,az 和 yx,z和a的距离是 25,x和y的距离也是 25 (直接相减是 -1,这就是要加 26 然后取余的原因),那么这样的话,所有互为偏移的字符串都有个 unique 的距离差,根据这个来建立映射就可以很好的进行单词分组了,这个思路真实太赞了,参见代码如下:

解法二:

class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
vector<vector<string> > res;
unordered_map<string, multiset<string>> m;
for (auto a : strings) {
string t = "";
for (char c : a) {
t += to_string((c + - a[]) % ) + ",";
}
m[t].insert(a);
}
for (auto it = m.begin(); it != m.end(); ++it) {
res.push_back(vector<string>(it->second.begin(), it->second.end()));
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/249

类似题目:

Group Anagrams

参考资料:

https://leetcode.com/problems/group-shifted-strings/

https://leetcode.com/problems/group-shifted-strings/discuss/67442/My-Concise-JAVA-Solution

https://leetcode.com/problems/group-shifted-strings/discuss/67451/4ms-Easy-C%2B%2B-Solution-with-Explanations

https://leetcode.com/problems/group-shifted-strings/discuss/67452/Concise-10-lines-JAVA-Solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Group Shifted Strings 群组偏移字符串的更多相关文章

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

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

  2. LeetCode – Group Shifted Strings

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

  3. [Locked] Group Shifted Strings

    Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...

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

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

  5. [LeetCode] 249. Group Shifted Strings 分组偏移字符串

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

  6. [LeetCode#249] Group Shifted Strings

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

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

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

  8. Group Shifted Strings -- LeetCode

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

  9. Group Shifted Strings

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

随机推荐

  1. [协议]ICMP协议剖析

    1.ICMP简介 ICMP全名为(INTERNET CONTROL MESSAGE PROTOCOL)网络控制消息协议. ICMP的协议号为1. ICMP报文就像是IP报文的小弟,总顶着IP报文的名头 ...

  2. 详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能

    文件同步传输工具比较多,传输的方式也比较多,比如:FTP.共享.HTTP等,我这里要讲的就是基于HTTP协议的WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器这样的一个工具(简称:一 ...

  3. React-Native学习系列(二) Image和ScrollView

    接下来,我们接着(一)继续讲,今天我们学习的是Image组件和ScrollView组件. Image组件 Image:一个用于显示多种不同类型图片的React组件.那么要如何使用呢? 引入本地图片: ...

  4. 报错:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

    SqlParameter[] sp = { new SqlParameter("@nGridID",SqlDbType.BigInt), new SqlParameter(&quo ...

  5. Configure bridge on a team interface using NetworkManager in RHEL 7

    SOLUTION IN PROGRESS February 29 2016 KB2181361 environment Red Hat Enterprise Linux 7 Teaming,Bridg ...

  6. 记录一次bug解决过程:resultType和手动开启事务

    一.总结 二.BUG描述:MyBatis中resultType使用 MyBatis中的resultType类似于入参:parameterType.先看IDCM项目中的实际使用案例代码,如下: // L ...

  7. 4.6 .net core依赖注入的封装

    现在流行的系统一般都采用依赖注入的实现方式,利用DI容器来直接获取所用到的类/接口的实例..net core也一样采用DI的方式,提供了DI容器的接口IServiceCollection,并提供了基于 ...

  8. Laravel大型项目系列教程(一)

    Laravel大型项目系列教程(一) 一.课程概述 1.课程介绍 本教程将使用Laravel完成一个多用户的博客系统,大概会包含如下内容: 路由管理. 用户管理,如用户注册.修改信息.锁定用户等. 文 ...

  9. webservice入门实例,CXF方式

    1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...

  10. Linux:常用命令

    文件压缩.解压 网络.进程 磁盘.文件使用情况 内存使用 1.文件压缩.解压 1)tar.gz文件解压: .bin.tar.gz 解压到指定目录: (指定的目录是存在的) .bin. 2)zip 文件 ...