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.
题解:
既然是group,那么我们需要有一个规则,把符合规则的放在一起。那么,规则是什么呢?
根据题目的意思,对于两串字符串,如果字符串的相邻字符的差值是一致的,那么我们就可以把它们放在一起。
但是对于az, ba这样的,字符之间的差值不一样啊。但是为何它们是一组的呢?
az字符之间的差值是25,ba之间字符的差值是-1,但是字符是每隔26就一循环,所以,对于-1,你一旦加上26就是25.
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> result = new ArrayList<List<String>>();
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = ; i < strings.length; i++) {
StringBuffer sb = new StringBuffer();
for (int j = ; j < strings[i].length(); j++) {
sb.append(Integer.toString(((strings[i].charAt(j) - strings[i].charAt()) + ) % ));
sb.append(" ");
}
String shift = sb.toString();
if (map.containsKey(shift)) {
map.get(shift).add(strings[i]);
} else {
List<String> list = new ArrayList<String>();
list.add(strings[i]);
map.put(shift, list);
}
}
for (String s : map.keySet()) {
Collections.sort(map.get(s));
result.add(map.get(s));
}
return result;
}
}
Group Shifted Strings的更多相关文章
- [Locked] Group Shifted Strings
Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- [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: & ...
- 249. Group Shifted Strings把迁移后相同的字符串集合起来
[抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...
- LeetCode – Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings -- LeetCode
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: & ...
随机推荐
- u11-nav02
header:before, header:after ,.navigation:before, .navigation:after,.nav-row:before, .nav-row:after,. ...
- TRUNC函数,ORA-01898 精度说明符过多
TRUNC(SYSDATE)即可默认当前日期(年月日),TRUNC(SYSDATE,'yyyy-mm-dd'),精度说明符过多
- poj3237 树链剖分 暴力
NEGATE a,b 将a b间的线段取反,这题应该用线段树+成段更新.我成段更新写的挫了,试了暴力修改过了(数据水). 也是简单的题目.不过要注意点和边的区别. #include<queue& ...
- Yii2 radioList设置默认值
可以在对应的Controller的action中设置 $model->type = 1; 在view中 <?php $form = ActiveForm::begin(); ?> ...
- php两种导出excel的方法
所需要的:jquery库,phpexcel插件,页面导出excel效果测试文件explode.php,excel导出功能实现文件exp.php和explode_excel.php,文件相关内容在此文下 ...
- hiho1015(kmp+统计出现次数)
http://hihocoder.com/problemset/problem/1015 时隔多天再次温习了一下KMP #include <iostream> #include <c ...
- app工程构成
1)工程配置文件: 2)源代码: 3)资源文件.
- Spring学习3—控制反转(IOC)Spring依赖注入(DI)和控制反转(IOC)
一.思想理解 Spring 能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的Service对象,还是持久层的DAO对象,都可在Spring的 管理下有机地协调.运行.S ...
- 修改myeclipse 新建JSP文件时的默认模板
MyEclipse中构造新的jsp模板(原创) 首先随便打开一个jsp页,在网页中单击右键选择:preferences 打开后如图所示,找到jsp template选项. 选择new,在弹出的提示框, ...
- TP中二维数组的遍历输出
例子分析 <volist name="list" id="vo"> <volist name="vo['sub']" id ...