748.最短补全词

题目描述

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

补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词 。

在匹配 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 里最靠前的那个。

示例 3:

输入:licensePlate = "Ah71752", words = ["suggest","letter","of","husband","easy","education","drug","prevent","writer","old"]
输出:"husband"

示例 4:

输入:licensePlate = "OgEu755", words = ["enough","these","play","wide","wonder","box","arrive","money","tax","thus"]
输出:"enough"

示例 5:

输入:licensePlate = "iMSlpe4", words = ["claim","consumer","student","camera","public","never","wonder","simple","thought","use"]
输出:"simple"

提示:

    1 <= licensePlate.length <= 7
    licensePlate 由数字、大小写字母或空格 ' ' 组成
    1 <= words.length <= 1000
    1 <= words[i].length <= 15
    words[i] 由小写英文字母组成

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

方法一:

哈希表

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        string a,b;
        int num=16,flag;
        for(char c:licensePlate){
            if(c>='a'&&c<='z'){
                a=a+c;
            }
            else if(c>='A'&&c<='Z'){
                c=c+32;
                a=a+c;
            }
        }
        for(string s:words){
            unordered_map<char,int> map;
            flag=0;
            for(char c:s){
                auto t=map.find(c);
                if(t==map.end()){
                    map.insert(pair<char,int>(c,1));
                }
                else{
                    t->second++;
                }
            }
            for(char c:a){
                auto t=map.find(c);
                if(t!=map.end()){
                    if(t->second==1){
                        map.erase(t);
                    }
                    else{
                        t->second--;
                    }
                }
                else{
                    flag=1;
                }
            }
            if(flag==0&&num>s.length()){
                num=s.length();
                b=s;
            }
        }
        return b;
    }
};

方法二:

比较数组

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        int a[27]={0},num=16;
        string ans;
        for(char c:licensePlate){
            if(isalpha(c)){
                a[tolower(c)-'a']++;
            }
        }
        for(auto s:words){
            int b[27]={0},flag=0;
            for(char c:s){
                b[c-'a']++;
            }
            for(int i=0;i<26;i++){
                if(a[i]>b[i]){
                    flag=1;
                }
            }
            if(flag==0&&s.length()<num){
                num=s.length();
                ans=s;
            }  
        }
        return ans;
    }
};

方法三:

排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 先来先服务调度算法(FCFS,First Come First Served):这是最简单的调度算法。在这种算法中,任务按照它们到达的顺序进行调度。这种算法的缺点是它不能保证任务在任何时候都以最有效的方式执行,因为它不考虑任务的优先级或等待时间。 2. 最寻道优先调度算法(SSTF,Shortest Seek Time First):这种算法根据任务的寻道时间来确定任务的执行顺序。寻道时间是指从当前位置到目标位置所需的时间。这种算法的目标是最小化总的寻道时间,但可能会导致长时间的空闲等待。 3. 扫描(电梯调度)算法:这种算法将建筑物划分为多个区域,然后电梯从一个区域移动到另一个区域,直到所有区域都被访问过。这种算法的目标是最小化电梯的运行时间和乘客的等待时间。然而,由于电梯需要不断地在不同的楼层之间移动,因此这种算法的效率较低。 4. 循环扫描调度算法:这种算法结合了电梯调度和最寻道优先调度算法的特点。电梯在一个区域内部以最寻道优先的方式运行,而在区域之间则以循环扫描的方式运行。这种算法试图在效率和公平性之间找到一个平衡点。 实验结果结论: 1. 先来先服务调度算法适用于简单的场景,但在有优先级和等待时间限制的任务时可能不是最佳选择。 2. 最寻道优先调度算法可以有效地减少寻道时间,但可能导致长时间的空闲等待。 3. 扫描(电梯调度)算法在某些情况下可以提高效率,但由于电梯需要不断地在不同的楼层之间移动,因此效率较低。 4. 循环扫描调度算法试图在效率和公平性之间找到一个平衡点,但可能需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值