LWC 63:748. Shortest Completing Word

传送门:748. Shortest Completing Word

Problem:

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].

思路:
很暴力,找到一个word符合licensePlate的定义,并不断更新即可。只要licensePlate中出现的字母,word中必须出现,且出现的次数至少相等。采用map计数。

Java版本:

    public String shortestCompletingWord(String licensePlate, String[] words) {String ans = "";int minLen = 1111;int[] map = new int[32];for (char c : licensePlate.toLowerCase().toCharArray())if (c >= 'a' && c <= 'z') map[c - 'a'] ++;for (String word : words) {int len = word.length();if (contains(map, word)) {if (len < minLen) {ans = word;minLen = len;}}}return ans;}boolean contains(int[] map, String word) {int[] map2 = new int[32];for (char c : word.toLowerCase().toCharArray()) {map2[c - 'a'] ++;}for (int i = 0; i < 32; ++i) {if (map[i] != 0) {if (map2[i] < map[i]) return false;}}return true;}

Python版本:

    def shortestCompletingWord(self, licensePlate, words):""":type licensePlate: str:type words: List[str]:rtype: str"""licence_plate = licensePlate.lower()d = dict()for c in licence_plate:if c.isalpha():if c not in d:d[c] = 0d[c] += 1res = Nonelength = 1111for word in words:freq = d.copy()for c in word:if c in freq:freq[c] -= 1if freq[c] == 0:freq.pop(c)if not freq:if len(word) < length:res = wordlength = len(word)return res

LWC 63:748. Shortest Completing Word相关推荐

  1. 748. Shortest Completing Word

    748. 最短完整词 如果单词列表(words)中的一个单词包含牌照(licensePlate)中所有的字母,那么我们称之为完整词.在所有完整词中,最短的单词我们称之为最短完整词. 单词在匹配牌照中的 ...

  2. 【Leetcode_easy】748. Shortest Completing Word

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

  3. [leetcode] 748. Shortest Completing Word

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

  4. 【Leetcode】748. Shortest Completing Word

    题目地址: https://leetcode.com/problems/shortest-completing-word/ 给定一个长nnn的字符串sss和一个英文小写单词数组AAA,找到AAA中最短 ...

  5. 748. Shortest Completing Word(C++)

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

  6. 748. 最短补全词( Shortest Completing Word) 3ms做法心得

    做法大致就是将每个字符对应的ASCII码-'a'后存进数组,然后对照两个数组来求最优解. import java.util.Locale;//748. 最短补全词 public class Short ...

  7. SitePoint播客#63:有两个网站

    Episode 63 of The SitePoint Podcast is now available! This week your hosts are Patrick O'Keefe (@iFr ...

  8. 计算机教师招聘板书设计,教师编制考试:信息技术丨《WORD表格制作》教案设计...

    教师编制考试:信息技术丨<WORD表格制作>教案设计 一.教学目标 (一)知识与技能 1.掌握在word中创建表格的方法. 2.了解表格中的合并单元格.拆分单元格.行高.列宽这些基本概念. ...

  9. 剑指offer——面试题63:二叉搜索树的第k个结点

    剑指offer--面试题63:二叉搜索树的第k个结点 Solution1: 20180916重做 /* struct TreeNode {int val;struct TreeNode *left;s ...

最新文章

  1. TCP/IP详解--TCP/IP中三次握手 四次握手状态分析
  2. 填表2018-11-11
  3. Linux中crontab无法执行java程序的问题
  4. .NET做人脸识别并分类
  5. dll编译为html,如何为MSHTML v9编译.dll。我目前正在获取“MIDL 2035:预期的常量表达式”的100个错误...
  6. Node.js 提升运行效率
  7. 网络营销之百度营销技巧(一) 百度知道推广日常手册
  8. 首家新三板上市区块链公司:他们提供面向政务、商务、公众等解决方案
  9. 软考-高项-论文-信息系统项目的质量管理
  10. python分析红楼梦中人物形象_《红楼梦》中女性人物形象分析
  11. 人工智能实战2019 第二次作业 焦宇恒
  12. 设计实现抽象数据类型“三元组”,要求动态分配内存
  13. 软考高级 真题 2009年上半年 信息系统项目管理师 案例分析
  14. Memory 基础知识介绍
  15. 我的世界java怎么写彩字_我的世界告示牌怎么打彩字 告示牌制作图文教程
  16. GNU Radio系列教程(三):初级篇之GNU Radio GRC流图
  17. visio绘制叠色图
  18. 外贸软件日用工艺品行业解决方案
  19. ACM Digital Library访问及完整联动Zotero
  20. python模块和类的通用转换规则(2),三步转oo

热门文章

  1. 大学生申请软著(新系统!)注意事项!亲身经历!
  2. STM32Cube扩展包开发指南
  3. win7在安装时跳过输入用户名界面,直接开启管理员用户
  4. php下下级人数,关于PHP下级自动下滑的一点小见解
  5. 将 Unity5.3 的老项目升级到 Unity 2018.3 遇到的些许问题。
  6. 9V转5V降压芯片,大电流3A方案和LDO
  7. NVIDIA Jetson Xavier NX 修改设备树和编译内核
  8. 智能运维探索 | 2022年值得关注的三个AIOps新趋势
  9. Muse UI遇到的坑 1
  10. 1.MIL 编程环境设置