Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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"], pattern = "FB" Output: [true,false,true,true,false] Explanation: "FooBar" can be generated like this "F" + "oo" + "B" + "ar". "FootBall" can be generated like this "F" + "oot" + "B" + "all". "FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
Example 2:
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa" Output: [true,false,true,false,false] Explanation: "FooBar" can be generated like this "Fo" + "o" + "Ba" + "r". "FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
Example 3:
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT" Output: [false,true,false,false,false] Explanation: "FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
Constraints:
1 <= pattern.length, queries.length <= 1001 <= queries[i].length <= 100queries[i] and pattern consist of English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Trie · String Matching
["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"] "FB"
["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"] "FoBa"
["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"] "FoBaT"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1023: Camelcase Matching
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> ans = new ArrayList<>();
for (var q : queries) {
ans.add(check(q, pattern));
}
return ans;
}
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
int i = 0, j = 0;
for (; j < n; ++i, ++j) {
while (i < m && s.charAt(i) != t.charAt(j) && Character.isLowerCase(s.charAt(i))) {
++i;
}
if (i == m || s.charAt(i) != t.charAt(j)) {
return false;
}
}
while (i < m && Character.isLowerCase(s.charAt(i))) {
++i;
}
return i == m;
}
}
// Accepted solution for LeetCode #1023: Camelcase Matching
func camelMatch(queries []string, pattern string) (ans []bool) {
check := func(s, t string) bool {
m, n := len(s), len(t)
i, j := 0, 0
for ; j < n; i, j = i+1, j+1 {
for i < m && s[i] != t[j] && (s[i] >= 'a' && s[i] <= 'z') {
i++
}
if i == m || s[i] != t[j] {
return false
}
}
for i < m && s[i] >= 'a' && s[i] <= 'z' {
i++
}
return i == m
}
for _, q := range queries {
ans = append(ans, check(q, pattern))
}
return
}
# Accepted solution for LeetCode #1023: Camelcase Matching
class Solution:
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
def check(s, t):
m, n = len(s), len(t)
i = j = 0
while j < n:
while i < m and s[i] != t[j] and s[i].islower():
i += 1
if i == m or s[i] != t[j]:
return False
i, j = i + 1, j + 1
while i < m and s[i].islower():
i += 1
return i == m
return [check(q, pattern) for q in queries]
// Accepted solution for LeetCode #1023: Camelcase Matching
struct Solution;
impl Solution {
fn camel_match(queries: Vec<String>, pattern: String) -> Vec<bool> {
queries
.into_iter()
.map(|query| Self::query_match(query.chars().collect(), pattern.chars().collect()))
.collect()
}
fn query_match(query: Vec<char>, pattern: Vec<char>) -> bool {
let mut j = 0;
for i in 0..query.len() {
if j < pattern.len() && query[i] == pattern[j] {
j += 1;
} else {
if query[i].is_uppercase() {
return false;
}
}
}
j == pattern.len()
}
}
#[test]
fn test() {
let queries = vec_string![
"FooBar",
"FooBarTest",
"FootBall",
"FrameBuffer",
"ForceFeedBack"
];
let pattern = "FB".to_string();
let res = vec![true, false, true, true, false];
assert_eq!(Solution::camel_match(queries, pattern), res);
let queries = vec_string![
"FooBar",
"FooBarTest",
"FootBall",
"FrameBuffer",
"ForceFeedBack"
];
let pattern = "FoBa".to_string();
let res = vec![true, false, true, false, false];
assert_eq!(Solution::camel_match(queries, pattern), res);
let queries = vec_string![
"FooBar",
"FooBarTest",
"FootBall",
"FrameBuffer",
"ForceFeedBack"
];
let pattern = "FoBaT".to_string();
let res = vec![false, true, false, false, false];
assert_eq!(Solution::camel_match(queries, pattern), res);
}
// Accepted solution for LeetCode #1023: Camelcase Matching
function camelMatch(queries: string[], pattern: string): boolean[] {
const check = (s: string, t: string) => {
const m = s.length;
const n = t.length;
let i = 0;
let j = 0;
for (; j < n; ++i, ++j) {
while (i < m && s[i] !== t[j] && s[i].codePointAt(0) >= 97) {
++i;
}
if (i === m || s[i] !== t[j]) {
return false;
}
}
while (i < m && s[i].codePointAt(0) >= 97) {
++i;
}
return i == m;
};
const ans: boolean[] = [];
for (const q of queries) {
ans.push(check(q, pattern));
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.