Short java solution (18ms)


  • 0
    public String shortestCompletingWord(String licensePlate, String[] words) {
        licensePlate = licensePlate.toLowerCase();
        int[] num = new int[128];
        int total = 0;
        for(char ch : licensePlate.toCharArray()) {
            if (ch >= 'a' && ch <= 'z') {
                total++;
                num[ch]++;
            }
        }
        String ans = "aaaaaaaaaaaaaaaaa";
        for(String s : words) {
            int count = 0;
            int[] num1 = new int[128];
            for(char ch : s.toCharArray())
                if (num1[ch]++ < num[ch]) count++;
            if (count == total && s.length() < ans.length())
                ans = s;
        }
        return ans;
    }

Log in to reply
 

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