From 04eb72383c8ac61aec3e93533899b1f9be1bdd7b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 2 Dec 2023 21:16:15 +0100 Subject: [PATCH] =?UTF-8?q?problems(java):=20add=20=E2=80=9C1662.=20Check?= =?UTF-8?q?=20If=20Two=20String=20Arrays=20are=20Equivalent=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...k-if-two-string-arrays-are-equivalent.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 problems/java/check-if-two-string-arrays-are-equivalent.java diff --git a/problems/java/check-if-two-string-arrays-are-equivalent.java b/problems/java/check-if-two-string-arrays-are-equivalent.java new file mode 100644 index 0000000..fc49fbd --- /dev/null +++ b/problems/java/check-if-two-string-arrays-are-equivalent.java @@ -0,0 +1,27 @@ +class Solution { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + int i = 0, ii = 0; + int j = 0, jj = 0; + + while (i < word1.length && j < word2.length) { + if (word1[i].charAt(ii) != word2[j].charAt(jj)) { + return false; + } + + ++ii; + ++jj; + + if (ii >= word1[i].length()) { + ++i; + ii = 0; + } + + if (jj >= word2[j].length()) { + ++j; + jj = 0; + } + } + + return i == word1.length && j == word2.length && ii == 0 && jj == 0; + } +}