[LeetCode&Python] Problem 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 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:
licensePlatewill be a string with length in range[1, 7].licensePlatewill contain digits, spaces, or letters (uppercase or lowercase).wordswill have a length in the range[10, 1000].- 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的更多相关文章
- 【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 748 Shortest Completing Word 解题报告
题目要求 Find the minimum length word from a given dictionary words, which has all the letters from the ...
- leetcode 748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- Homebrew 备忘
每次都搜,写篇博客记录以备后续查看. 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew ...
- 青蛙跳N阶(变态跳)
https://www.nowcoder.com/questionTerminal/22243d016f6b47f2a6928b4313c85387 描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级 ...
- c++ count函数
count函数 algorithm头文件(#include <algorithm>)定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次 ...
- linux文件权限多一个+啥意思
linux文件权限显示多了一个+,说明添加了acl权限 使用getfacl filename 能查看到 ower group other 还多了一个php 用户有rwx权限. acl详解 转: ht ...
- etymon word flower bee apiary forget out~1
1● anth 2● flower 花 1● ap 2● bee 3● apiary 养殖场
- python 利用turtle库绘制七段数码管的方式,绘制当前事件(时分秒00:00:00)
# coding:utf-8# 绘制七段数码管,显示当前时间import timeimport turtle as tt # 绘制间隔def drawGap(): tt.penup() tt.fd(3 ...
- nyoj-0469-擅长排列的小明 II(找规律)
nyoj-0469-擅长排列的小明 II 思路:递推分析:为了简便起见,我们用Ai代表第i个数字 , 由于A1一直是1,所以A2只能是2或3.假设dp[n]表示1->n这个序列的方案数 ...
- 【Jmeter_WebService接口】对项目中的GetProduct接口生成性能脚本
一.环境信息 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 二.通过soapui获取soup请求信息 1 ...
- 尚学堂java 参考答案 第九章
一.选择题 1.AC 解析:A.注意题目是Collections不是 Collection,前者是一个until下的类,后者才是接口 C.Set中的数据是无序且不能重复的 2.A 解析:将发生数组越界 ...
- MariaDB Role
一.MariaDB Role介绍 MariaDB从10.0/10.1版本开始支持role. Role相当于各种权限的集合,可以给多个账户统一权限的修改直接通过修改role来实现,不需要每个账户一个一个 ...