Check If Two String Arrays are Equivalent (LeetCode 1662): Solution and Explanation

Easy
Array
String
Two Pointers

Two arrays of strings are equivalent when gluing each array's pieces together gives the same string. The pieces themselves can be cut in completely different places.

Intuition

The direct answer is to join both arrays and compare the results. That is correct and short, but it copies every character into two new strings.

Notice that the cut points never matter. ["ab", "c"] and ["a", "bc"] are equal because the characters line up one by one, not because the pieces do. So walk both arrays as if they were already joined.

Each array gets a two-part cursor: which piece you are in and which character inside it. Compare the two characters under the cursors, step both forward, and when a cursor falls off the end of its piece, move it to the start of the next one.

A mismatch ends the search immediately. If no mismatch occurs, the strings are equal only when both arrays run out together; if one runs out first, its string is a strict prefix of the other.

Approach: Two cursors over the virtual concatenation

  1. Set w1 = c1 = 0 for word1 and w2 = c2 = 0 for word2.
  2. While w1 < len(word1) and w2 < len(word2), compare word1[w1][c1] with word2[w2][c2].
  3. If they differ, return false.
  4. Increment c1 and c2. If c1 reaches the length of word1[w1], move to w1 + 1 and reset c1 to 0. Do the same for word2.
  5. After the loop, return true only if w1 == len(word1) and w2 == len(word2).

Step-by-step example

word1 = ["ab","c"], word2 = ["a","bc"]
  1. Compare 'a' (word1[0][0]) with 'a' (word2[0][0]): equal. word2[0] is used up, so word2's cursor jumps to word2[1][0].
  2. Compare 'b' (word1[0][1]) with 'b' (word2[1][0]): equal. word1[0] is used up, so word1's cursor jumps to word1[1][0].
  3. Compare 'c' (word1[1][0]) with 'c' (word2[1][1]): equal. Both pieces end, so both cursors move past their arrays.
  4. Both arrays ran out together, so return true.

Code: Python, Java, C++

1class Solution:
2    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
3        w1 = w2 = 0
4        c1 = c2 = 0
5        while w1 < len(word1) and w2 < len(word2):
6            if word1[w1][c1] != word2[w2][c2]:
7                return False
8            c1 += 1
9            c2 += 1
10            if c1 == len(word1[w1]):
11                w1 += 1
12                c1 = 0
13            if c2 == len(word2[w2]):
14                w2 += 1
15                c2 = 0
16        return w1 == len(word1) and w2 == len(word2)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each character is compared at most once, where n is the length of the shorter combined string.

Space

O(1)

Only four integer indices are stored; no joined string is built.

Other approaches

Join and compare (time O(n), space O(n))

Concatenate each array with a join and compare the two strings. It is the simplest code and is fine for the given limits, but it allocates both full strings.

Character generators (time O(n), space O(1))

Write a generator that yields the characters of an array in order and zip two of them together. It reads cleanly in Python but has the same cursor logic hidden inside.

Common mistakes

  • Comparing pieces pairwise. word1[i] and word2[i] do not have to match, only the glued strings do.
  • Returning true as soon as the loop ends. If one array still has characters left, one string is a prefix of the other and the answer is false.
  • Forgetting to reset the character index to 0 after moving to the next piece.
  • Checking only the total lengths. Equal lengths do not mean equal characters.

Frequently asked questions

Is joining the strings an acceptable answer?

Yes. It runs in O(n) time and passes, but it uses O(n) extra memory. The cursor version keeps the same time and drops the extra memory, which is the usual follow-up question.

Why check that both arrays are exhausted at the end?

The loop stops as soon as either array runs out. If the other one still has characters, the strings have different lengths, so they cannot be equal.

Can pieces be empty?

Not under the constraints, since every piece has at least one character. If empty pieces were allowed, the cursor would need to skip them in a loop instead of a single if.

What is the time complexity of Check If Two String Arrays are Equivalent?

O(n) time, where n is the total number of characters, and O(1) extra space with the two-cursor method.