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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a string s and an array of strings words, determine whether s is a prefix string of words.
A string s is a prefix string of words if s can be made by concatenating the first k strings in words for some positive k no larger than words.length.
Return true if s is a prefix string of words, or false otherwise.
Example 1:
Input: s = "iloveleetcode", words = ["i","love","leetcode","apples"] Output: true Explanation: s can be made by concatenating "i", "love", and "leetcode" together.
Example 2:
Input: s = "iloveleetcode", words = ["apples","i","love","leetcode"] Output: false Explanation: It is impossible to make s using a prefix of arr.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 201 <= s.length <= 1000words[i] and s consist of only lowercase English letters.Problem summary: Given a string s and an array of strings words, determine whether s is a prefix string of words. A string s is a prefix string of words if s can be made by concatenating the first k strings in words for some positive k no larger than words.length. Return true if s is a prefix string of words, or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
"iloveleetcode" ["i","love","leetcode","apples"]
"iloveleetcode" ["apples","i","love","leetcode"]
count-prefixes-of-a-given-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1961: Check If String Is a Prefix of Array
class Solution {
public boolean isPrefixString(String s, String[] words) {
StringBuilder t = new StringBuilder();
for (var w : words) {
t.append(w);
if (t.length() > s.length()) {
return false;
}
if (t.length() == s.length()) {
return s.equals(t.toString());
}
}
return false;
}
}
// Accepted solution for LeetCode #1961: Check If String Is a Prefix of Array
func isPrefixString(s string, words []string) bool {
t := strings.Builder{}
for _, w := range words {
t.WriteString(w)
if t.Len() > len(s) {
return false
}
if t.Len() == len(s) {
return t.String() == s
}
}
return false
}
# Accepted solution for LeetCode #1961: Check If String Is a Prefix of Array
class Solution:
def isPrefixString(self, s: str, words: List[str]) -> bool:
n, m = len(s), 0
for i, w in enumerate(words):
m += len(w)
if m == n:
return "".join(words[: i + 1]) == s
return False
// Accepted solution for LeetCode #1961: Check If String Is a Prefix of Array
struct Solution;
impl Solution {
fn is_prefix_string(s: String, words: Vec<String>) -> bool {
let mut t = "".to_string();
for w in words {
t.push_str(&w);
if t == s {
return true;
}
}
false
}
}
#[test]
fn test() {
let s = "iloveleetcode".to_string();
let words = vec_string!["i", "love", "leetcode", "apples"];
let res = true;
assert_eq!(Solution::is_prefix_string(s, words), res);
let s = "iloveleetcode".to_string();
let words = vec_string!["apples", "i", "love", "leetcode"];
let res = false;
assert_eq!(Solution::is_prefix_string(s, words), res);
}
// Accepted solution for LeetCode #1961: Check If String Is a Prefix of Array
function isPrefixString(s: string, words: string[]): boolean {
const t: string[] = [];
const n = s.length;
let m = 0;
for (const w of words) {
m += w.length;
if (m > n) {
return false;
}
t.push(w);
if (m === n) {
return s === t.join('');
}
}
return false;
}
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.