Check If Two String Arrays are Equivalent - Problem
String Array Equivalence Check

You're given two arrays of strings and need to determine if they represent the same final string when concatenated.

The Challenge: Given two string arrays word1 and word2, return true if concatenating all elements in word1 produces the same string as concatenating all elements in word2.

Example:
word1 = ["ab", "c"] → concatenated: "abc"
word2 = ["a", "bc"] → concatenated: "abc"
• Result: true (both arrays represent the same string)

Goal: Efficiently compare the concatenated results without unnecessary memory allocation.

Input & Output

example_1.py — Basic Match
$ Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
💡 Note: word1 concatenates to "abc" and word2 concatenates to "abc", so they are equivalent.
example_2.py — Different Strings
$ Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false
💡 Note: word1 concatenates to "acb" while word2 concatenates to "abc", so they are not equivalent.
example_3.py — Single Elements
$ Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true
💡 Note: Both arrays represent the same string "abcddefg" when concatenated.

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

Visualization

Tap to expand
Two Pointers: Efficient Character-by-Character ComparisonBook 1: ["ab", "c"]abcPointerBook 2: ["a", "bc"]abcPointerCharacter ComparisonCurrent: Book1[0][0] = 'a'Current: Book2[0][0] = 'a'✓ Match! Continue...Algorithm Steps:1. Compare characters at current positions2. If match: advance both pointers, if end of string: move to next string3. If mismatch: return false immediately
Understanding the Visualization
1
Set Bookmarks
Place bookmarks at the start of both books
2
Read & Compare
Read one character from each book and compare them
3
Move Bookmarks
If characters match, advance both bookmarks to next position
4
Early Exit
If any characters don't match, we know books are different immediately
Key Takeaway
🎯 Key Insight: By comparing characters as we encounter them rather than building complete strings, we save memory and can exit early on the first mismatch, making the algorithm both space and time efficient.
Asked in
Amazon 25 Microsoft 18 Apple 12 Google 8
48.2K Views
Medium Frequency
~8 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen