Advertisement

Camelcase Matching - LeetCode 1023 Solution

Camelcase Matching - Complete Solution Guide

Camelcase Matching is LeetCode problem 1023, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of strings queries and a string pattern , return a boolean array answer where answer[i] is true if queries[i] matches pattern , and false otherwise. A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all . Example 1: Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pa

Detailed Explanation

The problem asks us to determine, for each string in a given array of strings (`queries`), whether it matches a given `pattern` string. A string matches the pattern if it can be formed by inserting lowercase letters (zero or more) into the pattern. Crucially, uppercase letters in the query string *must* match corresponding letters in the pattern, and any additional uppercase letters in the query that are not in the pattern means the string *doesn't* match. The input consists of an array of query strings and a pattern string. The output should be a boolean array where each element represents whether the corresponding query string matches the pattern.

Solution Approach

The solution iterates through each query string and checks if it matches the pattern using a helper function (or a lambda in C++ and Python). The helper function uses two pointers: one for the query string and one for the pattern string. It iterates through the query string. If the current character in the query matches the current character in the pattern, both pointers are incremented. If the current character in the query is uppercase and *doesn't* match the pattern, then the query is not a match. If it matches, we increment our pattern pointer. If the current character in the query is lowercase, it's skipped. After iterating through the query string, it checks if the pattern pointer has reached the end of the pattern string. If it has, then the query matches the pattern; otherwise, it doesn't.

Step-by-Step Algorithm

  1. Step 1: Iterate through each query string in the input array.
  2. Step 2: For each query, initialize a pattern index to 0.
  3. Step 3: Iterate through the characters of the query string.
  4. Step 4: If the pattern index is within the bounds of the pattern length and the current character of the query matches the character at the pattern index in the pattern, increment the pattern index.
  5. Step 5: If the current character of the query is uppercase and does NOT match the character in pattern at the current pattern index (or if the pattern index has reached the end of the pattern string), the query does not match, so return false.
  6. Step 6: After the loop, check if the pattern index has reached the end of the pattern. If it has, then all the pattern's characters have been found in order, and the query matches. Return true.
  7. Step 7: Store the boolean result of the match in a result array and return.

Key Insights

  • Insight 1: The core idea is to iterate through each query string and the pattern simultaneously, checking for matching characters. If a character in the query is uppercase and doesn't match the current pattern character, the query doesn't match.
  • Insight 2: Lowercase characters in the query can be skipped over, as they represent the inserted lowercase letters. This allows for flexibility in how the query string is built from the pattern.
  • Insight 3: The pattern must be fully consumed for a query to be considered a match. If the pattern index hasn't reached the end of the pattern string, it means the query isn't long enough to fulfill the pattern.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(n)

Topics

This problem involves: Array, Two Pointers, String, Trie, String Matching.

Companies

Asked at: Compass.