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. 计算机基础part1

    一:计算机的基本组成 1.计算机由输入单元.控制单元.算法逻辑单元.输出单元.存储单元,五大单元组成 二:概念篇 CPU:中央处理器,其内含有指令集(取码-解码-执行的过程) CPU同一时刻只能干一件 ...

  2. [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参

    [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...

  3. maven项目配置findbugs插件 使用git钩子控制代码的提交

    maven项目配置findbugs插件对代码进行静态检测 当发现代码有bug时,就不让用户commit代码到远程仓库里 没有bug时才可以commit到远程仓库中 (1)新建maven项目 ,配置fi ...

  4. 程序从sqlserver2008搬家到MySQL5.6

    1.数据表结构的搬家 SqlServer的建表sql语句在MySQL中肯定不能运行 这里使用转换工具 下载地址: https://download.csdn.net/download/zhutouai ...

  5. Cracking The Coding Interview 4.0_二叉树

    #include <iostream> #include <string> using namespace std; class tree { public: tree() { ...

  6. Linux系统分区方案(CentOs 6)

    装Linux如何分区: 方案1:(监控服务器,负载均衡器) 1./boot 引导分区,存放引导文件和Linux内核.       启动文件:用于判断你需要启动哪个操作系统或启动哪个内核.        ...

  7. Linux文件系统命令 pwd

    命令名:pwd 功能:查看当前所处的位置 eg: renjg@renjg-HP-Compaq-Pro--MT:~$ pwd /home/renjg renjg@renjg-HP-Compaq-Pro- ...

  8. 手机号的 DES-ECB 加密/解密

    前言:公司的手机号加密更换了加密方法,这次改成 DES-ECB 加密了 代码操作 # -*- coding:utf-8 -*- import base64 import json from Crypt ...

  9. 12.输入一个成绩计算其A,B,C,D,E等级

    #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int score; s ...

  10. SSH防止超时的设置

    针对SSH命令工具超时的解决方法: 1.在命令行输入这两行代码,即可完成 echo export TMOUT=1000000 >> /root/.bash_profile cat /roo ...