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. System.Data.Entity 无法引用的问题

    最近刚学MVC,跟着网上的博客学习,发现代码中有这样一句: using System.Data; using System.Data.Entity; 我项目引用的时候,也引用了System.Data. ...

  2. animation of android (4)

    TimeAnimator: 与objectAminator不同,它反馈的时间间隔.也就是说TimeAnimator不产生实际的动画效果,他反馈的时间间隔和时间值. 而你并不关心 interpolate ...

  3. JMeter源码集成到Eclipse

    由于JMeter纯Java开发,界面也是基于Swing或AWT搞出来的,所以想更深层次的去了解这款工具或对于想了解JMeter插件开发或二次开发的童鞋们来说,读读JMeter的源码估计是必不可少的,所 ...

  4. OOD原则汇总

    头五项原则是关于类设计的,它们是: ◆ SRP,单一职责原则,一个类应该有且只有一个改变的理由. ◆ OCP,开放封闭原则,你应该能够不用修改原有类就能扩展一个类的行为. ◆ LSP,Liskov替换 ...

  5. 避免jQuery名字冲突--noConflict()方法

    众所周知,在jQuery语法中,$符号是jQuery的简写方式.但在某些情况下,可能需要在同一个页面引入其他javascript库(比如Prototype).因为$简短方便,很多的库也是使用$符号.为 ...

  6. Python Memcached Script

    介绍 利用 python 书写了 memcached 的启动等一类操作 尽量的实现脚本的复用性,以及脚本的可扩展性,已达到一劳永逸的效果, 并且添加了 memcached 监控搭建 memcached ...

  7. 系统调用wait、waitpid和exec函数

    本文介绍了Linux下的进程的一些概念,并着重讲解了与Linux进程管理相关的重要系统调用wait,waitpid和exec函数族,辅助一些例程说明了它们的特点和使用方法. 1.7 背景 在前面的文章 ...

  8. javascript之url转义escape()、encodeURI()和encodeURIComponent()

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  9. SSI

    一.简介 SSI(Server Side Includes)技术,是为WEB服务器提供的一套命令,在HTML文档中通过注释行调用的命令或指针,就可以将文本.图形或应用程序信息包含到网页中. SSI具备 ...

  10. hdu 1542 Atlantis(线段树,扫描线)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...