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"]
]

Group Anagrams类似. 维护一个HashMap, key是每个string 的 base型.

Time Complexity: O(n * strings.length), n 是每个string的平均长度.

Space: O(hm.size()), HashMap size.

// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
String[] strs = {"abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"};
List<List<String>> list = groupStrings(strs);
for(int i=0; i<list.size(); i++){
System.out.println("List "+(i+1));
List<String> subList = list.get(i);
for(int j=0; j<subList.size(); j++){
System.out.println(subList.get(j));
}
} } public static List<List<String>> groupStrings(String[] strings) {
if(strings == null || strings.length == 0){
return new ArrayList<List<String>>();
} Map<String, List<String>> map = new HashMap<>();
for(int i=0; i<strings.length; i++){
String str = getBase(strings[i]);
if(!map.containsKey(str)){
map.put(str, new ArrayList<String>());
}
map.get(str).add(strings[i]); }
return new ArrayList<List<String>>(map.values()); } public static String getBase(String str){
if(str == null || str.length()==0){
return str;
}
StringBuilder sb = new StringBuilder();
int offset = str.charAt(0) - 'a';
for(int i=0; i<str.length(); i++){
char c = (char) (str.charAt(i)-offset);
if(c < 'a'){
c += 26;
}
sb.append(c); }
return sb.toString();
}
}

  

  

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. [Locked] Group Shifted Strings

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

  3. [LeetCode#249] Group Shifted Strings

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

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

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

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

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

  6. Group Shifted Strings -- LeetCode

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

  7. 249. Group Shifted Strings

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

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

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

  9. Group Shifted Strings

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

随机推荐

  1. Resharper插件安装和破解

    1.首先在最下面的地址,下载Resharper安装包,进行解压安装,安装界面如下: a 2.安装后 解压下载好的 文件 会得到如下: 3.打开序列号 会看到如下所示: 4.然后  复制 %LocalA ...

  2. Java判断对象是否为NULL

    Java使用反射判断对象是否为NULL 判断Java对象是否为null可以有两层含义: 第一层:  直接使用 object == null 去判断,对象为null的时候返回true,不为null的时候 ...

  3. 每天CSS学习之border-radius

    css3的border-radius属性,我们用之来画圆角边框. 1.border-radius:none;//表示不用圆角边框,边框会变成方形. 2.border-radius:水平方向{1,4}[ ...

  4. Cracking The Coding Interview 4.1

    //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...

  5. python获取代理IP

    利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf- -*- import requests from bs4 import Beautiful ...

  6. go语言基础学习

    go基础学习,面向对象-方法在Go语言中,可以给任意自定义类型(包括内置类型,但不包括指针类型)添加相应的方法 使用= 和:=的区别: // = 使用必须使用先var声明例如: var a a=100 ...

  7. day 57 data 插件 表的增删改查

    一 data的含义 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值.      1 .data(key, value):     描述:在匹配 ...

  8. Python 的基本运算及分析

    1题   输出1 2 3 4 5 6  8 9 10 . 方法一: count = 0while count < 10 : count += 1 if count == 7 : continue ...

  9. Android:AndroidManifest.xm中xmlns的作用

    有了它,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上 ...

  10. Oracle使用exp和imp导出、导入数据

    ===========导出============ exp 用户名/密码@服务器(localhost) file=文件路径.dmp owner=(用户名) ===========导入========= ...