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

题目标签:Hash Table

  题目给了我们一个 strings array,让我们把不同移位距离的string 分开归类。

  首先来看一下相同移位距离string 的特性:

    相同的移位string,拥有相同的移位距离,比如abc, bcd, xyz 都是移位了1个距离。根据这个特性,我们可以把bcd 和 xyz 恢复到 abc。

  利用HashMap,把最原始的 归位string 当作key,把可以恢复到 原始的 归位string 的 所有strings(List)当作value 存入map。

  

Java Solution:

Runtime beats 44.74%

完成日期:11/04/2017

关键词:HashMap

关键点:利用 char - 'a' 把所有相同移位距离的strings 转换成 同一个原始string 存入map

 class Solution
{
public List<List<String>> groupStrings(String[] strings)
{
List<List<String>> res = new ArrayList<>(); HashMap<String, List<String>> map = new HashMap<>(); // store original string as key; (List) strings come from same original one as value
for(String str: strings)
{
int offset = str.charAt(0) - 'a';
String key = ""; for(int i=0; i<str.length(); i++)
{
char c = (char) (str.charAt(i) - offset);
if(c < 'a')
c += 26; key += c;
} if(!map.containsKey(key))
map.put(key, new ArrayList<String>()); map.get(key).add(str); } // add each key's value into res
for(String key: map.keySet())
{
res.add(map.get(key));
} return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/20722/my-concise-java-solution

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 249. 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#249] Group Shifted Strings

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

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

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

  4. 249. Group Shifted Strings

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

  5. 249. Group Shifted Strings把迁移后相同的字符串集合起来

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

  6. [Locked] Group Shifted Strings

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

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

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

  8. LeetCode – Group Shifted Strings

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

  9. Group Shifted Strings -- LeetCode

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

随机推荐

  1. Angular——内置服务

    $location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...

  2. Fiddler 修改响应内容

    1. 导入 FiddlerCore.dll 第三方库. 2. 开启侦听端口,FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Defau ...

  3. Spring Boot 创建hello world项目

    Spring Boot 创建hello world项目 1.创建项目 最近在学习Spring Boot,这里记录使用IDEA创建Spring Boot的的过程 在1出勾选,选择2,点击Next 这里填 ...

  4. js中=,==,===的区别

    =      赋值 ==    先判断类型,在判断值,可以做类型转换 ===  恒等判断

  5. MyBatis 中 resultMap 详解

    resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表为(用户角色表),通过查询用户表信息展示页面, ...

  6. 高德地图将字符串地址转为经纬度的一个demo

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. Python异常捕捉的一个小问题

    问题: names = ['taotao','songwenjing','liu','li']I = iter(names)while True: try: s = next(I) except Ex ...

  8. [已解决]运行gunicorn失败:[ERROR] Connection in use 127.0.0.1 8080

    最近重新部署了一下应用程序,之后重新运行gunicorn,使用如下命令: gunicorn -b 0.0.0.0:8000 manage:app --reload 之后出现了一堆错误,具体错误内容如下 ...

  9. Codeforces Round #219 (Div. 2) D题

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  10. [K/3Cloud]调用动态表单时,传递自定义参数

    插件中在调用动态表单时,通过DynamicFormShowParameter的CustomParams,增加自定义的参数. private void ShowMaterialStock() { obj ...