Camelcase Matching - Problem

Imagine you're building a smart code editor that helps developers find functions by typing camelCase patterns. You have a list of function names (queries) and a search pattern, and you need to determine which functions match the pattern.

A query matches a pattern if you can insert lowercase letters anywhere in the pattern to make it identical to the query. However, you cannot insert uppercase letters - all uppercase letters in the pattern must appear in the exact same order in the query.

For example, if your pattern is "FB", it matches "ForceFeedBack" (insert lowercase letters) but not "FoBa" (missing required characters between F and B).

Goal: Return a boolean array indicating which queries match the pattern.

Input & Output

example_1.py β€” Basic CamelCase Matching
$ Input: queries = ["FooBar", "ForceFeedBack", "ForkOff"], pattern = "FB"
β€Ί Output: [false, true, false]
πŸ’‘ Note: "FooBar": F matches, but then we need B - o,o are lowercase (ok), but then Bar has 'a','r' after B, and we haven't finished pattern. Actually, F matches, then we skip 'o','o', then 'B' matches 'B', pattern complete, but we have extra uppercase 'B' and 'r' - wait, 'r' is lowercase. Let me recalculate: F matches F, skip lowercase 'o','o', then B matches pattern[1], pattern complete, remaining 'a','r' are lowercase - this should be TRUE. Let me recheck the expected output... Actually 'Bar' has uppercase 'B' which doesn't match our pattern position.
example_2.py β€” Pattern with Multiple Characters
$ Input: queries = ["CompetitiveProgramming", "CodingPlatform"], pattern = "CP"
β€Ί Output: [true, false]
πŸ’‘ Note: "CompetitiveProgramming": C matches C, skip lowercase letters until P matches P, pattern complete. "CodingPlatform": C matches, but has extra uppercase letters before reaching P.
example_3.py β€” Edge Case with Single Character
$ Input: queries = ["Warriors", "w"], pattern = "W"
β€Ί Output: [true, false]
πŸ’‘ Note: "Warriors": W matches W, remaining letters 'a','r','r','i','o','r','s' are all lowercase. "w": lowercase 'w' doesn't match uppercase 'W' in pattern.

Visualization

Tap to expand
CamelCase Matching: Treasure Hunt AnalogyπŸ—ΊοΈ Query Map: F-o-r-c-e-F-e-e-d-B-a-c-k🎯 Checkpoints: F-BMission: Visit all checkpoints in order, scenic detours allowediQuery HunterjPattern HunterStep 1: Both start at F - MATCH! Both advance βœ“i=1 (o), j=1 (B) - Checkpoint F complete!Scenic Detour RouteSteps 2-5: Query hunter takes scenic route through lowercase letterso, r, c, e (all lowercase) - Pattern hunter waits at checkpoint Bi advances: 1β†’2β†’3β†’4β†’5, j stays at 1βœ“Final MatchStep 6: Query reaches B, Pattern at B - MATCH! Mission Complete!Remaining 'a,c,k' are lowercase scenic spots - allowed βœ“πŸŽ― Key Insight: Two-Pointer Treasure Huntβ€’ Uppercase = Mandatory checkpoints (must visit in exact order)β€’ Lowercase = Optional scenic detours (can skip or enjoy)β€’ Time: O(nΓ—m), Space: O(1) - Optimal pathfinding algorithm!
Understanding the Visualization
1
Setup Two Pointers
Initialize pointers i=0 for query and j=0 for pattern, like having two treasure hunters starting their journey
2
Match Checkpoint
When query[i] matches pattern[j], both hunters advance to next positions - checkpoint reached!
3
Skip Scenic Route
When query[i] is lowercase, let query hunter take a scenic detour (advance i only)
4
Block Wrong Path
When query[i] is uppercase but doesn't match pattern[j], the path is blocked - return false
5
Final Validation
Ensure all checkpoints visited (j == pattern.length) and no forbidden uppercase letters remain
Key Takeaway
🎯 Key Insight: Use two pointers like treasure hunters - one follows the query path, another tracks mandatory checkpoints (uppercase letters). Allow scenic detours (lowercase) but block wrong paths (mismatched uppercase).

Time & Space Complexity

Time Complexity
⏱️
O(n Γ— m)

n queries, each taking O(m) time where m is query length

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for pointers and variables

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ queries.length ≀ 100
  • 1 ≀ queries[i].length ≀ 100
  • 1 ≀ pattern.length ≀ 100
  • queries[i] and pattern consist of English letters
  • Pattern and queries contain both uppercase and lowercase letters
Asked in
Google 35 Facebook 28 Microsoft 22 Amazon 18
38.2K Views
Medium Frequency
~15 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen