Check If Two String Arrays are Equivalent

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. An array represents the string formed by concatenating its elements in order.

30:00

Check If Two String Arrays are Equivalent
easy
Free written solution
Topics
Companies

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. An array represents the string formed by concatenating its elements in order.

Example 1:
Input: {"word1":["ab","c"],"word2":["a","bc"]}
Output: true
Constraints:
  • 1≤word1.length,word2.length≤1031 \leq \text{word1.length}, \text{word2.length} \leq 10^3

  • 1≤word1[i].length,word2[i].length≤1031 \leq \text{word1}[i].\text{length}, \text{word2}[i].\text{length} \leq 10^3

  • 1≤sum(word1[i].length),sum(word2[i].length)≤1031 \leq \text{sum(word1}[i].\text{length)}, \text{sum(word2}[i].\text{length)} \leq 10^3

  • word1[i] and word2[i] consist of lowercase letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word1":["ab","c"],"word2":["a","bc"]}

Instead of building both strings, walk them in place. (w1, c1) points at a character inside word1, (w2, c2) at one inside word2.

both arrays laid end to end

word1: piece 0, char 0

a
b
c

▼▲

1

2

word2: piece 0, char 0

a
b
c

amber ticks = where each array cuts its pieces; the cuts never need to line up

matched = 0
lengths 3 vs 3
Variables
VariableValue
w10
c10
w20
c20
len13
len23
DepthFunction Call
Stack empty
0/8