Check If String Is a Prefix of Array
EasyUpdated: Jul 4, 2025
Practice on:
Problem
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 ifs _is aprefix string of _words , orfalse otherwise.
Examples
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]andsconsist of only lowercase English letters.
Solution
Method 1 – Two Pointers (Prefix Check)
Intuition
We want to check if the string s can be formed by concatenating the first k words from the array. This is a straightforward prefix check using two pointers: one for the string and one for the array.
Reasoning
By iterating through the words and matching their characters with the corresponding characters in s, we can verify if s is a prefix. If we reach the end of s exactly after some words, then s is a prefix of the array.
Approach
- Initialize a pointer
ifor the stringsat 0. - Iterate through each word in
words:- For each character in the word, check if it matches
s[i]. - If any character does not match or if
iexceeds the length ofs, return false. - Increment
ifor each matched character. - If
ireaches the length ofs, return true (all ofsmatched).
- For each character in the word, check if it matches
- If the loop ends and
iis not equal to the length ofs, return false.
Edge cases:
sis shorter than the concatenation of all words.sis longer than the concatenation of all words.
Code
C++
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
int i = 0;
for (const string& w : words) {
for (char c : w) {
if (i == s.size() || s[i] != c) return false;
++i;
}
if (i == s.size()) return true;
}
return i == s.size();
}
};
Go
func isPrefixString(s string, words []string) bool {
i := 0
for _, w := range words {
for _, c := range w {
if i == len(s) || s[i] != byte(c) {
return false
}
i++
}
if i == len(s) {
return true
}
}
return i == len(s)
}
Java
class Solution {
public boolean isPrefixString(String s, String[] words) {
int i = 0;
for (String w : words) {
for (char c : w.toCharArray()) {
if (i == s.length() || s.charAt(i) != c) return false;
i++;
}
if (i == s.length()) return true;
}
return i == s.length();
}
}
Kotlin
class Solution {
fun isPrefixString(s: String, words: Array<String>): Boolean {
var i = 0
for (w in words) {
for (c in w) {
if (i == s.length || s[i] != c) return false
i++
}
if (i == s.length) return true
}
return i == s.length
}
}
Python
class Solution:
def is_prefix_string(self, s: str, words: list[str]) -> bool:
i = 0
for w in words:
for c in w:
if i == len(s) or s[i] != c:
return False
i += 1
if i == len(s):
return True
return i == len(s)
Rust
impl Solution {
pub fn is_prefix_string(s: String, words: Vec<String>) -> bool {
let mut i = 0;
let s_bytes = s.as_bytes();
for w in words {
for &c in w.as_bytes() {
if i == s_bytes.len() || s_bytes[i] != c {
return false;
}
i += 1;
}
if i == s_bytes.len() {
return true;
}
}
i == s_bytes.len()
}
}
Complexity
- ⏰ Time complexity:
O(n), as we scan each character ofsat most once, wherenis the length ofs. - 🧺 Space complexity:
O(1), as we use only a few variables for tracking indices.