[LeetCode#249] Group Shifted Strings
Problem:
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.
Analysis:
This problem is very easy.
The basic idea is to shift all words back into the form starting with 'a'. (all digits must shift the same distance). If the two words share the same shifted word, it means they actually come from the same shift group. Thus I have developed a shiftStr function for this purpose.
private String shiftStr(String str) {
StringBuffer buffer = new StringBuffer();
char[] char_array = str.toCharArray();
int dist = str.charAt(0) - 'a';
for (char c : char_array)
buffer.append((c - 'a' - dist + 26) % 26 + 'a');
return buffer.toString();
} Step 1: get the distance each word must shift (leftward, it actually does not matter). Use the first character of a Word, and compute the distance.
int dist = str.charAt(0) - 'a'; Step 2: shift all characters through the same distance.
for (char c : char_array)
buffer.append((c - 'a' - dist + 26) % 26 + 'a');
Note there is trick for this. Since 'c' - 'a' may be a negative number, we plus 26 to make it positive.
Note: to use the extra range wayaround, we also need to minus 'a'. (c - 'a' - dist + 26) % 26 + 'a'
We first make the character c have the index in the range [0 , 25] : c - 'a'.
Then we shift he character in the range through minus dist: c - 'a' - dist.
To avoid negative situation, we plus 26 after it. (since the range is 26, it would have no effect over positive number).
The we take mod of 26 for positive case (which exceeds 26).
The we convert it back to ASCII range.
Solution:
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
if (strings == null)
throw new IllegalArgumentException("strings is null");
List<List<String>> ret = new ArrayList<List<String>> ();
if (strings.length == 0)
return ret;
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>> ();
for (String str : strings) {
String shifted_str = shiftStr(str);
if (map.containsKey(shifted_str)) {
map.get(shifted_str).add(str);
} else{
ArrayList<String> item = new ArrayList<String> ();
item.add(str);
map.put(shifted_str, item);
ret.add(item);
}
}
for (List<String> list : ret)
Collections.sort(list);
return ret;
}
private String shiftStr(String str) {
StringBuffer buffer = new StringBuffer();
char[] char_array = str.toCharArray();
int dist = str.charAt(0) - 'a';
for (char c : char_array)
buffer.append((c - 'a' - dist + 26) % 26 + 'a');
return buffer.toString();
}
}
[LeetCode#249] Group Shifted Strings的更多相关文章
- LeetCode 249. Group Shifted Strings (群组移位字符串)$
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: & ...
- 249. Group Shifted Strings
题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...
- 249. Group Shifted Strings把迁移后相同的字符串集合起来
[抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...
- [Locked] Group Shifted Strings
Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- 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: & ...
- [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- Elasticsearch 5.0
Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...
- SQL SERVER 强制排序规则查询
有时会需要在2个DB之间的数据做比较, 但因为一些原因, 数据库的默认排序规则是不一样的, 例如 SELECT A.Col1, B.Col1, A.* FROM DB1.dbo.A LEFT JOIN ...
- UML: CIM & PIM
CIM-1:定义业务流程 定义及分析业务流程(Business Process)是为了尽快理清系统范围,以便估算开发成本及时间,可不是为了要改造业务流程.系统分析员千万别误解了此步骤的目的.所以,系统 ...
- websphere变成英文了怎么变回中文
今天进来发现,websphere在浏览器里面居然是英文的.这是因为我的浏览器少了一个中文语言设置,其实和页面编码无关. 解决办法: IE浏览器右键属性 -- internet选项 -- 常规 -- ...
- 关于asp.net mvc4 在IE8下 导出excel失败的解决办法
在使用FileResult向浏览器输出文件时(pdf,excel等),通常这样做: byte[] fileContents = Encoding.UTF8.GetBytes(sbHtml.ToStri ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- java核心技术记录之java术语
术语名 缩写 解释 Java Development Kit JDK 编写java程序的程序员使用的软件 Java Runtime Environment JRE 运行java程序的用户使用的软件 S ...
- POJ3414Pots
http://poj.org/problem?id=3414 题意 : 大意是说给你两个杯子的体积和一个目标体积,a,b,c,通过对a,b进行6种操作,调出c体积的水,6种操作分别是把a倒满,把b倒满 ...
- window下安装composer and yii2
我的环境是集合包xampp 1,下载composer:下载地址https://getcomposer.org/download/, 点击蓝色字体“Composer-Setup.exe” 2,安装com ...
- C程序的内存分配
一.预备知识-程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)- 由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...