Skip to content

Commit 2a20131

Browse files
authored
Easy 1662. Check if Two String Arrays are Equivalent
1 parent 8cea40d commit 2a20131

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Use any of two approaches below
2+
3+
T.c - O(n)
4+
s.c - O(1)
5+
6+
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
7+
8+
// Approach 1
9+
string s1 = "";
10+
string s2 = "";
11+
for(string s: word1){
12+
s1.append(s);
13+
}
14+
for(string p: word2){
15+
s2.append(p);
16+
}
17+
if(s1 == s2){
18+
return true;
19+
}
20+
return false;
21+
22+
23+
//Approach 2
24+
// Nice Approach // Use any of two approaches
25+
26+
int m = word1.size();
27+
int n = word2.size();
28+
29+
int w1i=0; int i =0;
30+
int w2i =0; int j =0;
31+
32+
while(w1i < m && w2i < n){
33+
if(word1[w1i][i] != word2[w2i][j])
34+
return false;
35+
i++;
36+
j++;
37+
if(i ==word1[w1i].length()){
38+
i =0;
39+
w1i++;
40+
}
41+
if(j == word2[w2i].length()){
42+
j =0;
43+
w2i++;
44+
}
45+
}
46+
if(w1i == word1.size() && w2i == word2.size())
47+
return true;
48+
49+
return false;
50+
}

0 commit comments

Comments
 (0)