- Published on
Check If Two String Arrays are Equivalent.
Problem of the day - Check If Two String Arrays are Equivalent
Tag - Easy.
Problem appeared very very easy to me. So, problem say, two string arrays word1
& word2
are given and it return true
if array elements concatenated results in the same string for both arrays.
example -
word1 = ["ab", "c"]
, word2 = ["a", "bc"]
output should be true
as both arrays results into abc
after concatenation.
My approach would be just to have two empty string variables. Iterate over each array and prepare final strings after concatenation, just return if both strings are equal or not.
Code
class Solution {public: bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) { string str1 = ""; string str2 = ""; for(auto x: word1) str1 += x; for(auto x: word2) str2 += x; return str1 == str2; }};
Time Complexity - O(n)
Space Complexity - O(1)
God, remind me to look at this another smart approach.
You might like previous editions of my coding diary -