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的更多相关文章

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

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

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

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

  3. 249. Group Shifted Strings

    题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...

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

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

  5. [Locked] Group Shifted Strings

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

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

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

  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. [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings

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

随机推荐

  1. JDBC连接数据库代码

    //连接是需要导包 http://pan.baidu.com/s/1o6nyuOa /*配合数据库建立表 create database day14 character set utf8 collat ...

  2. C# Double toString保留小数点方法

    有时候double型数据需要toString(),但又想保留小数,当值为整数,比如3.00时tostring后会变为”3″,具体说明见下: 1 string str0 = i.ToString(&qu ...

  3. Careercup - Facebook面试题 - 5761467236220928

    2014-05-02 07:06 题目链接 原题: Given an array of randomly sorted integers and an integer k, write a funct ...

  4. 【HDOJ】【3709】Balanced Bumber

    数位DP 题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html dfs的地方没太看懂……(也就那里是重点吧喂!)挖个 ...

  5. C#中两个日期类型相减得到天数

    protected int GetDuration(DateTime start, DateTime finish) { return (finish - start).Days; } 直接相减得到的 ...

  6. 如何将jsp中<input>设为只读

    将一个input变为只读,可以使用 readonly 属性 和 disabled 属性.  用disabled 属性时,文字显示为灰色.  下面的两种方法都是可以的: <input id =&q ...

  7. Introduction To Monte Carlo Methods

    Introduction To Monte Carlo Methods I’m going to keep this tutorial light on math, because the goal ...

  8. hdu 1166 树状数组 线段树入门

    点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  9. uva 434

    贪心 ~ #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #i ...

  10. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...