Java实现 LeetCode 748 最短完整词(字母拆分+暴力)

748. 最短完整词

若是单词列表(words)中的一个单词包含牌照(licensePlate)中全部的字母,那么咱们称之为完整词。在全部完整词中,最短的单词咱们称之为最短完整词。java

单词在匹配牌照中的字母时不区分大小写,好比牌照中的 “P” 依然能够匹配单词中的 “p” 字母。ide

咱们保证必定存在一个最短完整词。当有多个单词都符合最短完整词的匹配条件时取单词列表中最靠前的一个。spa

牌照中可能包含多个相同的字符,好比说:对于牌照 “PP”,单词 “pair” 没法匹配,可是 “supper” 能够匹配。code

示例 1:

输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
输出:"steps"
说明:最短完整词应该包括 "s""p""s" 以及 "t"。对于 "step" 它只包含一个 "s" 因此它不符合条件。同时在匹配过程当中咱们忽略牌照中的大小写。
 

示例 2:

输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
说明:存在 3 个包含字母 "s" 且有着最短长度的完整词,但咱们返回最早出现的完整词。

注意:ip

牌照(licensePlate)的长度在区域[1, 7]中。
牌照(licensePlate)将会包含数字、空格、或者字母(大写和小写)。
单词列表(words)长度在区间 [10, 1000] 中。
每个单词 words[i] 都是小写,而且长度在区间 [1, 15] 中。it

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] license = new int[26];
        for(char c : licensePlate.toCharArray()){
            if(c >= 'a' && c <= 'z')
                license[c - 'a']++;
            else if(c >= 'A' && c <= 'Z')
                license[c - 'A']++;
        }
        String res = null;
        for(String word : words){
            if(isContains(license,word))
                if(res == null || word.length() < res.length())
                    res = word;
        }
        
        return res;
    }
    
    private boolean isContains(int[] license,String word){
        int[] ans = new int[26];
        for(char c : word.toCharArray()){
            if(c >= 'a' && c <= 'z')
                ans[c - 'a']++;
            else if(c >= 'A' && c <= 'Z')
                ans[c - 'A']++;
        }
        
        for(int i = 0;i < 26;i++)
            if(ans[i] < license[i])
                return false;
        return true;
    }
}

748. 最短完整词

若是单词列表(words)中的一个单词包含牌照(licensePlate)中全部的字母,那么咱们称之为完整词。在全部完整词中,最短的单词咱们称之为最短完整词。io

单词在匹配牌照中的字母时不区分大小写,好比牌照中的 “P” 依然能够匹配单词中的 “p” 字母。class

咱们保证必定存在一个最短完整词。当有多个单词都符合最短完整词的匹配条件时取单词列表中最靠前的一个。test

牌照中可能包含多个相同的字符,好比说:对于牌照 “PP”,单词 “pair” 没法匹配,可是 “supper” 能够匹配。word

示例 1:

输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
输出:"steps"
说明:最短完整词应该包括 "s""p""s" 以及 "t"。对于 "step" 它只包含一个 "s" 因此它不符合条件。同时在匹配过程当中咱们忽略牌照中的大小写。
 

示例 2:

输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
说明:存在 3 个包含字母 "s" 且有着最短长度的完整词,但咱们返回最早出现的完整词。

注意:

牌照(licensePlate)的长度在区域[1, 7]中。
牌照(licensePlate)将会包含数字、空格、或者字母(大写和小写)。
单词列表(words)长度在区间 [10, 1000] 中。
每个单词 words[i] 都是小写,而且长度在区间 [1, 15] 中。

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] license = new int[26];
        for(char c : licensePlate.toCharArray()){
            if(c >= 'a' && c <= 'z')
                license[c - 'a']++;
            else if(c >= 'A' && c <= 'Z')
                license[c - 'A']++;
        }
        String res = null;
        for(String word : words){
            if(isContains(license,word))
                if(res == null || word.length() < res.length())
                    res = word;
        }
        
        return res;
    }
    
    private boolean isContains(int[] license,String word){
        int[] ans = new int[26];
        for(char c : word.toCharArray()){
            if(c >= 'a' && c <= 'z')
                ans[c - 'a']++;
            else if(c >= 'A' && c <= 'Z')
                ans[c - 'A']++;
        }
        
        for(int i = 0;i < 26;i++)
            if(ans[i] < license[i])
                return false;
        return true;
    }
}
相关文章
相关标签/搜索