1662. Check If Two String Arrays are Equivalent - Explanation
Description
Concatenate the strings in word1 in their given order, and do the same for word2. Return true if the two resulting strings are identical, or false otherwise. The boundaries between the individual pieces do not affect the comparison.
Example 1:
Input: word1 = ["ne","et"]
word2 = ["n","eet"]
Output: trueExplanation: Both arrays form "neet". Their pieces have different boundaries, but the resulting characters match exactly.
Example 2:
Input: word1 = ["ab","cd"]
word2 = ["ab","ce"]
Output: falseExplanation: The first array forms "abcd", while the second forms "abce". Their last characters differ, so return false.
Example 3:
Input: word1 = ["abc"]
word2 = ["ab"]
Output: falseExplanation: The arrays form "abc" and "ab". One has an extra character, so they are not equal.
Constraints:
- Each array contains between
1and1000strings. - Each string contains between
1and1000lowercase English letters. - The total number of characters in each array is at most
1000.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- String Manipulation - Concatenating strings and comparing string contents
- Two Pointers - Using multiple index variables to traverse data structures simultaneously
- Array Iteration - Traversing arrays of strings and individual characters within strings
1. Concatenate Strings
Intuition
The simplest approach is to concatenate all strings in each array into a single string, then compare the two resulting strings. If they match character by character, the arrays represent the same string.
Algorithm
- Join all strings in
word1into a single string. - Join all strings in
word2into a single string. - Compare the two concatenated strings.
- Return
trueif they are equal,falseotherwise.
Time & Space Complexity
- Time complexity:
- Space complexity:
Where and are the total number of characters in both the arrays and , respectively.
2. Concatenate Strings Of One Array
Intuition
We can reduce space usage by only concatenating one of the arrays. We then iterate through the second array character by character, comparing each character against the concatenated string. This way, we only build one full string instead of two.
Algorithm
- Concatenate all strings in
word1into a single strings1. - Initialize an index pointer
ito0. - Iterate through each string in
word2, then through each character in that string. - For each character, check if the index has exceeded
s1's length or if the characters do not match. - If either condition is true, return
falseimmediately. - Increment the index after each successful comparison.
- After processing all of
word2, returntrueonly ifiequals the length ofs1(ensuring both have the same length).
Time & Space Complexity
- Time complexity:
- Space complexity:
Where and are the total number of characters in both the arrays and , respectively.
3. Two Pointers
Intuition
We can avoid creating any concatenated strings by using four pointers: two to track which string we are currently in (one for each array), and two to track the character position within those strings. We compare characters one at a time, advancing through both arrays simultaneously.
Algorithm
- Initialize four pointers:
w1andw2for the current word index in each array,iandjfor the character position within the current words. - While both arrays have more content to process:
- Compare the current characters from both arrays.
- If they differ, return
false. - Advance both character pointers.
- If a character pointer reaches the end of its current word, move to the next word and reset the character pointer to
0.
- After the loop, check that both arrays have been fully processed (both word pointers have reached the end).
- Return
trueif both arrays are exhausted,falseotherwise.
class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
w1 = w2 = 0 # Index of word
i = j = 0 # Index of character
while w1 < len(word1) and w2 < len(word2):
if word1[w1][i] != word2[w2][j]:
return False
i, j = i + 1, j + 1
if i == len(word1[w1]):
w1 += 1
i = 0
if j == len(word2[w2]):
w2 += 1
j = 0
return w1 == len(word1) and w2 == len(word2)Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
Where and are the total number of characters in both the arrays and , respectively.
Common Pitfalls
Forgetting the Final Length Check
When iterating through one array while comparing to the concatenated string of the other, returning true after matching all characters without verifying both strings have the same length. If word2 is shorter than word1, all characters may match but the strings are not equivalent.
# Wrong: missing final length check
return True
# Correct: verify all characters were consumed
return i == len(s1)Comparing Array Lengths Instead of Total Characters
Assuming arrays with the same number of elements will produce equivalent strings. The arrays ["ab", "c"] and ["a", "bc"] have different lengths but represent the same string "abc".
Sign in to join the discussion