[?]*Group Shifted Strings

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"]
]
 1  public class Solution {
 2     public List<List<String>> groupStrings(String[] strings) {
 3
 4         List<List<String>> res = new ArrayList<List<String>>();
 5         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
 6
 7         for(String word : strings){
 8             String key = "";
 9             int offset = word.charAt(0) - ‘a‘;
10             for(int i = 1; i < word.length(); i++){
11                 key += (word.charAt(i) - offset + 26) % 26;
12             }
13
14             if(!map.containsKey(key)){
15                 map.put(key, new ArrayList<String>());
16             }
17             map.get(key).add(word);
18         }
19
20         for(List<String> list : map.values()){
21             Collections.sort(list);
22             res.add(list);
23         }
24
25         return res;
26
27     }
28 }

reference: https://leetcode.com/discuss/67240/around-13-lines-code-in-java

时间: 2024-10-21 06:41:41

[?]*Group Shifted Strings的相关文章

249.Group Shifted Strings

/* *249.Group Shifted Strings *2016-6-18 by Mingyang *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: *&q

Leetcode: Group Shifted Strings

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" -> ... -> &quo

[LeetCode#249] Group Shifted Strings

Problem: 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" -> ... -

Group Shifted Strings -- LeetCode

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" -> ... -> &quo

LeetCode – Group Shifted Strings

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" -> ... -> &quo

[string]Group Anagrams

Total Accepted: 57578 Total Submissions: 226493 Difficulty: Medium Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ [&

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.