码迷,mamicode.com
首页 > 其他好文 > 详细

249. Group Shifted Strings

时间:2015-12-01 14:35:18      阅读:599      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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.

链接: http://leetcode.com/problems/group-shifted-strings/

题解:

一道简单题,我又写得巨长...水平太有限了,唉。主要是数据结构选了一个Map<Integer, Map<String, List<String>>>,先以String长度为key,再以单词为key,List<String>为value。 其实可以不用这么麻烦, 就用Map<String,List<String>>就可以了。遍历keySet()的时候,只要长度不一样,continue就行了。时间复杂度还是一样的。 假设单词长度有限的情况下,还是O(n2)。 二刷再简化..........这句话说了几百遍。二刷再简化吧!

Time Complexity - O(n2), Space Complexity - O(n)。

public class Solution {
    public List<List<String>> groupStrings(String[] strings) {
        List<List<String>> res = new ArrayList<>();
        if(strings == null || strings.length == 0)
            return res;
        Arrays.sort(strings);
            
        Map<Integer, Map<String, List<String>>> map = new HashMap<>();
        
        for(int i = 0; i < strings.length; i++) {
            String s = strings[i];
            int len = s.length();
            if(!map.containsKey(len)) {
                Map<String, List<String>> tmpMap = new HashMap<>();
                tmpMap.put(s, new ArrayList<>(Arrays.asList(s)));
                map.put(len, tmpMap);
            } else {
                Map<String, List<String>> tmpMap = map.get(len);
                boolean hasSequence = false;
                
                outerloop:
                for(String t : tmpMap.keySet()) {
                    for(int k = 1; k < len; k++) {
                        int curDistance = (int)(s.charAt(k) - t.charAt(k));
                        int lastDistance = (int)(s.charAt(k - 1) - t.charAt(k - 1));
                        curDistance = curDistance >= 0 ? curDistance : curDistance + 26;
                        lastDistance = lastDistance >= 0 ? lastDistance : lastDistance + 26;
                        if(curDistance != lastDistance)
                            continue outerloop;
                    }
                    tmpMap.get(t).add(s);
                    hasSequence = true;
                    break;
                }
                
                if(!hasSequence) {
                    tmpMap.put(s, new ArrayList<>(Arrays.asList(s)));
                }
            }
        }
        
        for(int i : map.keySet()) {
            Map<String, List<String>> tmpMap = map.get(i);
            for(String s : tmpMap.keySet()) {
                res.add(map.get(i).get(s));
            }
        }
            
        return res;
    }
}

 

Reference:

https://leetcode.com/discuss/69783/concise-10-lines-java-solution-with-explanation

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

https://leetcode.com/discuss/64979/simple-solution-in-java-with-detailed-explaination

https://leetcode.com/discuss/64751/cannot-pass-tests-seems-nothing-wrong-for-the-custom-testcase

https://leetcode.com/discuss/58003/java-solution-with-separate-shiftstr-function

https://leetcode.com/discuss/53166/4ms-c-solution

https://leetcode.com/discuss/52627/python-easy-to-understand-solution-with-comments

https://leetcode.com/discuss/50582/4ms-c-solution

https://leetcode.com/discuss/50557/4ms-easy-c-solution-with-explanations

https://leetcode.com/discuss/50416/1-4-lines-in-java

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

https://leetcode.com/discuss/50163/1-4-lines-ruby-and-python

249. Group Shifted Strings

标签:

原文地址:http://www.cnblogs.com/yrbbest/p/5009004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!