Leetcode 1662: Check If Two String Arrays are Equivalent

grid47
grid47
Exploring patterns and algorithms
May 24, 2024 4 min read

You are given two string arrays word1 and word2. Return true if these two arrays represent the same string when their elements are concatenated in order, otherwise return false.
Problem
Approach
Steps
Complexity
Input: You are given two arrays of strings, `word1` and `word2`.
Example: word1 = ["hello", "world"], word2 = ["hell", "oworld"]
Constraints:
• 1 <= word1.length, word2.length <= 10^3
• 1 <= word1[i].length, word2[i].length <= 10^3
• 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
• word1[i] and word2[i] consist of lowercase letters.
Output: Return `true` if the two arrays represent the same string, otherwise return `false`.
Example: true
Constraints:
• The output will be a boolean value.
Goal: Check if the concatenated strings from `word1` and `word2` are equal.
Steps:
• Concatenate all strings in `word1` into a single string `str1`.
• Concatenate all strings in `word2` into a single string `str2`.
• Compare the two resulting strings and return `true` if they are equal, otherwise return `false`.
Goal: The constraints ensure the solution can handle large string arrays efficiently.
Steps:
• 1 <= word1.length, word2.length <= 10^3
• 1 <= word1[i].length, word2[i].length <= 10^3
• 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
Assumptions:
• The arrays `word1` and `word2` will only contain lowercase letters.
Input: word1 = ["hello", "world"], word2 = ["hell", "oworld"]
Explanation: Both `word1` and `word2` form the string "helloworld" when concatenated, so the answer is `true`.

Input: word1 = ["abc", "def"], word2 = ["abcd", "ef"]
Explanation: The strings formed by `word1` and `word2` are "abcdef" and "abcde", respectively, which are not equal. Therefore, return `false`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus