Skip to content

Commit f105faf

Browse files
authored
Update Check If Two String Arrays are Equivalent.java
1 parent 7d7cdea commit f105faf

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
class Solution {
22
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
3-
return Arrays.stream(word1).reduce((a, b) -> a + b)
4-
.equals(Arrays.stream(word2).reduce((a, b) -> a + b));
3+
int wordIdxOne = 0;
4+
int wordIdxTwo = 0;
5+
int idxOne = 0;
6+
int idxTwo = 0;
7+
while (wordIdxOne < word1.length && wordIdxTwo < word2.length) {
8+
if (idxOne == word1[wordIdxOne].length() || idxTwo == word2[wordIdxTwo].length()) {
9+
if (idxOne == word1[wordIdxOne].length()) {
10+
wordIdxOne++;
11+
idxOne = 0;
12+
}
13+
if (idxTwo == word2[wordIdxTwo].length()) {
14+
wordIdxTwo++;
15+
idxTwo = 0;
16+
}
17+
} else {
18+
if (word1[wordIdxOne].charAt(idxOne) != word2[wordIdxTwo].charAt(idxTwo)) {
19+
return false;
20+
}
21+
idxOne++;
22+
idxTwo++;
23+
}
24+
}
25+
return wordIdxOne == word1.length && wordIdxTwo == word2.length;
526
}
627
}

0 commit comments

Comments
 (0)