leetcode 748. Shortest Completing Word
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.
Example 1:
Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
Note that the answer is not "step", because the letter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.
Example 2:
Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".
We return the one that occurred first.
Note:
licensePlate will be a string with length in range [1, 7].
licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
words will have a length in the range [10, 1000].
Every words[i] will consist of lowercase letters, and have length in range [1, 15].
模拟找一下
class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
map<char, int> mp;
int len = 1000000;
string ans = "";
for (auto c : licensePlate) {
if (c >= 'A' && c <= 'Z') c += 32;
if (c >= 'a' &&c <= 'z') {
mp[c]++;
cout << c <<endl;
}
}
for (int i = 0; i < words.size(); ++i) {
map<char, int> mp1 = mp;
for (auto c : words[i]) {
if (c >= 'A' && c <= 'Z') c += 32;
if (c >= 'a' &&c <= 'z') {
if (mp1[c] > 0)mp1[c] --;
}
}
int mark = 0;
for (auto x : mp1) {
if (x.second != 0) {
mark = 1;break;
}
}
if (!mark) {
if (words[i].size() < len) {
len = words[i].size();
ans = words[i];
}
}
}
return ans;
}
};
leetcode 748. Shortest Completing Word的更多相关文章
- LeetCode 748 Shortest Completing Word 解题报告
题目要求 Find the minimum length word from a given dictionary words, which has all the letters from the ...
- 【Leetcode_easy】748. Shortest Completing Word
problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...
- 【LeetCode】748. Shortest Completing Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- 748. Shortest Completing Word
https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...
- [LeetCode] Shortest Completing Word 最短完整的单词
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- LeetCode算法题-Shortest Completing Word(Java实现)
这是悦乐书的第309次更新,第330篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第178题(顺位题号是748).从给定的字典单词中查找最小长度单词,其中包含字符串lic ...
- [Swift]LeetCode748. 最短完整词 | Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
随机推荐
- Typora +google + Markdown Here 公众号
一劳永逸的公众号排版方法 http://mp.weixin.qq.com/s/zb-YaacNLggG2-njF5HJ0A
- fastscript增加三方控件之二
fastscript增加三方控件之二 unit fs_BsDataSet; interface {$i fs.inc} uses SysUtils, Classes, fs_iinterpreter, ...
- 邁向IT專家成功之路的三十則鐵律 鐵律八:IT人學習之道-基礎功
修練過中國武術的人都知道,任何一種拳法的學習最重要的就是基礎功,而基礎功又可分為內在與外在的修練,內在的修練強調在平心.靜氣.不爭的調息.至於外在這首重在站樁.鬆沉.不疾不徐的應對能力.有了深厚基礎的 ...
- PhoneGap_百度百科
PhoneGap编辑 PhoneGap是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的 快速开发平台.它使开发者能够利用iPhone,Android,Palm,Sym ...
- Qnap 中VM下的win7
因工作需要alone了Qnap 中VM下的win7,更改冲突项:1. 电脑名2. 必需卸载虚拟网卡,因为克隆系统的MAC地址相同. 添加网卡,指定IP Address.Subnet mask.G ...
- project管理之makefile与自己主动创建makefile文件过程
(风雪之隅 http://www.laruence.com/2009/11/18/1154.html) Linux Makefile自己主动编译和链接使用的环境 想知道到Linux Makefile系 ...
- Error building Player: Win32Exception: ApplicationName='E:/adt-20140702/sdk\tools\zipalign.exe', Com
1.原因 更新sdk后报错..由于版本号不同,zipalign.exe所处路径不同 2.解决的方法 在sdk路径下搜索zipalign.exe .然后拷贝到报错内容中制定的路径即可了.
- 改进后的向量空间模型(VSM)
我们採用更加形式化的定义,并採用稍大一些的样例来展示怎样使用基于数据集频率的权重.相应于一个给定的词项,其权重使用IDF(逆文档频率)来计算. 为了给每篇文档建立一个相应的向量,能够考虑例如以下定义. ...
- SASS入门之SASS安装
当然...凭借我这样的肤浅的智商,根本不能理解什么叫certificate verfiy fail... 所以找了一段时间的方法,最后最终在一个sass群里找到了... 发在这里纯属作为自己的一个学习 ...
- PNG24图片兼容IE6解决的方法
非常多人都遇到一个问题:那就是PNG不能正常显示,比方: 网上试过的非常多办法都非常难实现.要嘛就是效果不好,那如今最好的办法就是直接调用JS插件,解决! 点击下载 如今说一下怎么用这个文件吧! 首先 ...