share my java solution!


  • 0
    T
    class Solution {
        public String shortestCompletingWord(String licensePlate, String[] words) {
            int[] array = new int[26];
            String str = licensePlate.toLowerCase();
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)>='a' && str.charAt(i)<='z'){
                    array[str.charAt(i)-'a']++;   
                }
            }
            
            
            int minLen = Integer.MAX_VALUE;
            int min_index = 0;
            
            int p=-1;
            for(String word:words){
                p++;
                int[] temp = new int[26];
                for(char c:word.toCharArray()){
                    temp[c-'a']++;
                }
                
                boolean check = true;
                for(int i=0;i<26;i++){
                    if(temp[i]<array[i]){
                        check = false;
                        break;
                    }
                }
                
                if(check==true && minLen>word.length()){
                    minLen = word.length();
                    min_index = p;
                }
            }
            
            return words[min_index];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.