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:

  1. licensePlate will be a string with length in range [1, 7].
  2. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
  3. words will have a length in the range [10, 1000].
  4. Every words[i] will consist of lowercase letters, and have length in range [1, 15].

这道题给了我们一个车牌号,还有一些单词,让我们找出包含这个车牌号中所有字母的第一个最短的单词。车牌中的字母有大小写之分,但是单词只是由小写单词组成的,所以需要把车牌号中的所有大写字母都转为小写的,转换方法很简单,ASCII码加上32即可。我们建立车牌中各个字母和其出现的次数之间的映射,同时记录所有字母的个数total,然后遍历所有的单词,对于每个单词都要单独处理,我们遍历单词中所有的字母,如果其在车牌中也出现了,则对应字母的映射减1,同时还需匹配的字母数cnt也自减1,最后遍历字母完成后,如果cnt为0(说明车牌中所有的字母都在单词中出现了),并且结果res为空或长度大于当前单词word的话,更新结果即可,参见代码如下:

解法一:

class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
string res = "";
int total = ;
unordered_map<char, int> freq;
for (char c : licensePlate) {
if (c >= 'a' && c <= 'z') {++freq[c]; ++total;}
else if (c >= 'A' && c <= 'Z') {++freq[c + ]; ++total;}
}
for (string word : words) {
int cnt = total;
unordered_map<char, int> t = freq;
for (char c : word) {
if (--t[c] >= ) --cnt;
}
if (cnt == && (res.empty() || res.size() > word.size())) {
res = word;
}
}
return res;
}
};

如果这道题的单词是按长度排序的话,那么上面的方法就不是很高效了,因为其会强制遍历完所有的单词。所以我们考虑给单词排序,博主这里用了TreeMap这个数据结构建立单词长度和包含所有该长度单词的数组之间的映射,其会自动按照单词长度来排序。然后还使用了一个chars数组来记录车牌中的所有字母,这样就可以方便的统计出字母总个数。我们从单词长度等于字母总个数的映射开始遍历,先检验该长度的所有单词。这里检验方法跟上面略有不同,但都大同小异,用一个bool型变量succ,初始化为true,然后建立一个字母和其出现次数的映射,先遍历单词,统计各个字母出现的次数。然后就遍历chars数组,如果chars中某个字母不在单词中,那么succ赋值为false,然后break掉。最后我们看succ,如果仍为true,直接返回当前单词word,之后的单词就不用再检验了,参见代码如下:

解法二:

class Solution {
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
map<int, vector<string>> m;
vector<char> chars;
for (string word : words) {
m[word.size()].push_back(word);
}
for (char c : licensePlate) {
if (c >= 'a' && c <= 'z') chars.push_back(c);
else if (c >= 'A' && c <= 'Z') chars.push_back(c + );
}
for (auto a : m) {
if (a.first < chars.size()) continue;
for (string word : a.second) {
bool succ = true;
unordered_map<char, int> freq;
for (char c : word) ++freq[c];
for (char c : chars) {
if (--freq[c] < ) {succ = false; break;}
}
if (succ) return word;
}
}
return "";
}
};

参考资料:

https://discuss.leetcode.com/topic/114155/java-c-clean-code

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Shortest Completing Word 最短完整的单词的更多相关文章

  1. 【Leetcode_easy】748. Shortest Completing Word

    problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...

  2. [Swift]LeetCode748. 最短完整词 | Shortest Completing Word

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  3. 【LeetCode】748. Shortest Completing Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode算法题-Shortest Completing Word(Java实现)

    这是悦乐书的第309次更新,第330篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第178题(顺位题号是748).从给定的字典单词中查找最小长度单词,其中包含字符串lic ...

  5. LeetCode 748 Shortest Completing Word 解题报告

    题目要求 Find the minimum length word from a given dictionary words, which has all the letters from the ...

  6. [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 ...

  7. leetcode 748. Shortest Completing Word

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  8. [LeetCode] Most Common Word 最常见的单词

    Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...

  9. [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

随机推荐

  1. 打印十字图 JAVA 递归实现

    这个是我自己想的,头疼了一个下午,不过还好.做出来了.在网上找这道题但没有找到用递归的做法. /*递归思想实现 * 标题:打印十字图 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示(可 ...

  2. 基于bootstrap的表格数据展示

    一.导入bootstrap文件 二.前端html代码 对应的是前台条件查询和js数据获取 js数据获取部分在第四段 三.后台数据 total为集合总数  int类型 rows为前台需要展示的数据集合 ...

  3. 听翁恺老师mooc笔记(7)--字符串1

    C语言中字符串的定义 如果定义一个字符数组word,并使用大括号对其初始化,如下图所示: 但是这个不是C语言的字符串,只是字符数组,不是字符串,因为不能使用字符串的方式进行计算.那么C语言的字符串长什 ...

  4. 配置SpringAop时需要用到的AspectJ表达式

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  5. bzoj 2962 序列操作

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 有一个长度为n的序列, ...

  6. asp.net web api 控制器

    1控制器操作的参数 控制器操作的参数可以是内置类型也可以是自定义类型,无参也是允许的. 2控制器操作返回值 类型 说明 void 操作返回值为void时,Web API返回空HTTP响应,其状态码为2 ...

  7. D的下L

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2   描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给 ...

  8. 11-移动端开发教程-zepto.js入门教程

    Zepto.js是一个轻量级的针对现代浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. 1. Why Zepto.js? API类 ...

  9. 在网络编程中的io流小问题

    在客户端和服务端调用io流进行传输数据的过程中,当将数据write到outputstream中,需要及时刷新,否则会发生io阻塞. 在输入数据的时候,最好选用BufferedReader,因为read ...

  10. Python之旅_第一章Python入门

    一.编程语言分类 1.机器语言:即计算机能听懂的二进制语言,0000 0001,直接操控硬件: 2.汇编语言:简写的英文标识符代替二进制语言,本质同样是直接操控硬件: 3.高级语言:用更贴近人类的语言 ...