Check If Two String Arrays are Equivalent

Easy
#1525Time: O(N + M), where N is the total number of characters in `word1` and M is the total number of characters in `word2`. We need to iterate through all characters to build the strings.Space: O(N + M). We need extra space to store the two concatenated strings, where N is the total length of strings in `word1` and M is the total length of strings in `word2`.
Data structures

Prompt

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.

Approaches

2 approaches with complexity analysis and trade-offs.

This approach involves creating the two complete strings by concatenating the elements of each array and then comparing the resulting strings.

Algorithm

  • Create a StringBuilder sb1.
  • Iterate through word1 and append each string to sb1.
  • Create a StringBuilder sb2.
  • Iterate through word2 and append each string to sb2.
  • Return the result of sb1.toString().equals(sb2.toString()).

Walkthrough

The idea is to simulate the process described in the problem directly. We build two strings, one for each input array.

  1. Initialize two StringBuilder objects, sb1 and sb2.
  2. Iterate through the word1 array. For each string s in word1, append it to sb1.
  3. Iterate through the word2 array. For each string s in word2, append it to sb2.
  4. After building both strings, convert the StringBuilder objects to String objects.
  5. Compare the two resulting strings using the .equals() method. If they are identical, return true; otherwise, return false.
class Solution {    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {        StringBuilder sb1 = new StringBuilder();        for (String s : word1) {            sb1.append(s);        }         StringBuilder sb2 = new StringBuilder();        for (String s : word2) {            sb2.append(s);        }         return sb1.toString().equals(sb2.toString());    }}

A more concise way to write this in Java using String.join:

class Solution {    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {        return String.join("", word1).equals(String.join("", word2));    }}

Complexity

Time

O(N + M), where N is the total number of characters in `word1` and M is the total number of characters in `word2`. We need to iterate through all characters to build the strings.

Space

O(N + M). We need extra space to store the two concatenated strings, where N is the total length of strings in `word1` and M is the total length of strings in `word2`.

Trade-offs

Pros

  • Very simple and intuitive to implement.

  • Code is clean and easy to read, especially with helpers like String.join.

Cons

  • Inefficient in terms of space, as it requires creating copies of all the characters in memory.

  • Can be slow if the total length of strings is very large due to memory allocation.

Solutions

class Solution { public boolean arrayStringsAreEqual ( String [] word1 , String [] word2 ) { return String . join ( "" , word1 ). equals ( String . join ( "" , word2 )); } }

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.