Check If Two String Arrays are Equivalent

Let’s break down the problem into smaller steps and then write the code.

Problem Analysis

You have two arrays of strings word1 and word2. You need to concatenate the strings in each array and then check if the resulting strings are equal.

Steps

  1. Initialize two empty strings, str1 and str2.
  2. Iterate through word1, appending each string to str1.
  3. Iterate through word2, appending each string to str2.
  4. Compare str1 and str2. If they are equal, return true. Otherwise, return false.

Code

Below is the code that follows the above steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
        # Step 1: Initialize empty strings
        str1 = ""
        str2 = ""

        # Step 2: Concatenate the strings in word1
        for word in word1:
            str1 += word

        # Step 3: Concatenate the strings in word2
        for word in word2:
            str2 += word

        # Step 4: Compare the concatenated strings
        return str1 == str2

Explanation

The code concatenates the strings in word1 and word2 and then compares the concatenated results. If they are the same, it returns true; otherwise, it returns false.