Camelcase Matching - Problem

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.

Input & Output

Example 1 — Basic Matching
$ Input: queries = ["ForceFeedBack","ForYou","Fun"], pattern = "FB"
Output: [true,false,false]
💡 Note: ForceFeedBack can be formed by inserting lowercase letters into FB (F+orce+B+ack). ForYou cannot because it has Y (uppercase) not in pattern. Fun cannot because it lacks B.
Example 2 — Exact Match
$ Input: queries = ["CompetitiveProgramming","FaceBook"], pattern = "CamelCase"
Output: [false,false]
💡 Note: CompetitiveProgramming has P (uppercase) not in pattern. FaceBook has uppercase letters F,B that don't match pattern CamelCase.
Example 3 — Edge Case
$ Input: queries = ["AA","aaa"], pattern = "AAA"
Output: [false,false]
💡 Note: AA is missing one A from pattern AAA. aaa has all lowercase but pattern requires uppercase A's.

Constraints

  • 1 ≤ queries.length ≤ 100
  • 1 ≤ queries[i].length ≤ 100
  • 1 ≤ pattern.length ≤ 100
  • queries[i] and pattern consist of English letters

Visualization

Tap to expand
Camelcase Matching INPUT queries[]: "ForceFeedBack" "ForYou" "Fun" pattern: "FB" Pattern chars to match: F B (uppercase) ALGORITHM STEPS 1 Two Pointer Approach i for query, j for pattern 2 Match Characters If query[i] == pattern[j], advance both pointers 3 Skip Lowercase If query[i] is lowercase, can skip (insert allowed) 4 Reject Extra Uppercase If unmatched uppercase in query --> false Example: "ForceFeedBack" F-o-r-c-e-F-e-e-d-B-a-c-k F---------F-------B------ Pattern F,B found in order Extra chars are lowercase: OK FINAL RESULT "ForceFeedBack" F...F...B matches "FB" Only lowercase extras true "ForYou" Has F but Y is uppercase Y not in pattern --> reject false "Fun" Has F but no B Pattern incomplete --> reject false Output: [true, false, false] Key Insight: A query matches pattern if: (1) all pattern characters appear in order in query, and (2) any extra characters in query between pattern matches are lowercase only. Time: O(n*m) where n = queries length, m = max query length | Space: O(1) TutorialsPoint - Camelcase Matching | Optimal Two-Pointer Solution
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
32.0K Views
Medium Frequency
~25 min Avg. Time
890 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