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: & ...
随机推荐
- POJ1201 Intervals
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- Mac OS下编写对拍程序
介绍 对拍是信息学竞赛中重要的技巧,它通过一个效率低下但正确率可以保证的程序,利用庞大的随机生成数据来验证我们的高级算法程序.对拍最大的优势在于可以通过人力所不能及的速度和数量达到验证的效果.下面我们 ...
- Java线程中run和start方法的区别
http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...
- Oracle 11g ORA-00845: MEMORY_TARGET not supported on this system
启动Oracle 11gR2后报错:ORA-00845 rac1:/home/oracle> sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
- CodeForces 703B(容斥定理)
题目链接:http://codeforces.com/contest/703/problem/B 解题思路: 第一次写 先求出每个点到其他点的价值,并将其记录 dp[i][j]=1(i<j),然 ...
- 常用 SQL 语句
一.SQL中新增列或者说添加字段的语法: alter table 表名 add 列名 数据类型 二.例如:在表texttable中添加一列字符型字段colnew: alter table textta ...
- Yii2 如何使用事件
原文地址:http://www.fancyecommerce.com/2016/04/29/yii2-%E4%BD%BF%E7%94%A8event-1-%EF%BC%8C%E5%A6%82%E4%B ...
- rsync+inotify 实现服务器之间目录文件实时同步(转)
软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...
- 为自己的git添加alias,命令缩写
在多人协作开发时,一般用git来进行代码管理.git有一些命令如:git pull . git push等等,这些命令可以设置alias,也就是缩写.如:git pull 是 git pl, git ...