[hash table]748. Shortest Completing Word

  1. Shortest Completing Word

Given a string licensePlate and an array of strings words, find the shortest completing word in words.

A completing word is a word that contains all the letters in licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more than once in licensePlate, then it must appear in the word the same number of times or more.

For example, if licensePlate = “aBc 12c”, then it contains letters ‘a’, ‘b’ (ignoring case), and ‘c’ twice. Possible completing words are “abccdef”, “caaacab”, and “cbca”.

Return the shortest completing word in words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the first one that occurs in words.

Example 1:

Input: licensePlate = “1s3 PSt”, words = [“step”,“steps”,“stripe”,“stepple”]
Output: “steps”
Explanation: licensePlate contains letters ‘s’, ‘p’, ‘s’ (ignoring case), and ‘t’.
“step” contains ‘t’ and ‘p’, but only contains 1 ‘s’.
“steps” contains ‘t’, ‘p’, and both ‘s’ characters.
“stripe” is missing an ‘s’.
“stepple” is missing an ‘s’.
Since “steps” is the only word containing all the letters, that is the answer.
Example 2:

Input: licensePlate = “1s3 456”, words = [“looks”,“pest”,“stew”,“show”]
Output: “pest”
Explanation: licensePlate only contains the letter ‘s’. All the words contain ‘s’, but among these “pest”, “stew”, and “show” are shortest. The answer is “pest” because it is the word that appears earliest of the 3.

Constraints:

1 <= licensePlate.length <= 7
licensePlate contains digits, letters (uppercase or lowercase), or space ’ '.
1 <= words.length <= 1000
1 <= words[i].length <= 15
words[i] consists of lower case English letters.

solution1数组

数组模拟哈希表更快
查找一个字符串包含另一个字符串用数组也行哈希表也行

根据题意,我们先统计licensePlate 中每个字母的出现次数(忽略大小写),然后遍历 words 中的每个单词,若 2626 个字母在该单词中的出现次数均不小于在licensePlate 中的出现次数,则该单词是一个补全词。返回最短且最靠前的补全词。

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        array<int,26>cnt={};
        for(auto ch:licensePlate){
            if(isalpha(ch)){
                ++cnt[tolower(ch)-'a'];
            }
        }
        //用于记录最终结果的 word 的下标索引
        int idx=-1;
        for(int i=0;i<words.size();++i){
            array<int,26>c{};
            for(char ch:words[i]){
                ++c[ch-'a'];
            }
            bool flag=true;
            //licensePlate 中出现的单词均要在 word 中,且出现的次数只能多不能少
            for(int j=0;j<26;j++){
                if(c[j]<cnt[j]){
                    flag=false;
                    break;
                }
            }
            //索引较小且单词长度最短者优先
            if(flag && (idx<0 || words[i].size()<words[idx].size())){
                idx=i;
            }
        }
        return words[idx];
    }
};

solution2哈希表

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        unordered_map<char, int> dict;
        for (auto& c : licensePlate)
            if (c != ' ' && !isdigit(c))
                dict[tolower(c)]++;
        string res;
        for (auto& word : words)
        {
            unordered_map<char, int> curFreq;
            for (auto c : word)
                curFreq[c]++;
            if (isValid(curFreq, dict))
            {
                if (res.empty() || res.size() > word.size())
                    res = word;
            }
        }
        return res;
    }
    /* str1中含有str2中的所有字符 */
    bool isValid(unordered_map<char, int>& dict1, unordered_map<char, int>& dict2)
    {
        for (auto& kvp : dict2)
        {
            if (dict1[kvp.first] < kvp.second)
                return false;
        }
        return true;
    }
};

作者:yanglr
链接:https://leetcode.cn/problems/shortest-completing-word/solution/geekplayers-leetcode-ac-yi-kan-jiu-dong-2276g/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值