Given two string arrays word1
and word2
, return true
if the two arrays represent the same string, and false
otherwise.
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true.
Example 2:
Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false
Example 3:
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true
Constraints:
1 <= word1.length, word2.length <= 103
1 <= word1[i].length, word2[i].length <= 103
1 <= sum(word1[i].length), sum(word2[i].length) <= 103
word1[i]
and word2[i]
consist of lowercase 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 brute force approach to checking if two sets of word lists represent the same combined sentence is simple. We will build the full sentence from each list of words and then compare the two full sentences. If they are equal, the original word lists are equivalent.
Here's how the algorithm would work step-by-step:
def array_strings_are_equal(word_list1, word_list2):
# Build the first string from the first array.
first_combined_string = "".join(word_list1)
# Build the second string from the second array.
second_combined_string = "".join(word_list2)
# Compare the two strings to check for equality.
if first_combined_string == second_combined_string:
return True
else:
return False
Instead of actually combining the string arrays into single strings, the efficient approach compares them piece by piece. Imagine reading the two arrays simultaneously, checking if they match character by character as you 'read' them.
Here's how the algorithm would work step-by-step:
def arrayStringsAreEqual(word_array1, word_array2):
array1_index = 0
array2_index = 0
string1_index = 0
string2_index = 0
while array1_index < len(word_array1) and array2_index < len(word_array2):
# Compare characters at current indices
if word_array1[array1_index][string1_index] != word_array2[array2_index][string2_index]:
return False
string1_index += 1
if string1_index == len(word_array1[array1_index]):
# Move to the next string in word_array1
array1_index += 1
string1_index = 0
string2_index += 1
if string2_index == len(word_array2[array2_index]):
# Move to the next string in word_array2
array2_index += 1
string2_index = 0
# Check if both arrays have been fully traversed
if array1_index < len(word_array1) or array2_index < len(word_array2):
return False
return True
Case | How to Handle |
---|---|
Both input arrays are null | Return true because both represent an empty string. |
One array is null, the other is not | Return false because they cannot represent the same string. |
Both input arrays are empty | Return true because both represent an empty string. |
One array is empty, the other is not | Return false because they cannot represent the same string. |
One array contains empty strings, the other does not | The concatenation of the first array would be an empty string (''), while the second would not, so return false. |
Arrays contain very long strings that may exceed memory limits when concatenated | Iterate through strings instead of concatenating them to avoid memory overflow. |
Arrays contain Unicode or other special characters | Ensure the string comparison method correctly handles Unicode and special characters. |
Input arrays are extremely large, causing performance issues | The solution should have linear time complexity, making it scale well with large arrays. |