最短补全词

题目描述

来源:力扣(LeetCode)

给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。

补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词 。

在匹配 licensePlate 中的字母时:

忽略 licensePlate 中的 数字和空格 。
不区分大小写。
如果某个字母在 licensePlate 中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。
例如:licensePlate = “aBc 12c”,那么它的补全词应当包含字母 ‘a’、‘b’ (忽略大写)和两个 ‘c’ 。可能的 补全词 有 “abccdef”、“caaacab” 以及 “cbca” 。

请你找出并返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words 中 最靠前的 那个。

示例 1:

输入:licensePlate = “1s3 PSt”, words = [“step”, “steps”, “stripe”, “stepple”]
输出:“steps”
解释:最短补全词应该包括 “s”、“p”、“s”(忽略大小写) 以及 “t”。
“step” 包含 “t”、“p”,但只包含一个 “s”,所以它不符合条件。
“steps” 包含 “t”、“p” 和两个 “s”。
“stripe” 缺一个 “s”。
“stepple” 缺一个 “s”。
因此,“steps” 是唯一一个包含所有字母的单词,也是本例的答案。

思路

  • 过滤

首先过滤掉给的目标字符串中的数字和空格,并将所有的大写字母转换为小写。

  • 拆分统计字母频次

然后拆分单词、字符串统计字母出现的频次。

  • 确定符合条件的单词

循环遍历单词数组,确定符合条件的单词,并记录下单词长度。

  • 找到最早出现最短的那个单词

循环遍历符合条件的单词组,找到最早出现并且最短的那个单词,最后返回结果。

python代码

import re

class Solution(object):
    def shortestCompletingWord(self, licensePlate, words):
        """
        :type licensePlate: str
        :type words: List[str]
        :rtype: str
        """
        filter_num = re.sub(r'[0-9]' ,'' ,licensePlate)
        filter_space = re.sub(r' ' ,'' ,filter_num)
        result = {}
        target = self.letter_count(filter_space.lower())
        # 遍历words
        for word in words:
            if self.dict_is_meet(target,self.letter_count(word)):
                result.setdefault(word,len(word))

        # 打印
        print(f'{str(result)}')

        # 排序
        return self.dict_sorted(result)

    # 排序
    def dict_sorted(self,dict):
        list = []
        for item in dict.items():
            if len(list) == 0:
                list.append(item)
            if item[1] < list[0][1]:
                list.clear()
                list.append(item)
        return list[0][0]

    # 判断字典是否符合条件
    def dict_is_meet(self,dict1,dict2):
        for key in dict1.keys():
            if dict2.get(key) == None or dict1.get(key) != dict2.get(key):
                return 0
        return 1

    # 单词拆分为字母计数
    def letter_count(self,word):
        dict = {}
        # 字母循环计数
        for letter in word:
            if self.has_key(letter, dict):
                dict[letter] = dict.get(letter) + 1
            else:
                dict.setdefault(letter, 1)
        return dict

    # 判断字典中是否有该键
    def has_key(self,key,dict):
        for k in dict.keys():
            if key == k:
                return 1
        return 0


if __name__ == '__main__':
    licensePlate = "1s3 456"
    words = ["looks","pest","stew","show"]

    s = Solution()
    result = s.shortestCompletingWord(licensePlate, words)
    print(result)
  

问题

在对字典进行遍历寻找最后符合条件的单词时,方法dict.items()在本地和网页上运行的结果不同,可能遍历的顺序不同,需要注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值