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"],
A solution is:

[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]
 
Solution:
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> resLists = new LinkedList<List<String>>(); HashMap<String,List<String>> patternMap = new HashMap<String,List<String>>();
for (String str : strings){
StringBuilder builder = new StringBuilder().append(str);
int delta = builder.charAt(0)-'a';
builder.setCharAt(0,'a');
for (int i=1;i<builder.length();i++){
char c = (char) ((builder.charAt(i) + 26 - delta)%26);
builder.setCharAt(i,c);
}
String pattern = builder.toString();
if (!patternMap.containsKey(pattern)){
patternMap.put(pattern,new LinkedList<String>());
}
patternMap.get(pattern).add(str);
} for (List<String> strList : patternMap.values()){
resLists.add(strList);
}
return resLists;
}
}

LeetCode-Group Shifted Strings的更多相关文章

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

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

  2. LeetCode – Group Shifted Strings

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

  3. [Locked] Group Shifted Strings

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

  4. [LeetCode#249] Group Shifted Strings

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

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

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

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

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

  7. Group Shifted Strings -- LeetCode

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

  8. 249. Group Shifted Strings

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

  9. [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings

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

  10. Group Shifted Strings

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

随机推荐

  1. Sonar代码质量管理工具

    最近上线了,Sonar代码扫描工具: 与jenkins集成: 实现自动扫描: 下面来简单聊聊Sonar能解决什么问题: ---------------------- Sonar简介 Sonar是一个用 ...

  2. C#中Dictionary,Hashtable,List的比较及分析

    一. Dictionary与Hashtable Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值 二者的特性大体上是相同的,有时可以把Dicti ...

  3. Hadoop Eclipse开发环境搭建

        This document is from my evernote, when I was still at baidu, I have a complete hadoop developme ...

  4. 软件测试作业3--Junit、hamcrest、eclemmat的安装和使用

    1.   how to install junit, hamcrest and eclemma? 首先下载下来Junit和Hamcrest的jar包,然后新建项目的时候将这两个jar包导入到工程里面就 ...

  5. C++基础——子类转父类转子类 (派生类转基类转派生类)

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  6. 读书笔记——Windows环境下32位汇编语言程序设计(5)模态对话框

    资源可以用VC之类的生成,然后拷贝出来. 例如:每一个MFC工程都有一个resource.h,没有做任何修改时,这个resource.h文件是原来自带的.当对资源进行过修改添加之类的时,新添加的资源的 ...

  7. 【DPDK】虚拟机开发环境配置

    DPDK介绍见:www.dpdk.org 本文介绍的步骤基本适用于dpdk 1.7.0 - dpdk 2.0.0 各版本.只是setup.sh显示的菜单有一些小的不同:同样的,也适用于ubuntu更高 ...

  8. HADOOP namenode HA

    参考的文章:http://www.cnblogs.com/smartloli/p/4298430.html 当然,在操作的过程中,发现与上述文章中描述的还是有一些小小的区别. 配置好后,start-d ...

  9. Mathout 安装部署

    安装Mahout,并运行测试样例,抓图测试实验过程 证明已部署成功 Mahout 下载地址:http://apache.dataguru.cn/mahout/0.9/mahout-distributi ...

  10. uva 1471 defence lines——yhx

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...