题目要求

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. Oracle数据导入指定表空间

    1. 打开工具Oracle SQL Plus 以dba身份登录sys用户 sqlplus /nologconn sys@url as sysdba 2. 创建用户并指定表空间 使用客户端工具或者Web ...

  2. hdoj:2037

    #include <iostream> using namespace std; struct Time { int start; int end; }; Time times[]; ]; ...

  3. 字节顺序:高位优先(big-endian)和低位优先(little-endian)

    网络字节序: MSB 高字节前存法 Most Significant Bit   (Big Edian) 主机字节序: LSB 低字节前存法 Lest Significant Bit  (Little ...

  4. python3二元Logistics Regression 回归分析(LogisticRegression)

    纲要 boss说增加项目平台分析方法: T检验(独立样本T检验).线性回归.二元Logistics回归.因子分析.可靠性分析 根本不懂,一脸懵逼状态,分析部确实有人才,反正我是一脸懵 首先解释什么是二 ...

  5. 开源分布式日志系统ExceptionLess部署杂乱笔记 加密

    前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wiki .NET 4.6.1 这个因为我装了VS2015,就没有 ...

  6. [IR] Concept Search and PLSA

    [Topic Model]主题模型之概率潜在语义分析(Probabilistic Latent Semantic Analysis) 感觉LDA在实践中的优势其实不大,学好pLSA才是重点 阅读笔记 ...

  7. 使用gdbserver远程调试

    使用gdbserver远程调试   1.默认crosstool交叉编译器没有自带gdbserver,需要自行编译 到GNU官方FTP下载,目前最新版的是gdb-6.7.1下载地址:http://ftp ...

  8. error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2

    使用VS2013版本引用外部的lib进行编译时候提示: 错误 25 error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:  值“0”不匹配值“2”(jrtpl ...

  9. UML类图中的几种关系的画法和含义

    UML的类图中,一共有以下六大关系: 泛化(Generalization), 实现(Realization), 依赖(Dependence),关联(Association),聚合(Aggregatio ...

  10. maven子项目的springboot配置

    正常来说一个maven子项目的parent是父项目,而不是直接继承,这时候就需要改下配置 默认生成的代码入下: <?xml version="1.0" encoding=&q ...