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 <= 1031 <= word1[i].length, word2[i].length <= 1031 <= sum(word1[i].length), sum(word2[i].length) <= 103word1[i] and word2[i] consist of lowercase letters.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.
C++
Java
Python
C#
JavaScript
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.
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.
C++
Java
Python
C#
JavaScript
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.
| Approach | Complexity |
|---|---|
| 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. |
Check If Two String Arrays are Equivalent - Leetcode 1662 - Python • NeetCodeIO • 7,963 views views
Watch 9 more video solutions →Practice Check If Two String Arrays are Equivalent with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor