|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | + -* Check If Two String Arrays are Equivalent *- |
| 5 | +
|
| 6 | +
|
| 7 | + Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. |
| 8 | +
|
| 9 | +A string is represented by an array if the array elements concatenated in order forms the string. |
| 10 | +
|
| 11 | +
|
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: word1 = ["ab", "c"], word2 = ["a", "bc"] |
| 16 | +Output: true |
| 17 | +Explanation: |
| 18 | +word1 represents string "ab" + "c" -> "abc" |
| 19 | +word2 represents string "a" + "bc" -> "abc" |
| 20 | +The strings are the same, so return true. |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: word1 = ["a", "cb"], word2 = ["ab", "c"] |
| 24 | +Output: false |
| 25 | +Example 3: |
| 26 | +
|
| 27 | +Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] |
| 28 | +Output: true |
| 29 | +
|
| 30 | +
|
| 31 | +Constraints: |
| 32 | +
|
| 33 | +1 <= word1.length, word2.length <= 103 |
| 34 | +1 <= word1[i].length, word2[i].length <= 103 |
| 35 | +1 <= sum(word1[i].length), sum(word2[i].length) <= 103 |
| 36 | +word1[i] and word2[i] consist of lowercase letters. |
| 37 | +
|
| 38 | +
|
| 39 | +*/ |
| 40 | + |
| 41 | +class A { |
| 42 | +// Runtime: 422 ms, faster than 83.33% of Dart online submissions for Check If Two String Arrays are Equivalent. |
| 43 | +// Memory Usage: 141.7 MB, less than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent. |
| 44 | + bool arrayStringsAreEqual(List<String> word1, List<String> word2) { |
| 45 | + return word1.join("") == word2.join(""); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class B { |
| 50 | +// Runtime: 505 ms, faster than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent. |
| 51 | +// Memory Usage: 141.4 MB, less than 66.67% of Dart online submissions for Check If Two String Arrays are Equivalent. |
| 52 | + bool arrayStringsAreEqual(List<String> word1, List<String> word2) { |
| 53 | + int wordIdx1 = 0, wordIdx2 = 0, chIdx1 = 0, chIdx2 = 0; |
| 54 | + while (true) { |
| 55 | + int ch1 = word1[wordIdx1][chIdx1].codeUnitAt(0); |
| 56 | + int ch2 = word2[wordIdx2][chIdx2].codeUnitAt(0); |
| 57 | + if (ch1 != ch2) return false; |
| 58 | + |
| 59 | + chIdx1++; |
| 60 | + chIdx2++; |
| 61 | + if (chIdx1 == word1[wordIdx1].length) { |
| 62 | + wordIdx1++; |
| 63 | + chIdx1 = 0; |
| 64 | + } |
| 65 | + if (chIdx2 == word2[wordIdx2].length) { |
| 66 | + wordIdx2++; |
| 67 | + chIdx2 = 0; |
| 68 | + } |
| 69 | + |
| 70 | + if (wordIdx1 == word1.length && wordIdx2 == word2.length) break; |
| 71 | + if (wordIdx1 == word1.length || wordIdx2 == word2.length) return false; |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class C { |
| 78 | + bool arrayStringsAreEqual(List<String> word1, List<String> word2) { |
| 79 | + int firstPointer = 0, secondPointer = 0; // inner pointer |
| 80 | + int firstWord = 0, secondWord = 0; // outer pointer |
| 81 | + |
| 82 | + while (firstWord < word1.length && secondWord < word2.length) { |
| 83 | + String firstString = word1[firstWord], secondString = word2[secondWord]; |
| 84 | + |
| 85 | + if (firstString.codeUnitAt(firstPointer) != |
| 86 | + secondString.codeUnitAt(secondPointer)) return false; |
| 87 | + |
| 88 | + if (firstPointer < firstString.length - 1) { |
| 89 | + firstPointer++; |
| 90 | + } else { |
| 91 | + firstPointer = 0; |
| 92 | + firstWord++; |
| 93 | + } |
| 94 | + |
| 95 | + if (secondPointer < secondString.length - 1) { |
| 96 | + secondPointer++; |
| 97 | + } else { |
| 98 | + secondPointer = 0; |
| 99 | + secondWord++; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return firstWord == word1.length && secondWord == word2.length; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class D { |
| 108 | + bool arrayStringsAreEqual(List<String> word1, List<String> word2) { |
| 109 | + StringBuffer sb1 = StringBuffer(); |
| 110 | + StringBuffer sb2 = StringBuffer(); |
| 111 | + for (String s in word1) { |
| 112 | + sb1.write(s); |
| 113 | + } |
| 114 | + for (String s in word2) { |
| 115 | + sb2.write(s); |
| 116 | + } |
| 117 | + return sb1.toString() == (sb2.toString()); |
| 118 | + } |
| 119 | +} |
0 commit comments