今天这个简单题,没啥好说的,把代码贴这

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int len = licensePlate.length();
        //题目给出的条件,最长单词就是15个字母
        int shortestLen = 15;
        String resStr = "";
        for(String word : words)
        {
            if(cover(licensePlate,word))
            {
                if(word.length() < shortestLen)
                {
                    resStr = word;
                    shortestLen = word.length();
                }
            }
        }
        return resStr;
    }
    public boolean cover(String licensePlate,String word)
    {
        int[] charInfo = new int[26];
        for(int i =0;i < licensePlate.length();i++)
        {
            char letter = licensePlate.charAt(i);
            if(letter - 'a' >= 0 && letter - 'a' < 26)
            {
                charInfo[letter-'a']++;
            }
            if(letter - 'A' >= 0 && letter - 'A' < 26)
            {
                charInfo[letter-'A']++;
            }
        }
        for(int i =0;i < word.length();i++)
        {
            char letter = word.charAt(i);
            charInfo[letter-'a']--;
        }
        for(int i = 0;i < 26;i++)
        {
            if(charInfo[i] > 0)
            {
                return false;
            }
        }
        return true;
    }
}

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐