其他分享
首页 > 其他分享> > (Easy) Shortest Completing Word - LeetCode

(Easy) Shortest Completing Word - LeetCode

作者:互联网

Description:

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.

 

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
Note that the answer is not "step", because the letter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.

 

Example 2:

Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".
We return the one that occurred first.

 

Note:

  1. licensePlate will be a string with length in range [1, 7].
  2. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
  3. words will have a length in the range [10, 1000].
  4. Every words[i] will consist of lowercase letters, and have length in range [1, 15].

 

Accepted 23.5K Submissions 42.7K

Solution:

class Solution   {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        
        if(licensePlate ==null||licensePlate.length()==0){
            return null;
        }
        String res = "";
       
        for(int i = 0; i<licensePlate.length();i++){
            
            if((licensePlate.charAt(i)>=65 && licensePlate.charAt(i)<=90)||(licensePlate.charAt(i)>=97 && licensePlate.charAt(i)<=122))
                res = res+licensePlate.charAt(i);
            
            }
        res= res.toLowerCase();
        
        HashMap<Character, Integer> map = new HashMap<Character,Integer>();
        
        
        for(int i = 0; i<res.length(); i++){
            if(!map.containsKey(res.charAt(i))){
              // System.out.println(res.charAt(i)+" "+ Count(res,res.charAt(i)));
                map.put(res.charAt(i),Count(res,res.charAt(i)));
            }
        }
        
        int Cur_length = Integer.MAX_VALUE;
        String out = "";
        
        for(int i = 0; i<words.length; i++){
            
            for(int j = 0; j<res.length();j++){
                
                char temp_char = res.charAt(j);
                int tmp_count = Count(words[i], temp_char);
                // System.out.println("i= "+i+"j = "+j+" check0 "+" "+temp_char);
                if(tmp_count< map.get(temp_char)){
                    break;
                }
               // System.out.println("i= "+i+"j = "+j+" check1 ");
                if(j==res.length()-1){
                    if( words[i].length()<Cur_length){
                        Cur_length = words[i].length();
                       // System.out.println(words[i]+" "+Cur_length);
                        out = words[i];
                    }
                }
            }
        }
        //System.out.println(map.get('p'));
        //System.out.println(res);
        return out;
        
    }
    
    
    public int Count(String s, char a){
        int i = 0; 
        for(int j = 0; j< s.length(); j++){
            if(s.charAt(j)==a){
                i++;
            }
        }
        
        return i;
    }
  
    
}

 

标签:licensePlate,word,String,Completing,length,words,letters,Word,Shortest
来源: https://www.cnblogs.com/codingyangmao/p/11448249.html