LeetCode Brushing Notes _49. Grouping of Letter Alien Words

The topic is from LeetCode

49. Grouping of Letter Alien Words

Other solutions or source code can be accessed: tongji4m3

description

Given an array of strings, combine the letter dyslexia together. Aliphatic words refer to strings with the same letters but different arrangements.

Example:

输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
输出:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Description:

All entries are in lowercase letters.
The order in which the answers are output is not considered.

Ideas

Use a Map to store the index and the corresponding group of the index. The index is a unique identifier calculated from the letter dysphorism. In this way, traverse the strs array and put strings with the same index in the same group.

Regarding the calculation of the index, each letter can be bound to a prime number. For a string, multiply the prime numbers of each letter contained, and the sum is the index

The second way of writing is similar to the previous one: map stores lexicographical strings and corresponding groups. Convert the string to a lexicographic string each time through the loop, and then judge

detail

The map directly stores the index used for zone grouping, and the List group can be used to save memory

Code

//生成素数
private List<Integer> prime()
{
    
    
    int N = 26;
    List<Integer> result = new LinkedList<>();

    for (int i = 2; i < 1000; i++)
    {
    
    
        int j;
        for (j = i - 1; j > 1; j--)
        {
    
    
            if (i % j == 0) break;
        }
        if(j==1) result.add(i);
        if(result.size()==N) break;
    }
    return result;
}
//素数可以提前生成好
public List<List<String>> groupAnagrams(String[] strs)
{
    
    
    Map<Integer, List<String>> map = new HashMap<>();
    //List<Integer> prime = prime();
    //预先准备好
    int[] prime = {
    
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
    for (String string : strs)
    {
    
    
        int index = 1;
        for (char ch : string.toCharArray())
        {
    
    
            //                index*=prime.get(ch - 'a');
            index*=prime[ch-'a'];
        }
        if(!map.containsKey(index)) map.put(index, new LinkedList<>());
        map.get(index).add(string);
    }
    return new LinkedList<>(map.values());
}
//第二种思路
//map存储字典序字符串和对应的组。每次循环都把字符串转为字典序字符串,再判断
public List<List<String>> groupAnagrams1(String[] strs)
{
    
    
    Map<String, List<String>> map = new HashMap<>();
    for (int i = 0; i < strs.length; i++)
    {
    
    
        //map中只存储字典序字符串
        char[] chars = strs[i].toCharArray();
        Arrays.sort(chars);
        String string = String.valueOf(chars);
        //第一次放入某类型的字母异位词
        if (!map.containsKey(string)) map.put(string, new LinkedList<>());

        map.get(string).add(strs[i]);
    }
    return new LinkedList<>(map.values());
}

Complexity analysis

time complexity

The first method: O (NK) O (NK)O ( N K ) N is the length of the array, and K is the maximum length of the string. The outer loop needs to traverse the string array once, and the inner needs to calculate the index of each string.

The second method: O (NK log ⁡ K) O(NK \log K)O ( N KlogK ) N is the length of the array, and K is the maximum length of the string. The outer loop needs to traverse the string array once. Each string needs to be sorted internally

Space complexity

O (NK) O (NK) O ( N K ) Stored in the values ​​of the Map requires a total of NK space

Guess you like

Origin blog.csdn.net/weixin_42249196/article/details/108212837