Check If Two String Arrays are Equivalent LeetCode solution in C++

1
class Solution {
2
public:
3
bool arrayStringsAreEqual(vector<string> &word1, vector<string> &word2) {
4
int wordIdx1 = 0, wordIdx2 = 0, chIdx1 = 0, chIdx2 = 0;
5
while (true) {
6
char ch1 = word1[wordIdx1][chIdx1];
7
char ch2 = word2[wordIdx2][chIdx2];
8
if (ch1 != ch2) return false;
9

10
chIdx1++; // incrementing the character index of current word from "word1"
11
chIdx2++; // incrementing the character index of current word from
12
// "word2";
13
//=========================================================
14
if (chIdx1 == word1[wordIdx1].size()) // if current word from "word1" is over
15
{
16
wordIdx1++; // move to next word in "word1"
17
chIdx1 = 0; // reset character index to 0
18
}
19
if (chIdx2 == word2[wordIdx2].size()) // if current word from "word2" is over
20
{
21
wordIdx2++; // move to next word in "word2"
22
chIdx2 = 0; // reset character index to 0
23
}
24
//=================================================================
25
if (wordIdx1 == word1.size() && wordIdx2 == word2.size())
26
break; // words in both arrays are finished
27

28
if (wordIdx1 == word1.size() || wordIdx2 == word2.size()) return false;
29
// if words in any onr of the arrays are finished and other still has some
30
// words in it then there is no way same string could be formed on
31
// concatenation
32
}
33
return true;
34
}
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0