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. js作用域及闭包

    作用域 执行环境是js最为重要的一个概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为. 1.全局执行环境就是最外围的一个执行环境,每一个函数都有自己的作用域 2.简单的说局部作用 ...

  2. python select poll epoll的区别

    select 优点:为最早的异步io处理模块,他可以再linux上和windows上使用,跨平台兼容性好,而poll和epoll都不能在windows系统环境中使用. 缺点:select的机制决定了他 ...

  3. bzoj2045

    题解: 莫比乌斯反演经典题目 直接套公式好了 代码: #include<bits/stdc++.h> using namespace std; ; typedef long long ll ...

  4. iconfont.cn批量加入

    iconfont.cn还没有一个批量加入的功能 以下是最新的图标批量加入购物车功能代码. var icons = document.querySelectorAll('.icon-gouwuche1' ...

  5. 前端基础之JavaScript进阶

    一.流程控制 if - else var a = 10; if (a >5){ console.log("yes"); }else { console.log("n ...

  6. SmtpClient SSL 发送邮件异常排查

    上周使用 SmtpCliet 发送邮件测试,在服务端配置 SSL 465 / 993 情况 ,客户端使用 465 SSL 端口发送邮件异常,测试代码如下: System.Net.ServicePoin ...

  7. linux centos7 安装mono

    1.看官方的命令: 另外一种是下载压缩包解压之后安装: https://apps.fedoraproject.org/packages/mono/ 三.安装Mono需要的GDI+兼容API的库Libg ...

  8. Kubernetes资源监控探索

    搭建kubernetes集群,有一个默认的dashboard,但是这个dashboard比较简陋,不能将自定义展示.所以打算使用Grafana+Heapster+Influxdb构建一个一体化监控平台 ...

  9. Java语法基础学习DayEight

    一.异常处理 1.结构 java.lang.Object |-----java.lang.Throwable |-----java.lang.Error:错误,java程序对此无能为力,不显式处理 | ...

  10. vue-router + ElementUI实现NavMenu 导航菜单 选中状态的切换

    elemen-ui官方网站:http://element.eleme.io/#/zh-CN/component/menu 新手小白利用vue+element-ui尝试搭建后台管理系统, 效果是这样的, ...