gpt4 book ai didi

1023. Camelcase Matching 驼峰式匹配

转载 作者:大佬之路 更新时间:2024-01-31 14:15:41 26 4
gpt4 key购买 nike

题目地址:https://leetcode.com/problems/camelcase-matching/

题目描述

Aquery word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.)

Given a list of queries, and a pattern, return an answer list of booleans, where answer[i] is true if and only if queries[i] matches the pattern.

Example 1:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
Output: [true,false,true,true,false]
Explanation: 
"FooBar" can be generated like this "F" + "oo" + "B" + "ar".
"FootBall" can be generated like this "F" + "oot" + "B" + "all".
"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".

Example 2:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
Output: [true,false,true,false,false]
Explanation: 
"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".

Example 3:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
Output: [false,true,false,false,false]
Explanation: 
"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".

Note:

1、 1<=queries.length<=100
2、 1<=queries[i].length<=100
3、 1<=pattern.length<=100
4、 AllstringsconsistsonlyofloweranduppercaseEnglishletters.;

题目大意

判断每个query是不是能通过Pattern的大写字母中插入若干个(可以为0)个小写字母实现。

解题方法

正则+字典

这题的含义其实就是pattern按照大写字母分开,query也按大写字母分开,分开时要保证大写字母是每一段的第一个字符。然后要求pattern中的每一段都是query中每一段的subsequence。

把一个字符串分成一个大写+n个小写的子字符串的方式可以使用正则,表达式是"[A-Z][a-z]*".

判断subsequence需要使用遍历的方式求解,这里学习了lee215的一个写法,iter(qu)这样即可保证顺序查找、找到停止,然后等待下一次查找开始。

Python代码如下:

class Solution(object):
    def camelMatch(self, queries, pattern):
        """
        :type queries: List[str]
        :type pattern: str
        :rtype: List[bool]
        """
        import re
        ps = re.findall("[A-Z][a-z]*", pattern)
        N = len(queries)
        res = []
        for q in queries:
            qs = re.findall("[A-Z][a-z]*", q)
            hasFind = False
            if len(ps) == len(qs):
                if all(self.isSubSeq(q, p) for (q, p) in zip(qs, ps)):
                    hasFind = True
            res.append(hasFind)
        return res
    
    def isSubSeq(self, qu, pa):
        qu = iter(qu)
        return all(p in qu for p in pa)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

2022

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com