LeetCode 题解工作台

驼峰式匹配

给你一个字符串数组 queries ,和一个表示模式的字符串 pattern ,请你返回一个布尔数组 answer 。只有在待查项 queries[i] 与模式串 pattern 匹配时, answer[i] 才为 true ,否则为 false 。 如果可以将 小写字母 插入模式串 pattern…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 双·指针·invariant

bolt

答案摘要

我们可以遍历 `queries` 中的每个字符串,判断其是否与 `pattern` 匹配,若匹配则将 `true` 加入答案数组,否则加入 `false`。 接下来,我们实现一个 $check(s, t)$ 函数,用于判断字符串 和 是否匹配。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 双·指针·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串数组 queries,和一个表示模式的字符串 pattern,请你返回一个布尔数组 answer 。只有在待查项 queries[i] 与模式串 pattern 匹配时, answer[i] 才为 true,否则为 false

如果可以将 小写字母 插入模式串 pattern 得到待查询项 queries[i],那么待查询项与给定模式串匹配。您可以在模式串中的任何位置插入字符,也可以选择不插入任何字符。

 

示例 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".

示例 2:

输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
输出:[true,false,true,false,false]
解释:
"FooBar" 可以这样生成:"Fo" + "o" + "Ba" + "r".
"FootBall" 可以这样生成:"Fo" + "ot" + "Ba" + "ll".

示例 3:

输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
输出:[false,true,false,false,false]
解释: 
"FooBarTest" 可以这样生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".

 

提示:

  • 1 <= pattern.length, queries.length <= 100
  • 1 <= queries[i].length <= 100
  • queries[i]pattern 由英文字母组成
lightbulb

解题思路

方法一:双指针

我们可以遍历 queries 中的每个字符串,判断其是否与 pattern 匹配,若匹配则将 true 加入答案数组,否则加入 false

接下来,我们实现一个 check(s,t)check(s, t) 函数,用于判断字符串 sstt 是否匹配。

我们可以使用双指针 iijj,分别指向两个字符串的首字符,然后遍历两个字符串。如果指针 iijj 指向的字符不同,并且 s[i]s[i] 为小写字母,则指针 ii 循环向后移动一位。

如果指针 ii 已经到达字符串 ss 的末尾,或者指针 iijj 指向的字符不同,则返回 false。否则,指针 iijj 同时向后移动一位。当指针 jj 到达字符串 tt 的末尾时,我们需要判断字符串 ss 中剩余的字符是否都为小写字母,若是则返回 true,否则返回 false

时间复杂度 (n×m)(n \times m),其中 nnmm 分别为数组 queries 的长度和字符串 pattern 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        def check(s, t):
            m, n = len(s), len(t)
            i = j = 0
            while j < n:
                while i < m and s[i] != t[j] and s[i].islower():
                    i += 1
                if i == m or s[i] != t[j]:
                    return False
                i, j = i + 1, j + 1
            while i < m and s[i].islower():
                i += 1
            return i == m

        return [check(q, pattern) for q in queries]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate effectively demonstrates two-pointer scanning and how it is used to match the query and pattern.

  • question_mark

    The candidate explains invariant tracking as a core concept for ensuring that the pattern's case constraints are respected during matching.

  • question_mark

    The candidate considers time and space efficiency when processing multiple queries, showing understanding of problem scale.

warning

常见陷阱

外企场景
  • error

    Failing to properly match uppercase characters, causing incorrect results when letters are skipped.

  • error

    Not accounting for the case sensitivity of the pattern, leading to false negatives when lowercase letters are inserted improperly.

  • error

    Mismanaging the two-pointer movement, resulting in unnecessary comparisons and inefficient matching logic.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Matching multiple queries with different pattern lengths, requiring a dynamic approach for varying input sizes.

  • arrow_right_alt

    Supporting more complex patterns that may contain both uppercase and lowercase letters interspersed within the pattern.

  • arrow_right_alt

    Incorporating additional constraints, such as ensuring that inserted characters are non-repetitive or constrained to certain ranges.

help

常见问题

外企场景

驼峰式匹配题解:双·指针·invariant | LeetCode #1023 中等