🔥 Check If Two String Arrays are Equivalent 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
This a very basic question. It is a beginner-friendly question which requires not much logic. We simply need to combine all the substrings in the array into one combines string for both the arrays. The order of combining should not change. After we get both the combined substring, we’ll simply compare them and return true is all the characters match, otherwise false.
Space Complexity: O(m+n) where m and n will be total characters present in array1 and array2 respectively
classSolution { // Runtime: 422 ms, faster than 100.00% of Dart online submissions for Check If Two String Arrays are Equivalent.// Memory Usage: 141.7 MB, less than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent.boolarrayStringsAreEqual(List<String> word1, List<String> word2) { return word1.join("") == word2.join(""); } }
classSolution { boolarrayStringsAreEqual(List<String> word1, List<String> word2) { int firstPointer =0, secondPointer =0; // inner pointerint firstWord =0, secondWord =0; // outer pointerwhile (firstWord < word1.length && secondWord < word2.length) { String firstString = word1[firstWord], secondString = word2[secondWord]; if (firstString.codeUnitAt(firstPointer) != secondString.codeUnitAt(secondPointer)) returnfalse; if (firstPointer < firstString.length -1) { firstPointer++; } else { firstPointer =0; firstWord++; } if (secondPointer < secondString.length -1) { secondPointer++; } else { secondPointer =0; secondWord++; } } return firstWord == word1.length && secondWord == word2.length; } }
classSolution { boolarrayStringsAreEqual(List<String> word1, List<String> word2) { StringBuffer sb1 =StringBuffer(); StringBuffer sb2 =StringBuffer(); for (String s in word1) { sb1.write(s); } for (String s in word2) { sb2.write(s); } return sb1.toString() == (sb2.toString()); } }