【LeetCode】748. Shortest Completing Word 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/shortest-completing-word/description/
题目描述
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].
题目大意
又是一个题目特别长,其实很简单的题,差点就放弃了。。
找出最短的符合licensePlate原则的字符串,如果有多个,那么返回第一个。
licensePlate原则其实特别简单,就是去除掉licensePlate里面的空格和数字,只保留英文字符,并转化为小写。然后再找words中包含这些字符的最短的字符串。
解题方法
这个题目中licensePlate规则看起来很复杂,其实只保留英文小写字母,然后找匹配就好了。看到一个比较好的方法,是通过Counter来做的,学到了可以使用substract
方法来做减法。这样可以方便的找出words中包含的licensePlate的word。
代码:
import re
from collections import Counter
class Solution(object):
def shortestCompletingWord(self, licensePlate, words):
"""
:type licensePlate: str
:type words: List[str]
:rtype: str
"""
regex = re.compile('[^a-zA-Z]')
license = regex.sub('',licensePlate)
counter1 = Counter(license.lower())
res = ''
for word in words:
if self.count(counter1, word):
if res == '' or len(word) < len(res):
res = word
return res
def count(self, counter1, word):
counter2 = Counter(word)
counter2.subtract(counter1)
return all([c >= 0 for c in counter2.values()])
二刷,python.
和上面的方法基本一样,可是更容易理解了。
class Solution(object):
def shortestCompletingWord(self, licensePlate, words):
"""
:type licensePlate: str
:type words: List[str]
:rtype: str
"""
subs = "1234567890"
licensePlate = licensePlate.replace(" ", "").lower()
for sub in subs:
licensePlate = licensePlate.replace(sub, "")
res = ""
plateCount = collections.Counter(licensePlate)
for word in words:
match = True
wordCount = collections.Counter(word)
for w, c in plateCount.items():
if c > wordCount[w]:
match = False
if (not res or len(res) > len(word)) and match:
res = word
return res
日期
2018 年 3 月 3 日
2018 年 11 月 10 日 —— 这么快就到双十一了??
【LeetCode】748. Shortest Completing Word 解题报告(Python)的更多相关文章
- 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_easy】748. Shortest Completing Word
problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...
- [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 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 748. Shortest Completing Word
https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- python—模拟生成双色球号
双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...
- 谈一谈 DDD
一.前言 最近 10 年的互联网发展,从电子商务到移动互联,再到"互联网+"与传统行业的互联网转型,是一个非常痛苦的转型过程.在这个过程中,一方面会给我们带来诸多的挑战,另一方面又 ...
- Spark On Yarn的各种Bug
今天将代码以Spark On Yarn Cluster的方式提交,遇到了很多很多问题.特地记录一下. 代码通过--master yarn-client提交是没有问题的,但是通过--master yar ...
- 大数据学习day33----spark13-----1.两种方式管理偏移量并将偏移量写入redis 2. MySQL事务的测试 3.利用MySQL事务实现数据统计的ExactlyOnce(sql语句中出现相同key时如何进行累加(此处时出现相同的单词))4 将数据写入kafka
1.两种方式管理偏移量并将偏移量写入redis (1)第一种:rdd的形式 一般是使用这种直连的方式,但其缺点是没法调用一些更加高级的api,如窗口操作.如果想更加精确的控制偏移量,就使用这种方式 代 ...
- 利用python代码获取文件特定的内容,并保存为文档
说明:有段时间需要读取上百个文件的单点能(sp),就写了下面的代码(计算化学狗努力转行中^-^) import os.path import re # 1 遍历指定目录,显示目录下的所有文件名 def ...
- 【leetocde】922. Sort Array By Parity II
Given an array of integers nums, half of the integers in nums are odd, and the other half are even. ...
- 【STM32】使用DMA+SPI传输数据
DMA(Direct Memory Access):直接存储器访问 一些简单的动作,例如复制或发送,就可以不透过CPU,从而减轻CPU负担 由于本人使用的是正点原子开发板,部分代码取自里面的范例 本篇 ...
- 写一个简单的AIDL
1.首先创建一个AIDL文件,并添加上两个接口.IMyAidlInterface.aidlpackage com.example.broadcastdemo;// Declare any non-de ...
- minSdkVersion、targetSdkVersion、targetApiLevel的区别
在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion="4" android:targetSdk ...
- python做一个http接口测试框架
目录结构 project case#测试用例 suite#测试目录 logs#测试日志 papi#测试类 result#测试结果 setting.py#配置文件 1.日志类,用于测试时日志记录 pya ...