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 completethe 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].
from collections import Counter
class Solution(object):
def shortestCompletingWord(self, licensePlate, words):
"""
:type licensePlate: str
:type words: List[str]
:rtype: str
"""
a=''.join(l for l in licensePlate.lower() if l.isalpha())
A=[w for w in words if all(Counter(w)[k]>=Counter(a)[k] for k in Counter(a))]
return [w for w in A if len(w)==min(map(len,A))][0]

  

[LeetCode&Python] Problem 748. Shortest Completing Word的更多相关文章

  1. 【Leetcode_easy】748. Shortest Completing Word

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

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

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

  3. LeetCode 748 Shortest Completing Word 解题报告

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

  4. leetcode 748. Shortest Completing Word

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

  5. [LeetCode&Python] Problem 821. Shortest Distance to a Character

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  6. 748. Shortest Completing Word

    https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...

  7. [LeetCode] Shortest Completing Word 最短完整的单词

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

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

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

  9. [LeetCode&Python] Problem 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

随机推荐

  1. nginx源码安装教程(CentOS)

    1.说明 官方源码安装说明:http://nginx.org/en/docs/configure.html 源码包下载地址:http://nginx.org/en/download.html 版本说明 ...

  2. mac ci框架安装使用 memcached存session

    mac 安装memcached brew info memcached brew install memcached brew install memcached 安装过程具体不详细多讲 memcac ...

  3. nyoj-0708-ones(dp)

    nyoj-0708-ones 题意:用1,+,*,(,). 这四个符号组成表达式表达数s(0 <= s <= 10000),且1最少时1的的个数 状态转移方程: dp[i] = min(d ...

  4. shell 通配符

    Bash中的通配符 '?' 匹配一个任意字符 '*' 匹配0个或任意多个字符,也就是可以匹配任何内容 '[]' 匹配括号中任意一个字符.例如[abc]代表一定匹配一个字符,或者是a,或者是b,或者是c ...

  5. json模块&xml

    json模块将数据修改成字符串,方便其他语言进行识别. 只认双引号,不认单引号.使用json.dumps的操作步骤 先将单引号修改成单引号 将变量使用单引号引起来 将数据类型编程json字符串 jso ...

  6. 1.python函数式编程-map函数

    编程方法论 面向过程 函数式 面向对象 面向过程 将编程过程拆分成多个步骤,在函数中按照每个步骤进行编程: 函数式编程 编程语言定义的函数+数学意义的函数 1.不可变,不用变量保存状态,不修改变量: ...

  7. learning ddr state diagram

  8. day03 is 与== 常量

    is身份运算:比较的是id是否相等 ==判断值是否相等 ... 值相等id不一定相等 >>>x=1111111111111111111111111111111111111111111 ...

  9. css3 前端开发

    一.前缀: -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-radius)用于Safari和Chrome. 二.CSS3圆 ...

  10. eclipse package视图和navigator视图的区别

    package视图是适合开发的视图,因为开发时我们只关注源文件,并不关注编译后的二进制文,所有在该视图中存放二进制文件的classes文件被隐藏了,而navigator视图,就是项目在工作空间中存放的 ...