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. ASP.NET Web - 服务器控件

    控件 HTML 说明 Label <span> 返回一个包含文本的span元素 Literal static text 返回简单的静态文本.使用Literal控件,可以根据客户应用程序转换 ...

  2. SQL Server 数据库最小宕机迁移方案

    一.目的 在做SQL Server数据库维护的时候,当上司要求我们把几十G的数据文件搬动到其它服务器,并且要求最小宕机时间的时候,我们有没什么方案可以做到这些要求呢? 在这里我们假设这两台机器并不是在 ...

  3. JAVA SSH 框架介绍

    SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...

  4. Android 自定义Toast,不使用系统Toast

    效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...

  5. C# Winform 拖放操作

    http://www.cnblogs.com/imlions/p/3189773.html 在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winfor ...

  6. mac mysql安装

    一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...

  7. 【转载】VMWare ESXi 5.0和vSphere Client安装和配置

      免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:张洪洋_     原文地址:http://blog.sina.com.cn ...

  8. .NET基础篇——Entity Framework 数据转换层通用类

    在实现基础的三层开发的时候,大家时常会在数据层对每个实体进行CRUD的操作,其中存在相当多的重复代码.为了减少重复代码的出现,通常都会定义一个共用类,实现相似的操作,下面为大家介绍一下Entity F ...

  9. 8个月从CS菜鸟到拿到Google Offer的经历+内推

    http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=77453&page=1&authorid=10377 ...

  10. 在实体注解OneToMany时,要加上mappedby,避免产生中间表。

    在实体注解OneToMany时,要加上mappedby,避免产生中间表.