Skip to main content

Check If Two String Arrays are Equivalent - Solution & Explanation

EasyArrayString20 min readAsked at: Amazon, Meta, Google
Practice this problem

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.

 

Example 1:

Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.

Example 2:

Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false

Example 3:

Input: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true

 

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.

Approach Overview

Problem Overview: You receive two arrays of strings, word1 and word2. When all elements in each array are concatenated, they form two full strings. The task is to check whether those resulting strings are identical without misinterpreting character order.

Approach 1: String Concatenation (O(n) time, O(n) space)

The most direct solution is to concatenate all strings in word1 into a single string and do the same for word2. After building the two final strings, perform a simple equality check. This works because string equality guarantees both length and character order match. The total runtime is O(n), where n is the total number of characters across both arrays, since every character is copied once. Space complexity is also O(n) because two new strings are created.

This approach is clean and easy to implement in any language. However, it allocates extra memory for the combined strings. For small inputs this cost is negligible, but interviewers often expect you to recognize that the comparison can be done without building the full strings.

Approach 2: Two-Pointer Character Comparison (O(n) time, O(1) space)

A more efficient approach compares characters directly using pointers. Maintain two indices: one for the current string in word1 and another for word2, along with character positions inside those strings. Iterate character by character, comparing the current characters from both arrays. When you reach the end of a string in either array, move the pointer to the next string and reset the character index.

This method processes characters sequentially without constructing intermediate strings. Every character is visited exactly once, so the runtime remains O(n). Since only a few pointer variables are used, the space complexity is O(1). This technique resembles streaming comparison and is common in string processing problems where memory efficiency matters.

The logic relies on careful pointer movement across array boundaries, making it a good exercise in handling nested iteration across array elements while maintaining character-level comparison.

Recommended for interviews: The two-pointer approach is typically preferred. It demonstrates awareness of unnecessary memory allocations and shows you can iterate across segmented strings efficiently. Starting with the concatenation solution shows clear thinking, but transitioning to the pointer-based optimization highlights strong problem-solving skills and familiarity with two-pointer techniques.

Approach 1: String Concatenation Approach

This approach involves concatenating all the elements in each string array to form two complete strings, and then comparing these strings for equality. Since we need to ensure that the concatenated results of the two arrays are the same, this approach is both intuitive and straightforward.

This C program uses the strcat function to concatenate all the parts of word1 and word2 into two separate strings. It then applies strcmp to check if these strings are identical, returning true if they are; otherwise, false. The buffer sizes are assumed to be within bounds because of the constraints given in the problem.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity is O(n), where n is the sum of the lengths of both arrays' strings, as each character is traversed once during concatenation. The space complexity is O(n) due to the storage of the concatenated strings.

Try this approach in the editor →

Approach 2: Two-Pointer Approach

Instead of explicitly concatenating the arrays into strings, this approach uses two pointers to iterate through the characters of the arrays simultaneously. It allows checking one character at a time across both arrays, ensuring they match without creating additional strings.

This C solution uses multiple iterators to navigate through the arrays without creating concatenated strings. Characters are compared directly during iteration until all characters in both arrays have been evaluated.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity is O(n), where n is the total number of characters in both arrays. Space complexity is minimized to O(1) as no additional storage is required beyond basic variables.

Try this approach in the editor →

Approach 3: String Concatenation

Concatenate the strings in the two arrays into two strings, then compare whether the two strings are equal.

The time complexity is O(m), and the space complexity is O(m). Here, m is the total length of the strings in the arrays.

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Approach 4: Direct Traversal

In Solution 1, we concatenated the strings in the two arrays into two new strings, which has additional space overhead. We can also directly traverse the two arrays and compare the characters one by one.

We use two pointers i and j to point to the two string arrays, and another two pointers x and y to point to the corresponding characters in the strings. Initially, i = j = x = y = 0.

Each time we compare word1[i][x] and word2[j][y]. If they are not equal, we directly return false. Otherwise, we increment x and y by 1. If x or y exceeds the length of the corresponding string, we increment the corresponding string pointer i or j by 1, and then reset x and y to 0.

If both string arrays are traversed, we return true, otherwise, we return false.

The time complexity is O(m), and the space complexity is O(1). Here, m is the total length of the strings in the arrays.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Concatenation Approach

The time complexity is O(n), where n is the sum of the lengths of both arrays' strings, as each character is traversed once during concatenation. The space complexity is O(n) due to the storage of the concatenated strings.

Two-Pointer Approach

Time complexity is O(n), where n is the total number of characters in both arrays. Space complexity is minimized to O(1) as no additional storage is required beyond basic variables.

String Concatenation—
Direct Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String ConcatenationO(n)O(n)Best for quick implementation or when memory usage is not a concern.
Two-Pointer Character ComparisonO(n)O(1)Preferred when avoiding extra string allocation or when optimizing memory usage.

Video Solution

Check If Two String Arrays are Equivalent | Goldman Sachs | Explanation | Live Coding • codestorywithMIK • 18,544 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check If Two String Arrays are Equivalent easy or hard?
The problem is classified as Easy on LeetCode with a high acceptance rate around 85%. The core challenge is recognizing that multiple strings can represent a single logical string and implementing a clean comparison strategy.
Check If Two String Arrays are Equivalent Python/Java solution
In Python or Java, the simplest implementation concatenates the arrays using join operations and compares the resulting strings. The optimized implementation maintains indices for both arrays and compares characters directly to avoid building intermediate strings while still achieving O(n) runtime.
How to solve Check If Two String Arrays are Equivalent in O(n)?
Traverse both arrays using two pointers that track the current string index and character position. Compare characters one by one and advance pointers accordingly. When a string ends, move to the next string in that array. If all characters match and both arrays finish simultaneously, the arrays represent the same string.
What is the best approach for Check If Two String Arrays are Equivalent?
The two-pointer character comparison approach is the most efficient solution. It iterates through both string arrays character by character without building full concatenated strings. This achieves O(n) time complexity and O(1) extra space, making it the preferred method in technical interviews.
Is Check If Two String Arrays are Equivalent asked at Google/Amazon/Meta?
Problems involving string comparison and pointer traversal frequently appear in interviews at companies like Amazon, Google, and Meta. This specific problem tests understanding of string handling, array traversal, and memory optimization, which are common interview themes.
What data structure is used in Check If Two String Arrays are Equivalent?
The problem primarily uses arrays of strings and sequential character traversal. The optimized solution relies on pointer indices rather than additional data structures, making it a lightweight algorithm focused on string processing.
What is the time complexity of Check If Two String Arrays are Equivalent?
The optimal solution runs in O(n) time, where n is the total number of characters across both arrays. Each character is visited exactly once during comparison. The concatenation method also runs in O(n) time but uses additional O(n) memory for the constructed strings.

Ready to solve this problem?

Practice Check If Two String Arrays are Equivalent with our built-in code editor and test cases.

Practice on FleetCode