|
| 1 | +# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation |
| 2 | + |
| 3 | +## Intuition |
| 4 | + |
| 5 | +Lets first crack normal word comparisons, how did we arrive at the below conclusions?? We have a map in our head that |
| 6 | +a --1 b--2 or z>x |
| 7 | +hello , leetcode – true |
| 8 | +hello, hi – true |
| 9 | +app, apple – true |
| 10 | + |
| 11 | +Similarly we just give a defined map and ask the computer to compare like us. :) |
| 12 | + |
| 13 | +## Approach |
| 14 | + |
| 15 | +Use a orderMap array to store the new order of letters.(I prefer array over hashmap, since inserting takes time in hashmap). |
| 16 | +Write a custom compare method which returns false if the order has to be reverse. |
| 17 | + |
| 18 | +1. In the compare method initiate j = 0; |
| 19 | +2. write a while loop and check if j < s1.length and s2.length. |
| 20 | +3. compare each letter with the new orderMap. |
| 21 | +if letters are same j++; |
| 22 | +if letter of s1> s2 return true |
| 23 | +else return false |
| 24 | +4. Special case for words like app, apple, if s1.length<s2.length return false |
| 25 | +return true |
| 26 | + |
| 27 | +## Code |
| 28 | + |
| 29 | +```dart |
| 30 | +class Solution { |
| 31 | + List<int> orderMap = List.filled(26, 0); |
| 32 | + bool isAlienSorted(List<String> words, String order) { |
| 33 | + for (int i = 0; i < order.length; i++) { |
| 34 | + orderMap[order.codeUnitAt(i) - 'a'.codeUnitAt(0)] = i; |
| 35 | + } |
| 36 | +
|
| 37 | + for (int i = 1; i < words.length; i++) { |
| 38 | + if (!compare(words[i], words[i - 1])) return false; |
| 39 | + } |
| 40 | + return true; |
| 41 | + } |
| 42 | +
|
| 43 | + bool compare(String s1, String s2) { |
| 44 | + int j = 0; |
| 45 | + while (j < s1.length && j < s2.length) { |
| 46 | + if (s1[j] == s2[j]) |
| 47 | + j++; |
| 48 | + else if (orderMap[s1.codeUnitAt(j) - 'a'.codeUnitAt(0)] > |
| 49 | + orderMap[s2.codeUnitAt(j) - 'a'.codeUnitAt(0)]) |
| 50 | + return true; |
| 51 | + else |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (s1.length < s2.length) return false; |
| 55 | + return true; |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Approach |
| 61 | + |
| 62 | +The naive approach here would be to iterate through pairs of consecutive words in our input array (W) and compare the position of each letter in the input alphabet (O), moving letter by letter until we find a discrepancy and can determine which word comes first lexicographically. |
| 63 | + |
| 64 | +As this is an Easy question, this method works, but with a time complexity of O(N *M* P) where N is the length of W, M is the average length of each word in W, and P is the length of O. |
| 65 | + |
| 66 | +Rather than repetitively finding the position of a character in O, however, we can create a lookup table of indexes from O (alpha) at a time complexity of O(P) and turn every position lookup into a simple O(1) operation. That changes the overall time complexity to O(N * M + P). |
| 67 | + |
| 68 | +Then, as noted before, we can just iterate through word pairs (a, b) in W, then iterate through comparative characters (aChar, bChar) in those two words and evaluate them based on their lexicographical indexes (aix, bix). |
| 69 | + |
| 70 | +If aix < bix or if we reach the end of a, then the two words are in correct lexicographical order and we should move to the next pair of words. If aix > bix or if we reach the end of b, the two words are not in correct lexicographical order and we should return false. |
| 71 | + |
| 72 | +If we reach the end without exiting, we should return true. |
| 73 | + |
| 74 | +## Complexity |
| 75 | + |
| 76 | +Time complexity: O(N * M + P). |
| 77 | +Space complexity: O(n) |
| 78 | + |
| 79 | +## Code |
| 80 | + |
| 81 | +```dart |
| 82 | +import 'dart:collection'; |
| 83 | +
|
| 84 | +class Solution { |
| 85 | + bool isAlienSorted(List<String> words, String order) { |
| 86 | + HashMap<String, int> alpha = HashMap(); |
| 87 | + for (int i = 0; i < order.length; i++) alpha[order[i]] = i; |
| 88 | + for (int i = 1; i < words.length; i++) { |
| 89 | + String a = words[i - 1], b = words[i]; |
| 90 | + for (int j = 0; j < a.length; j++) { |
| 91 | + if (j == b.length) return false; |
| 92 | + String aChar = a[j], bChar = b[j]; |
| 93 | + if ((alpha[aChar] ?? 0) < (alpha[bChar] ?? 0)) break; |
| 94 | + if ((alpha[aChar] ?? 0) > (alpha[bChar] ?? 0)) return false; |
| 95 | + } |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Code = 3 |
| 103 | + |
| 104 | +```dart |
| 105 | +class Solution { |
| 106 | + List<int> x = List.filled(26, 0); |
| 107 | + bool isAlienSorted(List<String> words, String order) { |
| 108 | + for (int i = 0; i < order.length; ++i) |
| 109 | + x[order.codeUnitAt(i) - 'a'.codeUnitAt(0)] = i; |
| 110 | + for (int i = 0; i < words.length; ++i) { |
| 111 | + for (int j = i + 1; j < words.length; ++j) { |
| 112 | + String a = words[i], b = words[j]; |
| 113 | + int ml = max(a.length, b.length); |
| 114 | + for (int m = 0; m < ml; ++m) { |
| 115 | + int p = a.length > m ? x[a.codeUnitAt(m) - 'a'.codeUnitAt(0)] : -1; |
| 116 | + int q = b.length > m ? x[b.codeUnitAt(m) - 'a'.codeUnitAt(0)] : -1; |
| 117 | + if (p < q) break; |
| 118 | + if (p > q) return false; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return true; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
0 commit comments