Leetcode 49. Group Anagrams [medium][java]

Given an array of strings, group anagrams together.

Example
在这里插入图片描述
NOTE

  1. Knowledge about map has been forgotten, should be reviewed.

Solution 1
Consideration

  1. use a map to store the patterns
  2. for each string, sort it first, then check if it is in the map. If yes, append to the list. Otherwise, put the new key to the map and append the string to the list.

Time Complexity: O(NKlogK), where N is the length of strs, and K is the maximum length of a string in strs. The outer loop has complexity O(N) as we iterate through each string. Then, we sort each string in O(KlogK) time.

Space Complexity: O(NK), the total information content stored in ans.

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if(strs == null || strs.length == 0)
            return new ArrayList();
        Map<String,List> map = new HashMap();
        for(String s:strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String new_str = String.valueOf(chars);
            if(!map.containsKey(new_str)) {
                map.put(new_str, new ArrayList());
            }
            map.get(new_str).add(s);
        }
        return new ArrayList(map.values());
    }
}

Solution 2
Consideration

  1. Use a array to store the count of each characters
  2. Use a map to store the count pattern
  3. Convert the count to string, and check if the map contains the pattern key.

Time Complexity: O(NK), where N is the length of strs, and K is the maximum length of a string in strs.
Space Complexity: O(NK)

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if(strs == null || strs.length == 0)
            return new ArrayList();
        Map<String,List> map = new HashMap();
        int[] count = new int[26];
        for(String s:strs) {
            Arrays.fill(count, 0);
            char[] chars = s.toCharArray();
            for(char c: chars)
                ++count[c-'a'];
            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < 26; i++) {
                sb.append('#');
                sb.append(count[i]);
            }
            String new_str = sb.toString();
            if(!map.containsKey(new_str)) {
                map.put(new_str, new ArrayList());
            }
            map.get(new_str).add(s);
        }
        return new ArrayList(map.values());
    }
}

Solution 3
Consideration

  1. Use 26 primes to represent 26 alphabets. As for primes, different combinations the multiplications will be different.
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if(strs == null || strs.length == 0)
            return new ArrayList();
        int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
        Map<Integer, List<String>> map = new HashMap<>();
        for(String str:strs) {
            int k = 1;
            for(char ch:str.toCharArray()) {
                k *= primes[ch-'a'];
            }
            if(!map.containsKey(k)) {
                map.put(k, new ArrayList<>());
            }
            map.get(k).add(str);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

References

  1. https://leetcode.com/problems/group-anagrams/solution/
  2. https://blog.csdn.net/zgljl2012/article/details/71635126
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值