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:
     *"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"]
     *]
     *这个题目一看就是HashMap,刚开始我说那String的长度来存作为key,但是发现相同长度的string可能不为同一阵营
     *所以后面该用一个新的string,就是把每两个字符之间的差存起来,写出一个string
     *["eqdf", "qcpr"]。
     *((‘q’ - ‘e‘) + 26) % 26 = 12, ((‘d’ - ‘q‘) + 26) % 26 = 13, ((‘f’ - ‘d‘) + 26) % 26 = 2
     *((‘c’ - ‘q‘) + 26) % 26 = 12, ((‘p’ - ‘c‘) + 26) % 26 = 13, ((‘r’ - ‘p‘) + 26) % 26 = 2
     *所以"eqdf"和"qcpr"是一组shifted strings。
     */
     public List<List<String>> groupStrings(String[] strings) {
            List<List<String>> result = new ArrayList<List<String>>();
            HashMap<String, List<String>> d = new HashMap<String, List<String>>();
            for(int i = 0; i < strings.length; i++) {
                StringBuffer sb = new StringBuffer();
                for(int j = 0; j < strings[i].length(); j++) {
                    sb.append(Integer.toString(((strings[i].charAt(j) - strings[i].charAt(0)) + 26) % 26));
                    sb.append(" ");
                }
                String shift = sb.toString();
                if(d.containsKey(shift)) {
                    d.get(shift).add(strings[i]);
                } else {
                    List<String> l = new ArrayList<String>();
                    l.add(strings[i]);
                    d.put(shift, l);
                }
            }
            for(String s : d.keySet()) {
                Collections.sort(d.get(s));
                result.add(d.get(s));
            }
            return result;
        }  
时间: 2024-08-04 16:42:48

249.Group Shifted Strings的相关文章

[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" -> ... -

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

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

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

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.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea