题目要求

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.

题目分析及思路

给定一组字符串,要求找到符合要求的最短词。要求是该词必须含有所有来自字符串licensePlate中的字母,不区分大小写;且如果licensePlate中同一个字母出现了多次,则符合要求的词中的该字母也需要出现同样次数。保证一定至少存在一个结果,且如果有多个结果的话,则返回在字符串数组中第一次出现的那个结果。可以先获得licensePlate中所有的字母,且都转换成小写得到letters。之后遍历字符串数组中的每一个字符串中的每一个字母,去匹配letters中的字母。若该词符合要求且结果数组为空,则将该词放进结果数组;若数组不为空且该词的长度小于结果数组中词的长度,则用该词替换掉结果数组的词。最后返回结果数组中的第一个结果。

python代码

class Solution:

def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:

letters = [c.lower() for c in licensePlate if c.isalpha()]

res = []

for word in words:

for c in word:

if c in letters:

letters.remove(c)

if len(letters) == 0:

if not res:

res.append(word)

else:

if len(word) < len(res[-1]):

res.pop()

res.append(word)

letters = [c.lower() for c in licensePlate if c.isalpha()]

return res[0]

LeetCode 748 Shortest Completing Word 解题报告的更多相关文章

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

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

  2. leetcode 748. Shortest Completing Word

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

  3. 【Leetcode_easy】748. Shortest Completing Word

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

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

  5. 748. Shortest Completing Word

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

  6. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. 推荐几个Windows工具软件: Stickies - 桌面贴

    主页: http://www.zhornsoftware.co.uk/stickies/index.html Stickies work like Post-it notes for your PC. ...

  2. Java多线程系列——信号量:Semaphore

    简介 信号量为多线程协作提供了更为强大的控制方法.也可以说,信号量是对锁的扩展.无论是内部锁 synchronized 还是重入锁 ReentrantLock,一次都只允许一个线程访问一个资源,而信号 ...

  3. Linux初始化init系统-Sysvinit、Upstart、Systemd

    首先了解以下Ubuntu运行级别(init)对应工具的变化历史: 1.Ubuntu 6.10及以前版本使用Sysvinit. 2.Ubuntu 14.10及以前版本使用Upstart但是还留着Sysv ...

  4. Excel中substitute替换函数的使用方法

    问题现象:在Excel中,对几千条数据按照时间顺序排序,但总是有部分数据不参与排序,单纯用单元格调整不起任何作用. 解决办法: 数据排列问题最重要的是数据格式的一致性.解决这个问题,建议按如下步骤: ...

  5. linux下用/proc/stat文件来计算cpu的利用率-c语言实现

    proc文件系统介绍 /proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为内核与进程提供通信的接口.用户和应用程序可以通过/proc得到系统的信息,并可以改变内 ...

  6. Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)

    一.使用zookeeper管理远程Mycat配置文件 环境准备: 虚拟机192.168.152.130: zookeeper,具体参考前面文章 搭建dubbo+zookeeper+dubboadmin ...

  7. (原)关于i++和++i的小程序测试

    今天看到一个程序,于是用vs运行了一下,结果出乎我的意料: 代码: ; +(i++))+(+(i++)); i=; +(++i))+(+(++i)); i=; printf("x=%d,y= ...

  8. python 简单的server请求

    1.代码 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # __author henry # __date 2018/11/4 from wsgiref ...

  9. Mac 卸载 Jenkins

    Jenkins的安装方式不同(Mac 安装 Jenkins),卸载方法也不同. 一.通过安装包安装的卸载方式 打开终端,执行 ~ /Library/Application\ Support/Jenki ...

  10. WCF ChannelFactory<T> WCF Channel and ChannelFactory Caching

    https://stackoverflow.com/questions/3200197/creating-wcf-channelfactoryt?rq=1 https://stackoverflow. ...