Camelcase matching

November 21, 2022

two-pointers queue

Problem URL: Camelcase matching

We will create a helper function to check if a string is a subsequence of another string. Then we will iterate over the queries and check if the query is a subsequence of the pattern. If it is, then we will append it to the result.

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        def match(item):
            queue = collections.deque([c for c in pattern])
            for c in item:
                if queue and c == queue[0]:
                    queue.popleft()
                elif c.isupper():
                    return False
            return not queue

        return [True if match(item) else False for item in queries]

Time complexity: O(n)
Space complexity: O(n)