Group Shifted Strings

时间:2023-01-03 14:27:43

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

[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]

Note: For the return value, each inner list's elements must follow the lexicographic order.

题解:

既然是group,那么我们需要有一个规则,把符合规则的放在一起。那么,规则是什么呢?

根据题目的意思,对于两串字符串,如果字符串的相邻字符的差值是一致的,那么我们就可以把它们放在一起。

但是对于az, ba这样的,字符之间的差值不一样啊。但是为何它们是一组的呢?

az字符之间的差值是25,ba之间字符的差值是-1,但是字符是每隔26就一循环,所以,对于-1,你一旦加上26就是25.

 public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> result = new ArrayList<List<String>>();
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = ; i < strings.length; i++) {
StringBuffer sb = new StringBuffer();
for (int j = ; j < strings[i].length(); j++) {
sb.append(Integer.toString(((strings[i].charAt(j) - strings[i].charAt()) + ) % ));
sb.append(" ");
}
String shift = sb.toString(); if (map.containsKey(shift)) {
map.get(shift).add(strings[i]);
} else {
List<String> list = new ArrayList<String>();
list.add(strings[i]);
map.put(shift, list);
}
} for (String s : map.keySet()) {
Collections.sort(map.get(s));
result.add(map.get(s));
}
return result;
}
}

Group Shifted Strings的更多相关文章

  1. &lbrack;Locked&rsqb; Group Shifted Strings

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

  2. &lbrack;LeetCode&num;249&rsqb; Group Shifted Strings

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

  3. &lbrack;LeetCode&rsqb; Group Shifted Strings 群组偏移字符串

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

  4. 249&period; Group Shifted Strings

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

  5. &lbrack;Swift&rsqb;LeetCode249&period;群组偏移字符串 &dollar; Group Shifted Strings

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

  6. 249&period; Group Shifted Strings把迁移后相同的字符串集合起来

    [抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...

  7. LeetCode – Group Shifted Strings

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

  8. Group Shifted Strings -- LeetCode

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

  9. LeetCode 249&period; Group Shifted Strings (群组移位字符串)&dollar;

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

随机推荐

  1. java-并发-高级并发对象2

    浏览以下内容前,请点击并阅读 声明 并发集合 java.util.concurrent包包含了许多对于java集合框架的补充,根据提供的接口可以将这些集合分为以下几类: BlockingQueue 定 ...

  2. IOS开发之--异常处理--使用try 和 catch 来捕获错误。

    一个搞java的老板问我会不会try catch  我说不会 学这么久也没听周围朋友用这个 因为苹果控制台本来就可以打印异常 特此研究一下. 1.try catch:  是捕获异常代码段   特点:对 ...

  3. react 组件的生命周期

    组件的生命周期 过程 装载(Mounting) :组件被插入到 DOM 中: 更新(Updating) :组件重新渲染以更新 DOM: 卸载(Unmounting) :组件从 DOM 中移除. 过程 ...

  4. 解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG

    解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-6 ...

  5. 第四章:Django 模型 —— 设计系统表

    1. Django框架提供了完善的模型(Model )层来创建和存储数据,每一个模型对应数据库中的唯一的一张表. 2. Django 模型基础知识: .每一本模型是一个Python类,继承了djang ...

  6. Python机器学习笔记:XgBoost算法

    前言 1,Xgboost简介 Xgboost是Boosting算法的其中一种,Boosting算法的思想是将许多弱分类器集成在一起,形成一个强分类器.因为Xgboost是一种提升树模型,所以它是将许多 ...

  7. Elastic Stack之ElasticSearch分布式集群yum方式搭建

    Elastic Stack之ElasticSearch分布式集群yum方式搭建 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.搜索引擎及Lucene基本概念 1>.什么 ...

  8. django学习之——创建项目

    创建项目让我迷茫了会: 直接cmd 执行django-admin.py startproject  pro_name 肯定是不行的 必须cd到 D:\Program Files\Python3.3.5 ...

  9. 每天一个linux命令&lpar;4&rpar; df命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  10. 转载&comma;自己留着看eclipse 快捷键

    Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...