LeetCode1002.在字符串-Java之间找到常见字符



问题是:给定一个仅由小写字母制成的数组a的字符串,返回列表中所有字符串中显示的所有字符的列表(包括重复)。例如,如果一个字符在所有字符串中出现3次,但不是4次,则需要在最终答案中包含3次。

您可以以任何顺序返回答案。

示例1:

输入:["贝拉","标签","滚子"]输出:[" E"," L"," L"]

示例2:

输入:["酷","锁"," cook"]输出:[" C"," O"]

注意:

1< = a.length< = 100

1< = a [i] .length< = 100

a [i] [j]是一个小写字母

这是我的代码:

class Solution {
    public List<String> commonChars(String[] A) {
        List<String> charArr = new ArrayList<>();
        for(int i = 1; i<A.length ; i++){
            A[0].replaceAll("[^" + A[i] + "]", "");
        }
        for(int i=0; i<A[0].length(); i++){
            charArr.add(Character.toString(A[0].charAt(i)));
        }
        return charArr;
    }
}

结果我得到了

输入:["贝拉","标签","滚筒"]

输出:[" B"," E"," L"," L"," A"]

期望:[" E"," L"," L"]

显然这些角色没有被删除,有人可以帮助我解决这个问题吗?

不确定java,但这是python中的1班轮

list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
>>> from collections import Counter
>>> import functools
>>> A = ["bella","label","roller"]
>>> list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
['e', 'l', 'l']
>>> A =  ["cool","lock","cook"]
>>> list(functools.reduce(lambda a,b: a&b, [Counter(c) for c in A]).elements())
['c', 'o']
>>>

我用Manaar的想法解决了问题,谢谢大家的建议和评论!

**class Solution {
    public List<String> commonChars(String[] A) {
        List<String> charArr = new ArrayList<>();
        int[][] lowerCase = new int[26][A.length];
        for(int i = 0 ; i< A.length ; i++){
            for(int j =0; j<A[i].length();j++)
            lowerCase[A[i].charAt(j)-'a'][i] ++;            
        }
        for(int i =0; i < 26 ;i++){
            int min = lowerCase[i][0];
            for(int j = 0 ; j< A.length ; j++){
                if(lowerCase[i][j]<min)
                    min = lowerCase[i][j];
            }
            while(min!=0){
                charArr.add(Character.toString((char)(i+'a')));
                min--;
            }
        }
        return charArr;
    }
}

这是我如何解决此问题的一些伪代码:

Place the letters from the first string of the array into a list
for each letter in the list{
    count the occurrences of the letter
    for each string in the array{
        count the occurrences of the letter
        if the letter occurs in the list more often than in the string{
            decrease occurrence of letter in list to match letter in string
        }
    }
}

希望这会有所帮助!

有点更改版本:

public List<String> commonChars01(String[] A) {
    int[][] lowerCase = new int[26][A.length];
    
    for(int i = 0 ; i< A.length ; i++){
        for(int j = 0; j < A[i].length(); j++) {
            lowerCase[A[i].charAt(j) -'a'][i] ++;
        }                        
    }        
  
    List<String> list = new ArrayList<>();
    
    for (int i = 0; i < 26; i++) { 
        // all the elements of the array should be equal
        long count = IntStream.of(lowerCase[i])
                .boxed().distinct().count();
        
        // [0, 0, 0] should fail, hence sum should be greater than or equal to 3.
        int sum = IntStream.of(lowerCase[i])
                .boxed().reduce(0, (a,b) -> a + b); 
                
        if ( sum >= 3 && count == 1) {
            int timesRepeated = lowerCase[i][0];
            while (timesRepeated != 0) {
                list.add(Character.toString((char)(i + 'a')));
                timesRepeated--;
            }
        }
    }        
    
    return list;
}

尝试以下 -

 public List<String> commonChars(String[] words) {
    List<String> ans = new ArrayList<>();
    int wordNums = words.length;
    int[][] charNums = new int[wordNums][26];
    for (int i = 0; i < wordNums; i++) {
        for (int j = 0; j < words[i].length(); j++) {
            charNums[i][words[i].charAt(j) - 'a'] +=1;
        }
    }
    for (int i = 0; i < 26; i++) {
        int j = 0;
        int count = Integer.MAX_VALUE;
        for (; j < wordNums; j++) {
            if(charNums[j][i] == 0 ){
                count = 0;
                break;
            }
            count = Math.min(count, charNums[j][i]);
        }
        for(int k = 0;k<count;k++){
            ans.add(String.valueOf((char) (i + 'a')));
        }
    }
    return ans;
}

最新更新