1002 - find common characters

Posted by mrkumar on Sat, 07 Dec 2019 00:01:47 +0100

Preface

Weekly Contest 126 Of Find common characters:

Given A string array A with only lowercase letters, returns A list of all characters (including repeating characters) displayed in each string in the list. For example, if A character appears three times in each string, but not four times, you need to include that character three times in the final answer.

You can return the answers in any order.

Example 1:

Input: ["bella","label","roller"]
Output: ["e","l","l"]

Example 2:

Input: ["cool","lock","cook"]
Output: ["c","o"]

Tips:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100
  3. A[i][j] is a small letter

Solving problems

It is important to output the repeated characters in all words. If there are more than one repeated characters, output the corresponding number. My solution is as follows:

  1. Take the character in the word as the key and the number of characters in the word as value Map to record the character and frequency of each word.
  2. Find out the elements with the same key in each Map, and take the minimum value of occurrence value as the new value to form the Map with repeated word occurrence frequency
  3. Traverse the Map in step 2. According to value, you can know the number of occurrences of each word and convert the Map into a List

Implementation code

    /**
     * 1002. Find common characters
     * @param A
     * @return
     */
    public List<String> commonChars(String[] A) {
        // map recording the number of occurrences of repeating characters
        // key is the character in the word, and value is the number of occurrences of the character in the word
        Map<String,Integer> searchMap=new HashMap<>();
        for(int i=0;i<A.length;i++){
            String[] words=A[i].split("");
            Map<String,Integer> map=new HashMap<>();
            for(String word:words){
                if(map.containsKey(word)){
                    map.put(word,map.get(word)+1);
                }else{
                    map.put(word,1);
                }
            }
            if(i==0){//Record all characters and their corresponding occurrence times in the first traversal
                searchMap.putAll(map);
            }else{
                Iterator<Map.Entry<String,Integer>> it=searchMap.entrySet().iterator();
                //map of the number of occurrences of new repeating characters
                Map<String,Integer> newSearchMap=new HashMap<>();
                while (it.hasNext()){
                    Map.Entry<String,Integer> entry=it.next();
                    String key=entry.getKey();
                    Integer value=entry.getValue();
                    if(map.containsKey(key)){// If the same character exists in the original map, it should be recorded in the new map. It should be noted that the number of occurrences is less
                        Integer otherValue=map.get(key);
                        newSearchMap.put(key,Math.min(value,otherValue));
                    }
                }
                searchMap=newSearchMap;
            }
        }
        List<String> result=new ArrayList<>();
        searchMap.forEach((k,v)->{//Convert map to result
            for(int i=1;i<=v;i++){//Add the corresponding number of key s to the result according to the number of occurrences value
                result.add(k);
            }
        });
        return result;
    }

Topics: Java less