1662. Check If Two String Arrays are Equivalent LeetCode Solution
In this guide, you will get 1662. Check If Two String Arrays are Equivalent LeetCode Solution with the best time and space complexity. The solution to Check If Two String Arrays are Equivalent problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Check If Two String Arrays are Equivalent solution in C++
- Check If Two String Arrays are Equivalent solution in Java
- Check If Two String Arrays are Equivalent solution in Python
- Additional Resources

Problem Statement of Check If Two String Arrays are Equivalent
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.
Complexity Analysis
- Time Complexity: O(|\texttt{word1}| + |\texttt{word2}|)
- Space Complexity: O(1)
1662. Check If Two String Arrays are Equivalent LeetCode Solution in C++
class Solution {
public:
bool arrayStringsAreEqual(std::vector<std::string>& word1,
std::vector<std::string>& word2) {
int i = 0; // word1's index
int j = 0; // word2's index
int a = 0; // word1[i]'s index
int b = 0; // word2[j]'s index
while (i < word1.size() && j < word2.size()) {
if (word1[i][a] != word2[j][b])
return false;
if (++a == word1[i].size()) {
++i;
a = 0;
}
if (++b == word2[j].size()) {
++j;
b = 0;
}
}
return i == word1.size() && j == word2.size();
}
};
/* code provided by PROGIEZ */
1662. Check If Two String Arrays are Equivalent LeetCode Solution in Java
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i = 0; // word1's index
int j = 0; // word2's index
int a = 0; // word1[i]'s index
int b = 0; // word2[j]'s index
while (i < word1.length && j < word2.length) {
if (word1[i].charAt(a) != word2[j].charAt(b))
return false;
if (++a == word1[i].length()) {
++i;
a = 0;
}
if (++b == word2[j].length()) {
++j;
b = 0;
}
}
return i == word1.length && j == word2.length;
}
}
// code provided by PROGIEZ
1662. Check If Two String Arrays are Equivalent LeetCode Solution in Python
class Solution:
def arrayStringsAreEqual(self, word1: list[str], word2: list[str]) -> bool:
i = 0 # word1's index
j = 0 # word2's index
a = 0 # word1[i]'s index
b = 0 # word2[j]'s index
while i < len(word1) and j < len(word2):
if word1[i][a] != word2[j][b]:
return False
a += 1
if a == len(word1[i]):
i += 1
a = 0
b += 1
if b == len(word2[j]):
j += 1
b = 0
return i == len(word1) and j == len(word2)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.