[leetcode]748. Shortest Completing Word

162 篇文章 0 订阅

[leetcode]748. Shortest Completing Word


Analysis

想把拖鞋塞到某男生嘴里!!!—— [生气气]

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate
Here, for letters we ignore case. For example, “P” on the licensePlate still matches “p” on the word.
It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.
The license plate might have the same letter occurring multiple times. For example, given a licensePlate of “PP”, the word “pair” does not complete the licensePlate, but the word “supper” does.
其实就是个字符串匹配的问题,然后我做的时候忘记licensePlate里面还有非字母(数字,空格等),所以搞了很久,心态爆炸!

Implement

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        unordered_map<char, int> mymap;
        int num = 0;
        for(char s:licensePlate){
            if(s>='A' && s<='Z'){
                mymap[s+32]++;
                num++;
            }

            else if(s>='a' && s<='z'){
                mymap[s]++;
                num++;
            }

        }
        string res = "";
        for(string word:words){
            int tmp = num;
            unordered_map<char, int> tmpmap = mymap;
            for(char c:word){
                tmpmap[c]--;
                if(tmpmap[c]>=0)
                    tmp--;
            }
            if(tmp==0 && (res.size()==0 || res.size()>word.size()))
                res = word;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值