Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise
Example 1:
Input: word1 = {"ab", "c"}, word2 = {"a", "bc"} Output: true
Approach:
Java
public class ArrayStringsAreEqual {
public static void main(String[] args) {
String word1[] = { "a", "bc" };
String word2[] = { "ab", "c" };
System.out.println(arrayStringsAreEqual(word1, word2));
}
public static boolean arrayStringsAreEqual(String[] word1,
String[] word2) {
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
for (String s : word1) {
sb1.append(s);
}
for (String s : word2) {
sb2.append(s);
}
if (sb1.toString().equals(sb2.toString()))
return true;
return false;
}
}
C++
#include <bits/stdc++.h>
using namespace std;
bool arrayStringsAreEqual(vector<string>& word1,
vector<string>& word2)
{
string str1="",str2="";
for(int i=0;i<word1.size();i++)
str1+=word1[i];
for(int i=0;i<word2.size();i++)
str2+=word2[i];
if(str1==str2)
return true;
return false;
}
int main()
{
vector<string> word1 ={"ab", "c"};
vector<string> word2 = {"a", "bc"};
if(arrayStringsAreEqual(word1,word2))
cout<<"true"<<"\n";
else
cout<<"false\n";
return 0;
}
No comments:
Post a Comment