【Leetcode_总结】1023. 驼峰式匹配 – python

  • Post author:
  • Post category:python


Q:

如果我们可以将

小写字母

插入模式串

pattern

得到待查询项

query

,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)

给定待查询列表

queries

,和模式串

pattern

,返回由布尔值组成的答案列表

answer

。只有在待查项

queries[i]

与模式串

pattern

匹配时,

answer[i]

才为

true

,否则为

false


示例 1:

输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
输出:[true,false,true,true,false]
示例:
"FooBar" 可以这样生成:"F" + "oo" + "B" + "ar"。
"FootBall" 可以这样生成:"F" + "oot" + "B" + "all".
"FrameBuffer" 可以这样生成:"F" + "rame" + "B" + "uffer".

连接:

https://leetcode-cn.com/problems/camelcase-matching/

思路:写的不好 乱糟糟

代码:

class Solution:
    def camelMatch(self, queries, pattern):
        res = ['#' for _ in range(len(queries))]
        for s in pattern:
            for i in range(len(queries)):
                if pattern[-1] in queries[i]:
                    end = len(queries[i]) - ''.join(list(queries[i])[::-1]).index(pattern[-1])
                    if s in queries[i] and queries[i].index(s) < end:
                        tmp = (queries[i])
                        tmp = (tmp.replace(str(s), '', 1))
                        queries[i] = tmp
                    else:
                        res[i] = False
                else:
                    res[i] = False

        for i in range(len(queries)):
            for w_ in queries[i]:
                if w_ >= 'A' and w_ < 'Z':
                    res[i] = False
                    break
            if res[i] == '#':
                res[i] = (True)
        return res



版权声明:本文为maka_uir原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。