Check If Two String Arrays are Equivalent
EASYDescription
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]andword2[i]consist of lowercase letters.
Approaches
Checkout 2 different approaches to solve Check If Two String Arrays are Equivalent. Click on different approaches to view the approach and algorithm in detail.
String Concatenation
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()).
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 Analysis
Pros and Cons
- Very simple and intuitive to implement.
- Code is clean and easy to read, especially with helpers like
String.join.
- 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.
Code Solutions
Checking out 3 solutions in different languages for Check If Two String Arrays are Equivalent. Click on different languages to view the code.
class Solution { public boolean arrayStringsAreEqual ( String [] word1 , String [] word2 ) { return String . join ( "" , word1 ). equals ( String . join ( "" , word2 )); } }Video Solution
Watch the video walkthrough for Check If Two String Arrays are Equivalent
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.