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" -> ... -> "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 all strings that belong to the same shifting sequence, 所以找到属于相同移位序列的key 很重要。因此单独写了函数shift,

写这个shift函数,我之前想的很复杂,每个String要移26个距离,看shifted string是不是一个key。其实干嘛这么复杂,指定一个相同移位序列的key,定义为第一个char为‘a’

buffer.append((c - ‘a‘ - dist + 26) % 26 + ‘a‘) 极容易错。将相同移位序列的Strings存入key 对应的value中,建立正确的HashMap很重要。

 1 public class Solution {
 2     public List<List<String>> groupStrings(String[] strings) {
 3         List<List<String>> res = new ArrayList<List<String>>();
 4         if (strings==null || strings.length==0) return res;
 5         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
 6         for (int i=0; i<strings.length; i++) {
 7             String temp = shift(strings[i]);
 8             if (map.containsKey(temp)) {
 9                 map.get(temp).add(strings[i]);
10             }
11
12             else {
13                 List<String> li= new ArrayList<String>();
14                 li.add(strings[i]);
15                 map.put(temp, li);
16             }
17
18         }
19         for (List<String> each : map.values()) {
20             Collections.sort(each);
21             res.add(new ArrayList<String>(each));
22         }
23         return res;
24     }
25
26     public String shift(String cur) {
27         StringBuffer res = new StringBuffer();
28         int len = cur.length();
29         int dist = cur.charAt(0) - ‘a‘;
30         for (int k=0; k<len; k++) {
31             //res.append((cur.charAt(k)+j>‘z‘)? (‘a‘+(int)(cur.charAt(k)+j-‘z‘)-1) : (cur.charAt(k)+j));
32             char c = cur.charAt(k);
33             res.append((c-‘a‘-dist+26)%26+‘a‘);
34         }
35         return res.toString();
36     }
37 }
时间: 2024-10-11 12:13:12

Leetcode: Group Shifted Strings的相关文章

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

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#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

[?]*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:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurre

LeetCode 205 Isomorphic Strings

Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of cha

LeetCode (19) Multiply Strings

题目描述 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 对两组非负数字进行相乘,使用数组表示数字,且题目中说明数组很大,因此,因此不能直接将数组转换成数字相乘.这道题目是要求自己构造乘法的思路,需要注意的地方主要为进位的处理. 解题

Java [Leetcode 43]Multiply Strings

题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 解题思路: 设置数组记录单个位置相乘的结果,最后负责相加进位. 代码如下: public class Solution { public String multiply(St