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"],
A solution is:

[
  ["abc","bcd","xyz"],
  ["az","ba"],
  ["acef"],
  ["a","z"]
]

Group Anagrams类似. 维护一个HashMap, key是每个string 的 base型.

Time Complexity: O(n * strings.length), n 是每个string的平均长度.

Space: O(hm.size()), HashMap size.

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        String[] strs = {"abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"};
        List<List<String>> list = groupStrings(strs);
        for(int i=0; i<list.size(); i++){
            System.out.println("List "+(i+1));
            List<String> subList = list.get(i);
            for(int j=0; j<subList.size(); j++){
                System.out.println(subList.get(j));
            }
        }

    }

    public static List<List<String>> groupStrings(String[] strings) {
        if(strings == null || strings.length == 0){
            return new ArrayList<List<String>>();
        }

        Map<String, List<String>> map = new HashMap<>();
        for(int i=0; i<strings.length; i++){
            String str = getBase(strings[i]);
            if(!map.containsKey(str)){
                map.put(str, new ArrayList<String>());
            }
            map.get(str).add(strings[i]);

        }
        return new ArrayList<List<String>>(map.values());

    }

    public static String getBase(String str){
        if(str == null || str.length()==0){
            return str;
        }
        StringBuilder sb =  new StringBuilder();
        int offset = str.charAt(0) - ‘a‘;
        for(int i=0; i<str.length(); i++){
            char c = (char) (str.charAt(i)-offset);
            if(c < ‘a‘){
                c += 26;
            }
            sb.append(c);

        }
        return sb.toString();
    }
}

  

  

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/9352106.html

时间: 2024-07-28 20:37:39

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