1023. Camelcase Matching

要达到题目的要求: query 中的 word 能否通过对 pattern 添加小写字母得到。

分为两个步骤去判断是否成立:

1. word 中 应包含这个 pattern 子串

2. 如果只能通过添加小写字母得到,那么 word 和 pattern 中的大写字母应该一样  

 

题解中的:

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        p = tuple(c for c in pattern if c.isupper())
        def f(words):
            i = 0
            for c in pattern:
                j = words.find(c, i)
                if j == -1:
                    return False
                i = j + 1
            return p == tuple(c for c in words if c.isupper())
        return [*map(f, queries)]

我照着题解的思路又写了一遍的,不过在这个过程中我不小心把tuple 写成了 list,结果仍是正确的 

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        p = [c for c in pattern if c.isupper()]
        def f(word):
            i = 0
            for c in pattern:
                y = word.find(c, i)
                if y == -1:
                    return False
                i = y + 1
            return p == [c for c in word if c.isupper()]
        return [*map(f, queries)]

 

 

如果去掉return 语句中 map前面的 * 号,输出的结果如下图:

 

 

 

 

 

 

在本地 ide 调试的时候, 提示:

1023. Camelcase Matching_第1张图片

百度之:是typing模块可以进行类型检查,在代码开头添加 from typing import list

1023. Camelcase Matching_第2张图片

 

 

what‘s more ,python3 的函数注释: 

1023. Camelcase Matching_第3张图片 

 

你可能感兴趣的:(呦吼,i,don't,know,哦豁)