Magicsheet logo

Check If Two String Arrays are Equivalent

Easy
12.5%
Updated 8/1/2025

Check If Two String Arrays are Equivalent

What is this problem about?

Given two string arrays, word1 and word2, you need to determine if they represent the same string. For example, if word1 = ["ab", "c"] and word2 = ["a", "bc"], both represent the concatenated string "abc", so they are equivalent.

Why is this asked in interviews?

This is a popular "Easy" question at Apple, Amazon, and Google. It tests your basic string manipulation skills and your ability to optimize for space. While the "naive" solution is to concatenate both arrays and compare the resulting strings, a more advanced solution uses pointers to compare characters one by one without creating new large strings.

Algorithmic pattern used

The pattern is Two Pointers. You maintain four indices: i1 and i2 for the current word in each array, and p1 and p2 for the current character within those words. You compare word1[i1][p1] with word2[i2][p2]. If they match, you increment the character pointers. If a word is finished, you move to the next word in that array and reset the character pointer.

Example explanation

word1 = ["ab", "c"], word2 = ["a", "bc"]

  1. Compare word1[0][0] ('a') and word2[0][0] ('a'). Match!
  2. word2[0] is finished. Move to word2[1].
  3. Compare word1[0][1] ('b') and word2[1][0] ('b'). Match!
  4. word1[0] is finished. Move to word1[1].
  5. Compare word1[1][0] ('c') and word2[1][1] ('c'). Match! Both arrays are finished at the same time. Result: True.

Common mistakes candidates make

The biggest mistake is using string concatenation ("".join(word1)), which uses O(N)O(N) extra space. In an interview, the goal is often to see if you can achieve O(1)O(1) extra space using pointers. Another error is not checking if both arrays finish at the same time; if one array has extra characters at the end, they are not equivalent.

Interview preparation tip

Practice "in-place" comparisons whenever possible. Many string and array problems have a simple O(N)O(N) space solution and a slightly harder O(1)O(1) space solution. Interviewers highly value the latter.

Similar Questions