forked from Akshaya-Amar/LeetCodeSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckIfTwoStringArraysareEquivalent.cpp
54 lines (40 loc) · 1.08 KB
/
CheckIfTwoStringArraysareEquivalent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Source: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
Time: O(n), where n is the number of characters present in the given string array
Space: O(1), in-place
*/
class Solution {
public:
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
int index1 = 0;
int index2 = 0;
int word1Index = 0;
int word2Index = 0;
string a = word1[0];
string b = word2[0];
int aSize = a.size();
int bSize = b.size();
int word1Size = word1.size();
int word2Size = word2.size();
while(word1Index < word1Size && word2Index < word2Size) {
if(a[index1++] != b[index2++]) {
return false;
}
if(index1 == aSize) {
index1 = 0;
if(++word1Index < word1Size) {
a = word1[word1Index];
aSize = a.size();
}
}
if(index2 == bSize) {
index2 = 0;
if(++word2Index < word2Size) {
b = word2[word2Index];
bSize = b.size();
}
}
}
return word1Index == word1Size && word2Index == word2Size;
}
};