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 <= 100
1 <= words[i].length <= 20
1 <= s.length <= 1000
words[i]
and s
consist of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to see if a given long word can be formed by sticking together the beginning words from a collection of smaller words. We'll try all possible combinations of starting words to see if any match the long word exactly.
Here's how the algorithm would work step-by-step:
def is_prefix_of_array(long_word, array_of_words):
combined_word = ""
for word in array_of_words:
combined_word += word
# Check if the combined word matches the long word
if combined_word == long_word:
return True
# If the combined word is longer, it can't be a prefix
if len(combined_word) > len(long_word):
return False
# If we get here, no prefix was found
return False
We want to see if our target string can be formed by combining the beginning elements of a given list of words. The efficient way is to build a string from the word list and compare it to the target, stopping early if we find a mismatch or a match.
Here's how the algorithm would work step-by-step:
def is_prefix_string(target_string, array_of_words):
string_builder = ""
for word in array_of_words:
string_builder += word
# Early return if we have an exact match
if string_builder == target_string:
return True
# Return early if the prefix exceeds the target
if len(string_builder) > len(target_string):
return False
# Target can't be formed if we exhaust the word list
return False
Case | How to Handle |
---|---|
Null or empty string s | Return false immediately, as an empty string cannot be a prefix. |
Null or empty array words | Return false immediately, as an empty array cannot create a prefix. |
String s is empty but the array is not | Return true, since an empty string is a prefix of any string. |
String s is very long (e.g., exceeds memory limits) | The algorithm should process s incrementally to avoid loading the entire string into memory at once. |
The string s is shorter than the first word in words | Return false, because the first word alone is longer than s. |
The words array contains very long strings | Handle strings of substantial length to prevent potential memory overflow or performance issues during concatenation and comparison. |
The string s is formed exactly using some words but not the whole array | The solution should return true only if s is formed by the prefix of the array words. |
No valid solution exists because the words array cannot form s | The solution should correctly return false if the combined words can never form s. |