java 249. Group Shifted Strings.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 249. Group Shifted Strings.java相关的知识,希望对你有一定的参考价值。

public class Solution {
    private String getSequence(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < str.length(); i++) {
            int temp = str.charAt(i) - str.charAt(i - 1);
            if (temp > 0) {
                temp -= 26;
            }
            sb.append(temp);
        }
        return sb.toString();
    }
    public List<List<String>> groupStrings(String[] strings) {
        HashMap<String, List<String>> map = new HashMap<String,List<String>>();
        List<List<String>> res = new ArrayList<List<String>>();
        for (String str : strings) {
            String key = getSequence(str);
            if(!map.containsKey(key)) {
                map.put(key, new ArrayList<String>());
            }
            map.get(key).add(str);
        }
        for(String key : map.keySet()) {
            res.add(map.get(key));
        }
        return res;
    }
}

/*
["abc","bcd","acef","xyz","az","ba","a","z"]
["az","ba","bz","ac","ca","cz","ad","zc"]
[]
*/

以上是关于java 249. Group Shifted Strings.java的主要内容,如果未能解决你的问题,请参考以下文章

java 249. Group Shifted Strings.java

java 249. Group Shifted Strings.java

java 249. Group Shifted Strings.java

java 249. Group Shifted Strings.java

249.Group Shifted Strings

249. Group Shifted Strings