Check If Two String Arrays are Equivalent
EasyPrompt
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: falseExample 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]andword2[i]consist of lowercase letters.
Approaches
2 approaches with complexity analysis and trade-offs.
This approach involves creating the two complete strings by concatenating the elements of each array and then comparing the resulting strings.
Algorithm
- Create a
StringBuildersb1. - Iterate through
word1and append each string tosb1. - Create a
StringBuildersb2. - Iterate through
word2and append each string tosb2. - Return the result of
sb1.toString().equals(sb2.toString()).
Walkthrough
The idea is to simulate the process described in the problem directly. We build two strings, one for each input array.
- Initialize two
StringBuilderobjects,sb1andsb2. - Iterate through the
word1array. For each stringsinword1, append it tosb1. - Iterate through the
word2array. For each stringsinword2, append it tosb2. - After building both strings, convert the
StringBuilderobjects toStringobjects. - Compare the two resulting strings using the
.equals()method. If they are identical, returntrue; otherwise, returnfalse.
class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { StringBuilder sb1 = new StringBuilder(); for (String s : word1) { sb1.append(s); } StringBuilder sb2 = new StringBuilder(); for (String s : word2) { sb2.append(s); } return sb1.toString().equals(sb2.toString()); }}A more concise way to write this in Java using String.join:
class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { return String.join("", word1).equals(String.join("", word2)); }}Complexity
Time
O(N + M), where N is the total number of characters in `word1` and M is the total number of characters in `word2`. We need to iterate through all characters to build the strings.
Space
O(N + M). We need extra space to store the two concatenated strings, where N is the total length of strings in `word1` and M is the total length of strings in `word2`.
Trade-offs
Pros
Very simple and intuitive to implement.
Code is clean and easy to read, especially with helpers like
String.join.
Cons
Inefficient in terms of space, as it requires creating copies of all the characters in memory.
Can be slow if the total length of strings is very large due to memory allocation.
Solutions
Solution
class Solution { public boolean arrayStringsAreEqual ( String [] word1 , String [] word2 ) { return String . join ( "" , word1 ). equals ( String . join ( "" , word2 )); } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.