LeetCode1160. 拼写单词

一. 题目
  1. 题目
    在这里插入图片描述

  2. 示例
    在这里插入图片描述

二. 方法一
  1. 解题思路

    1. 将chars字符串转化为Map
    2. 然后遍历words, 统计每一个元素所包含的字符是否在Map中
    3. 如果不在, 则直接跳过
    4. 如果在, 则将Map中该字符的数量-1, 如果字符数量小于0, 则跳过
    5. 到这一步, 则说明该元素的所有字符都在Map中, 且数量小于等于Map中该字符的数量
    6. 累加满足的元素的长度, 最后返回即可
  2. 解题代码

    def countCharacters(self, words: List[str], chars: str) -> int:
        map1 = {}
        count = 0
        # 可按照方法三优化
        # 因为这是我自己的思路写的, 所以就不做更改
        for ele in chars:
            if ele not in map1:
                map1[ele] = 1
            else:
                map1[ele] += 1
        
        for ele in words:
            flag = True
            temp = map1.copy()
            for e in ele:
                if e in temp:
                    temp[e] -= 1
                    if temp[e] < 0:
                        flag = False
                        break
                else:
                    flag = False
                    break
            if flag:
                count += len(ele)
    
        return count
    
  3. 分析
    时间复杂度: O(mn), m:列表中元素的个数, n:列表中元素的平均长度
    空间复杂度: O(n)

三. 方法二
  1. 解题思路

    1. 将列表中的元素和chars都转变为map
    2. 比较每个元素的字符是否都在chars中,
    3. 如果有字符不在, 则直接跳过该字符
    4. 如果都在, 则再比较元素字符个数是否小于或等于chars中相同元素的个数.
    5. 不满足则跳过, 满足累计其字符串长度
  2. 解题代码

    def countCharacters(self, words: List[str], chars: str) -> int:
        map1, map2 = {}, {}
        flag = True
        count = 0
        for ele in chars:
            if ele not in map1:
                map1[ele] = 1
            else:
                map1[ele] += 1
        for word in words:
            map2.clear()
            for ele in word:
                if ele not in map2:
                    map2[ele] = 1
                else:
                    map2[ele] += 1
            for key in map2.keys():
                if key in map1.keys() and map2[key] <= map1[key]:
                    flag = True
                else:
                    flag = False
                    break
            if flag:
                count += len(word)
        return count
    
  3. 分析
    时间复杂度: O(mn)
    空间复杂度: O(m + n)

四. 方法三: 方法二的优化(官方)
  1. 解题思路

    1. 解题思路和方法二是一样的, 但是调用了collections.Counter()方法, 使得代码更为简洁
    2. map中如果key不存在, 则查询到的value值为0, 所以不用额外判断key是否存在
  2. 解题代码

    def countCharacters(self, words: List[str], chars: str) -> int:
        count = 0
        chars_map = collections.Counter(chars)
        for word in words:
            word_map = collections.Counter(word)
            for key in word_map.keys():
                if word_map[key] > chars_map[key]:
                    break
            else:
                count += len(word)
        return count
    
  3. 分析
    时间复杂度: O(mn)
    空间复杂度: O(m + n)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值