↓Skip to main content
  1. LeetCode/

LeetCode 1023: Camelcase Matching

·2 mins· ·
LeetCode Medium Two-Pointers String Trie
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-03-19

  • Submissions: Accepted (first try)

Approach
#

A query matches if lowercase letters can be inserted into the pattern to form it. In other words:

  1. The pattern is a subsequence of the query.
  2. Every query character outside that subsequence is lowercase.

Compare with two pointers:

  • Same character: advance both.
  • Different and the query character is uppercase: uppercase cannot be inserted, so fail.
  • Different and lowercase: treat it as inserted and advance only the query pointer.

At the end the pattern must be used up, and anything left in the query must be lowercase.

LeetCode tags this as Trie, but each query is compared with one pattern, so two pointers are enough.

My Solution
#

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        def match(q: str) -> bool:
            j = 0
            for ch in q:
                if j < len(pattern) and ch == pattern[j]:
                    j += 1
                elif ch.isupper():
                    return False
            return j == len(pattern)

        return [match(q) for q in queries]

My original version ran the two pointers in a while loop and then checked separately that query[r:] was all lowercase. Scanning the whole query with for folds “the rest must be lowercase” into the loop.

Complexity
#

Time O(total length of all queries), space O(1) excluding the output.

Mistakes to Avoid
#

  • Checking only that the pattern is a subsequence and forgetting that extra characters cannot be uppercase.
  • Not checking the rest of the query after the pattern is used up.

Interview Explanation
#

A query matches when the pattern is a subsequence of it and every extra character is lowercase. For each query I scan once: on a match with the pattern’s current character I advance the pattern pointer, and on a mismatch with an uppercase character I fail immediately. The pattern must be fully used at the end. Each query is linear, and no Trie is needed.

Related

LeetCode 3775: Reverse Words With Same Vowel Count
·1 min
LeetCode Medium String Simulation Two-Pointers
LeetCode 3775 note: count the first word’s vowels, then reverse every later word with the same count
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one
LeetCode 2337: Move Pieces to Obtain a String
·2 mins
LeetCode Medium Two-Pointers String
LeetCode 2337 note: after skipping blanks the piece order must match; L only moves left and R only moves right, so each L’s start index must be >= its target index and each R’s must be <=
LeetCode 777: Swap Adjacent in LR String
·2 mins
LeetCode Medium Two-Pointers String
LeetCode 777 note: XL→LX moves L left and RX→XR moves R right; after skipping X the order must match, with L only moving left and R only moving right
LeetCode 809: Expressive Words
·3 mins
LeetCode Medium Two-Pointers String Array
LeetCode 809 note: two pointers compare runs of equal characters; when the lengths differ, the run in s must be at least 3 and no shorter than the word’s run, and both strings must end together
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking