题目要求

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. Android反编译和二次打包

    参考:APK反编译 一.工具介绍: 1.解压工具 2.JDK 3.apktool: aapt.exe,apktool.bat,apktool.jar;三个在同一目录结合使用,用来反编译apk,反编译生 ...

  2. 转载 :配置ssh密钥认证自动登录

    原文地址 :https://segmentfault.com/a/1190000000481249 在客户端来看,SSH提供两种级别的安全验证.[摘自wikipedia] 第一种级别(基于密码的安全验 ...

  3. 蓝牙mesh介绍

    了解一下关于蓝牙Mesh的知识. 蓝牙mesh网络使用,并且依赖于低功耗蓝牙(BLE).低功耗蓝牙技术是蓝牙mesh使用的无线通信协议栈. 蓝牙BR / EDR能够实现一台设备到另一台设备的连接和通信 ...

  4. linux命令后加一个 &

    默认情况下,进程是在前台运行的,这时就把shell给占据了,我们无法进行其它操作.对于那些没有交互的进程,很多时候,我们希望将其在后台启动,可以在启动参数的时候加一个'&'实现这个目的. ti ...

  5. 转自ruby迷: 使用Net::SSH和Net::SCP编写Linux服务器管理脚本

    试了一下perl下安装ssh模块,整了半天linux/window上都装不上,各依赖模块的版本总是匹配不上,后改了一下思路,用ruby吧 Net::SSH和Net::SCP是两个Ruby操作SSH的g ...

  6. Linux内核 设备树操作常用API

    Linux设备树语法详解一文中介绍了设备树的语法,这里主要介绍内核中提供的操作设备树的API,这些API通常都在"include/of.h"中声明. device_node 内核中 ...

  7. mysql distinct 后面没有括号

    当distinct应用到多个字段的时候,其应用的范围是其后面的所有字段,而不只是紧挨着它的一个字段,而且distinct只能放到所有字段的前面 如下语句是错误的: SELECT country, di ...

  8. base标签浏览器兼容问题

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  9. ELK之elasticsearch集群搭建

    安装配置elasticsearch不详述 环境:主elasticsearch IP 172.16.90.11 备elasticsearch IP  172.16.90.12 修改配置文件 /etc/e ...

  10. acm入门练习心得

    A题  两数相加等于n相乘等于m 问是否存在两数 写了超级久没有写出来,因为我的思路是 把m的因子都找出来,加加看,看等不等于n.后来发现m的因子还要考虑负数的情况,还要考虑m是不是负数的情况.太过麻 ...