Skip to content

Commit cc063c5

Browse files
committed
Runtime 7 ms (Top 39.79%) | Memory 11.0 MB (Top 85.15%)
1 parent 63d77fa commit cc063c5

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
// Runtime: 18 ms (Top 5.29%) | Memory: 11.7 MB (Top 29.17%)
21
class Solution {
32
public:
4-
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
5-
string a,b;
6-
for(auto x:word1)
7-
a += x;
8-
for(auto x:word2)
9-
b += x;
10-
11-
if(a.compare(b)==0)
12-
return 1;
13-
14-
return 0;
3+
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2)
4+
{
5+
int wordIdx1 = 0, wordIdx2 = 0, chIdx1 = 0, chIdx2 = 0;
6+
while(true)
7+
{
8+
char ch1 = word1[wordIdx1][chIdx1];
9+
char ch2 = word2[wordIdx2][chIdx2];
10+
if (ch1 != ch2) return false;
11+
12+
chIdx1++; //incrementing the character index of current word from "word1"
13+
chIdx2++; //incrementing the character index of current word from "word2";
14+
//=========================================================
15+
if (chIdx1 == word1[wordIdx1].size()) //if current word from "word1" is over
16+
{
17+
wordIdx1++; //move to next word in "word1"
18+
chIdx1 = 0; //reset character index to 0
19+
}
20+
if (chIdx2 == word2[wordIdx2].size()) //if current word from "word2" is over
21+
{
22+
wordIdx2++; //move to next word in "word2"
23+
chIdx2 = 0; //reset character index to 0
24+
}
25+
//=================================================================
26+
if (wordIdx1 == word1.size() && wordIdx2 == word2.size()) 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 words in it
30+
//then there is no way same string could be formed on concatenation
31+
}
32+
return true;
1533
}
1634
};

0 commit comments

Comments
 (0)