Practice Check If Two String Arrays are Equivalent
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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.
Source: Check If Two String Arrays are Equivalent on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
word1 = ["ab", "c"], word2 = ["a", "bc"]
true
Joining the first array produces "ab" + "c" = "abc". Joining the second produces "a" + "bc" = "abc" as well, so the answer is true. The boundaries between array elements do not matter. Comparing elements at matching array indices would incorrectly reject this example; compare the resulting character sequences, either by concatenating them or by advancing character pointers across the pieces.
# Example 2
word1 = ["a", "cb"], word2 = ["ab", "c"]
false
The first array represents "acb", while the second represents "abc". Their second characters differ: c versus b. They therefore represent different strings even though both have length three and contain the same letters. Character order must be preserved, so sorting the letters or comparing character counts would not solve this task.
# Example 3
word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
true
The first array joins to "abc" + "d" + "defg" = "abcddefg". This exactly matches the single string in the second array. Both occurrences of d are retained, including the one at the boundary between "d" and "defg". Concatenation does not deduplicate repeated characters or insert spaces between pieces.
Constraints
How to solve Check If Two String Arrays are Equivalent
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Only the sequence of characters matters, not where the pieces break, so walk both arrays with a character pointer each and compare one character at a time, rolling into the next piece whenever the current piece is exhausted.
Why brute force is not enough
Joining each array into one string and comparing is correct and O(L), but it allocates two new strings of total length up to 10^3 each; interviewers usually accept it and then ask for a constant-space version, or they want you to notice that comparing pieces at equal array indices is simply wrong.
Approach
- Hold four indices: which piece and which character within that piece for each array.
- Loop while both arrays still have characters: compare the two current characters and return false on any mismatch.
- Advance each character index; when it reaches the end of its piece, move to the next piece and reset the character index to 0.
- When the loop ends, return true only if both arrays were exhausted at the same moment; leftover characters on either side mean the strings differ in length.
- An optional precheck compares total lengths and rejects early when they differ.
Walkthrough of Example 1
word1 = ["ab", "c"], word2 = ["a", "bc"] → true
- word1 is [ab, c] and word2 is [a, bc]. Compare a with a: match; word1 advances to b in piece 0, while word2 finishes piece 0 and moves to b in piece 1.
- Compare b with b: match; word1 finishes piece 0 and moves to c in piece 1, word2 advances to c in piece 1.
- Compare c with c: match; both pointers run past their last piece at the same time.
- Both arrays are exhausted together, so return true.
Time and space
Time O(L) · Space O(1). L is the total number of characters across the arrays (at most 10^3 each); every character is visited once and only four indices are stored.
Interview pitfalls
- Comparing word1[i] with word2[i]; example 1 would wrongly return false.
- Returning true when the loop ends without confirming both arrays are exhausted; [abc] versus [abc, d] would pass.
- Forgetting to roll over to the next piece when a piece ends, causing an out-of-range read.
- Assuming pieces are non-empty; the constraints guarantee it here, but a general version must skip empty pieces in a loop rather than a single step.
- Sorting characters or comparing letter counts, which accepts acb versus abc (example 2).
Follow-up questions interviewers ask
- How would you compare two lazily produced character streams, such as iterators or file chunks, with the same approach?
- Can you generalize to k arrays and report the first position where any two disagree?
- How would you check equivalence while ignoring case or whitespace without materializing the strings?
Related LeetCode Problems
Practice more problems similar to Check If Two String Arrays are Equivalent.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.