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
Approach 1:
public class StringArrayEquivalent {
public static void main(String[] args) {
String[] word1 = {"ab", "c"};
String[] word2 = {"a", "bc"};
boolean answer = arrayStringsAreEqual(word1, word2);
System.out.println("Both String arrays are equivalent: "+answer);
}
public static boolean arrayStringsAreEqual(String[] word1, String[] word2)
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for(String s:word1){
sb1.append(s);
}
for(String s:word2){
sb2.append(s);
}
if((sb1.toString().equals(sb2.toString()))){
return true;
}
return false;
}
}
output:
Both String arrays are equivalent: true
Step 1:
Initially, Create the new StringBuilder classes.
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
Step 2:
The string arrays can be converted to string.
convert the string array word1 into string by appending the each string to the StringBuilder class.
for(String s:word1){
sb1.append(s);
}
Same for the word2 also.
Step 3:
Convert the StringBuilder to String using the toString() method.
We cannot directly compare the two StringBuilder objects because StringBuilder does not override the 'equals' method.
By default, the 'equals' method in 'Object' class compare the references, not their content.
To compare the content convert the StringBuilder to String using the toString() method.
step 4:
After converting to string check if both are equal return true else false.
My Solution Link: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/submissions/1104840828/
Approach 2:
class Solution {
public static void main(String[] args){
String[] word1 = {"ab", "c"};
String[] word2 = {"a", "bc"};
boolean answer = arrayStringsAreEqual(word1, word2);
System.out.println("Both String arrays are equivalent: "+answer);
}
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
String s1 = String.join("",word1);
String s2 = String.join("",word2);
return s1.equals(s2);
}
}
output:
Both String arrays are equivalent: true
Explanation:
Here join function is used to join the array without space.
String s1 = String.join("",word1);
word1 = [ "ab", "c" ]
after join() the output is "abc".
One more example:
String s1 = String.join("-",word1);
Here '-' it is the separator: "ab-c"
Then check the two strings are equals if equal return true else false.
My Solution Link: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/submissions/1051495855/
Thank you for reading.
Happy coding :)