【leetcode27-----最短补全词】

最短补全词

给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出 words 中的 最短补全词 。

补全词 是一个包含 licensePlate 中所有字母的单词。忽略 licensePlate 中的 数字和空格 。不区分大小写。如果某个字母在 licensePlate 中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。

例如:licensePlate = "aBc 12c",那么它的补全词应当包含字母 'a'、'b' (忽略大写)和两个 'c' 。可能的 补全词 有 "abccdef"、"caaacab" 以及 "cbca" 。

请返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words 中 第一个 出现的那个。

示例 1:

输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
输出:"steps"
解释:最短补全词应该包括 "s"、"p"、"s"(忽略大小写) 以及 "t"。
"step" 包含 "t"、"p",但只包含一个 "s",所以它不符合条件。
"steps" 包含 "t"、"p" 和两个 "s"。
"stripe" 缺一个 "s"。
"stepple" 缺一个 "s"。
因此,"steps" 是唯一一个包含所有字母的单词,也是本例的答案。
示例 2:

输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
解释:licensePlate 只包含字母 "s" 。所有的单词都包含字母 "s" ,其中 "pest"、"stew"、和 "show" 三者最短。答案是 "pest" ,因为它是三个单词中在 words 里最靠前的那个。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/shortest-completing-word
 

题解一:

笔者使用的暴力解,很复杂效率很低,主要是记录一下,首先找出给定字符串中的字母,将其全部转成小写,然后遍历字符串数组,对于数组中的每一个元素,也就是每一个单词,将其包含的字母以及字母出现的次数以键值对的形式存入到HashMap集合中,然后判断该单词中是否包含字符串中的字母,若全都包含,再判断该单词是不是长度最小的,最后返回长度最小的包含所有字母的单词。代码如下:

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        Map<String,Integer> map=new HashMap<String,Integer>();
        String s="";
		int count=Integer.MAX_VALUE;
        String res="";
		for(int i=0;i<licensePlate.length();i++) {
			if(licensePlate.charAt(i)>='a'&&licensePlate.charAt(i)<='z') {
				s=s+licensePlate.charAt(i);
			}else if(licensePlate.charAt(i)>='A'&&licensePlate.charAt(i)<='Z') {
				char c=licensePlate.charAt(i);
				c+=32;    //将大写字母转换成小写字母
				s=s+c;
			}
		}
        for(String ss:words){
            for(int i=0;i<ss.length();i++){
               
               char key=ss.charAt(i);
                map.put(key,map.getOrDefault(key,0)+1);//把单词中出现的字母以及出现次数存入集合
            }
            int k=0;
            for(;k<s.length();k++){
                
                char key+=s.charAt(k);
                if(map.containsKey(key)){//若单词包含字符串中的字母
                    int value=map.get(key);
                    map.replace(key,value-1);//把字母对应的value值减1,因为字符串中相同字母可能会出现多次
                    if(map.get(key)==0){//当字母对应的value值为0时,就把这个字母从集合中移出
                        map.remove(key);//因为可能字符串中有三个s,但单词中只有两个s,所以每次要把value值减一,当value等于0时要把字母从集合中移出
                    }
                }else{
                    break;
                }
            }
            map.clear();
            if(k==s.length()){
                if(ss.length()<count){//判断是不是长度最小的包含所有字母的单词
                    count=ss.length();
                    res=ss;
                }
            }
        }
        return res;
    }
}

题解二:

官解的解法是分别统计字符串中26个字母的出现次数和字符串数组中每个单词中26个字母出现的次数,若单词中26个字母出现的次数都不小于字符串,则该单词就包含字符串中出现的所有字母,最后返回长度最短的包含所有字母的单词。代码如下:

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] cnt = new int[26];
        for (int i = 0; i < licensePlate.length(); ++i) {
            char ch = licensePlate.charAt(i);
            if (Character.isLetter(ch)) {
                ++cnt[Character.toLowerCase(ch) - 'a'];
            }
        }
        int idx = -1;
        for (int i = 0; i < words.length; ++i) {
            int[] c = new int[26];
            for (int j = 0; j < words[i].length(); ++j) {
                char ch = words[i].charAt(j);
                ++c[ch - 'a'];
            }
            boolean ok = true;
            for (int j = 0; j < 26; ++j) {
                if (c[j] < cnt[j]) {
                    ok = false;
                    break;
                }
            }
            if (ok && (idx < 0 || words[i].length() < words[idx].length())) {
                idx = i;
            }
        }
        return words[idx];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值