Problem Name: 1662. Check If Two String Arrays are Equivalent
Problem Link: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
Difficulty: Easy
Tag: Array | String
Language: C# | C++
OJ: LeetCode
Algorithm:
Input:
The algorithm takes two arrays of strings as input, denoted as word1 and word2.
Initialization:
Two empty strings, answer1 and answer2, are initialized. These strings will be used to concatenate the characters from the input arrays.
Concatenation of Characters:
The algorithm iterates over each string in word1, and for each character in those strings, it appends the character to answer1.
The same process is repeated for word2, appending characters to answer2.
Comparison:
The concatenated strings answer1 and answer2 are compared.
Return:
If answer1 is equal to answer2, the algorithm returns true, indicating that the concatenation of strings in word1 is equal to the concatenation of strings in word2.
If answer1 is not equal to answer2, the algorithm returns false.
Code(C#)
public class Solution
{
public bool ArrayStringsAreEqual(string[] word1, string[] word2)
{
string answer1 = "";
string answer2 = "";
for(int i=0; i<word1.Length; i++)
for (int j = 0; j < word1[i].Length; j++)
answer1 += word1[i][j];
for(int i=0; i<word2.Length; i++)
for (int j = 0; j < word2[i].Length; j++)
answer2 += word2[i][j];
if(answer1 == answer2) return true;
else return false;
}
}
Code(C++)
class Solution {
public:
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
string answer1,answer2;
for(int i=0; i<word1.size(); i++)
for(int j=0; j<word1[i].size(); j++)
answer1 += word1[i][j];
for(int i=0; i<word2.size(); i++)
for(int j=0; j<word2[i].size(); j++)
answer2 += word2[i][j];
if(answer1 == answer2) return true;
else return false;
}
};