leetcode.1002. Find common characters

leetcode.1002. Find common characters

Given a string array A consisting of only lowercase letters, return a list of all characters (including repeated characters) displayed in each string in the list. For example, if a character appears 3 times in each string, but not 4 times, you need to include the character 3 times in the final answer.

You can return the answers in any order.

Example 1:

输入:["bella","label","roller"]
输出:["e","l","l"]

Example 2:

输入:["cool","lock","cook"]
输出:["c","o"]

prompt:

1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j] 是小写字母

Code 1

This question can be understood as finding the intersection of the number of characters between each string. Take Example 1 as an example:

输入:["bella","label","roller"]
输出:["e","l","l"]

We know that the list of the number of characters in the first string is:

b 1
e 1
l 2
a 1

The list of the number of characters in the second string is:

l 2
a 1
b 1
e 1

The list of the number of characters in the third string is:

r 2
o 1
l 2
e 1

The result of these three intersections is:

e 1
l 2

The results are clear at a glance.

In fact, there is nothing to say at this point of analysis. We can use hashmap to represent the relationship between characters and numbers . However, considering efficiency, we can use arrays to optimize the code. Use the subscript i of the array res to indicate which character it is, and use res[i] to indicate the number of times the character appears. code show as below:

public List<String> commonChars(String[] A) {
    
    
    List<String> list = new ArrayList<>();
    int[] res = new int[26];
    //统计第一个字符串,字符与数量的关系
    for (char c : A[0].toCharArray()) {
    
    
        res[c - 'a']++;
    }
    for (int i = 1; i < A.length; i++) {
    
    
        int[] temp = new int[26];
        //统计第 i 个字符串,字符与数量的关系
        for (char c : A[i].toCharArray()) {
    
    
            temp[c - 'a']++;
        }
        //比较第最小出现次数,结果放入res保存。
        for (int j = 0; j < 26; j++) {
    
    
            res[j] = Math.min(res[j], temp[j]);
        }
    }
    for (int i = 0; i < res.length; i++) {
    
    
        if (res[i] > 0) {
    
    
            for (int j = 0; j < res[i]; j++) {
    
    
                list.add(((char) ('a' + i) + ""));
            }
        }
    }
    return list;
}

Transfer from-1002. Find common characters

Code 2

It is also not necessary to count the first character, the character-quantity relationship, we construct an array of length 26, each element in it is the maximum value of an int type integer, and then compare this array with the string. Figure.
Insert picture description here
After reading this picture, you may find it strange that it aappears here , but the result is not printed. That's because there are times in rollerthis string .a0

class Solution {
    
    
    public List<String> commonChars(String[] A) {
    
    
        int[] minfreq = new int[26];
        Arrays.fill(minfreq, Integer.MAX_VALUE);
        for (String word: A) {
    
    
            int[] freq = new int[26];
            int length = word.length();
            for (int i = 0; i < length; ++i) {
    
    
                char ch = word.charAt(i);
                ++freq[ch - 'a'];
            }
            for (int i = 0; i < 26; ++i) {
    
    
                minfreq[i] = Math.min(minfreq[i], freq[i]);
            }
        }

        List<String> ans = new ArrayList<String>();
        for (int i = 0; i < 26; ++i) {
    
    
            for (int j = 0; j < minfreq[i]; ++j) {
    
    
                ans.add(String.valueOf((char) (i + 'a')));
            }
        }
        return ans;
    }
}

official

Guess you like

Origin blog.csdn.net/e891377/article/details/109068229