Domain 1EasyCORE

Check If Two String Arrays are Equivalent

The problem requires us to determine whether two string arrays represent the same concatenated string. The input consists of two lists of strings, `word1` and `word2`. The output should be a boolean value: `true` if the concatenation of all elements in `word1` equals the concatenation of all elements in `word2`, otherwise `false`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

1662. Check If Two String Arrays are Equivalent

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 82.6%


Problem Breakdown

The problem requires us to determine whether two string arrays represent the same concatenated string. The input consists of two lists of strings, word1 and word2. The output should be a boolean value: true if the concatenation of all elements in word1 equals the concatenation of all elements in word2, otherwise false.

Inputs:

  • Two arrays of strings: word1 and word2.

Outputs:

  • A boolean indicating whether the concatenated strings are equal.

Implicit Constraints/Edge Cases:

  • The arrays can be empty, but they will not contain any null or undefined values.
  • Each string in the arrays consists only of lowercase letters.
  • The lengths of the arrays and their elements are within specified bounds (1 to 1000).

Intuition

A brute-force approach would involve concatenating all strings in each array and then comparing the resulting strings. This method is straightforward but inefficient, especially given the constraints where the total length of strings can be up to 1000.

The key insight is to avoid creating large concatenated strings by using two pointers or iterators to traverse both arrays simultaneously. This approach allows us to compare characters one by one without the overhead of string concatenation.

Approach

To solve this problem efficiently, we will use a two-pointer technique:

  1. Initialize two pointers, i and j, to track the current position in word1 and word2, respectively.
  2. Use two additional pointers, p and q, to track the current character within the strings pointed to by i and j.
  3. Compare characters from both arrays:
    • If the characters are different, return false.
    • If one string is exhausted before the other, move to the next string in the respective array.
  4. Continue this process until all characters in both arrays have been compared.
  5. If all characters match and both arrays are fully traversed, return true.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the total number of characters in word1 and m is the total number of characters in word2. This is because we traverse each character exactly once.
  • Space Complexity: O(1). We use a constant amount of extra space for pointers, regardless of the input size.

Solution

def arrayStringsAreEqual(word1: List[str], word2: List[str]) -> bool:
    i, j = 0, 0  # Pointers to track current string in word1 and word2
    p, q = 0, 0  # Pointers to track current character within the strings
    
    while i < len(word1) and j < len(word2):
        if word1[i][p] != word2[j][q]:
            return False
        
        # Move to the next character in the current string
        p += 1
        q += 1
        
        # If end of current string in word1 is reached, move to the next string
        if p == len(word1[i]):
            i += 1
            p = 0
        
        # If end of current string in word2 is reached, move to the next string
        if q == len(word2[j]):
            j += 1
            q = 0
    
    # Check if both arrays have been fully traversed
    return i == len(word1) and j == len(word2)

Edge Cases Handled

  • Empty Arrays: If both word1 and word2 are empty, the function returns true.
  • Single Character Strings: The function correctly handles cases where each array contains only single-character strings.
  • Unequal Length Strings: The function ensures that all characters in both arrays are compared, even if the individual strings have different lengths.