Skip to content

Commit 0eff8bc

Browse files
committed
leetcode
1 parent 0a61045 commit 0eff8bc

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
201201
- [**1137.** N-th Tribonacci Number](N-thTribonacciNumber/n_th_tribonacci_number.dart)
202202
- [**1626.** Best Team With No Conflicts](BestTeamWithNoConflicts/best_team_with_no_conflicts.dart)
203203
- [**1071.** Greatest Common Divisor of Strings](GreatestCommonDivisorOfStrings/greatest_common_divisor_of_strings.dart)
204+
- [**953.** Verifying an Alien Dictionary](VerifyingAnAlienDictionary/verifying_an_alien_dictionary.dart)
204205

205206
## Reach me via
206207

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
3+
4+
5+
-* 953. Verifying an Alien Dictionary *-
6+
7+
8+
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
9+
10+
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.
11+
12+
13+
14+
Example 1:
15+
16+
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
17+
Output: true
18+
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
19+
Example 2:
20+
21+
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
22+
Output: false
23+
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
24+
Example 3:
25+
26+
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
27+
Output: false
28+
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
29+
30+
31+
Constraints:
32+
33+
1 <= words.length <= 100
34+
1 <= words[i].length <= 20
35+
order.length == 26
36+
All characters in words[i] and order are English lowercase letters.
37+
38+
39+
*/
40+
import 'dart:collection';
41+
import 'dart:math';
42+
43+
class A {
44+
List<int> orderMap = List.filled(26, 0);
45+
bool isAlienSorted(List<String> words, String order) {
46+
for (int i = 0; i < order.length; i++) {
47+
orderMap[order.codeUnitAt(i) - 'a'.codeUnitAt(0)] = i;
48+
}
49+
50+
for (int i = 1; i < words.length; i++) {
51+
if (!compare(words[i], words[i - 1])) return false;
52+
}
53+
return true;
54+
}
55+
56+
bool compare(String s1, String s2) {
57+
int j = 0;
58+
while (j < s1.length && j < s2.length) {
59+
if (s1[j] == s2[j])
60+
j++;
61+
else if (orderMap[s1.codeUnitAt(j) - 'a'.codeUnitAt(0)] >
62+
orderMap[s2.codeUnitAt(j) - 'a'.codeUnitAt(0)])
63+
return true;
64+
else
65+
return false;
66+
}
67+
if (s1.length < s2.length) return false;
68+
return true;
69+
}
70+
}
71+
72+
class B {
73+
bool isAlienSorted(List<String> words, String order) {
74+
HashMap<String, int> alpha = HashMap();
75+
for (int i = 0; i < order.length; i++) alpha[order[i]] = i;
76+
for (int i = 1; i < words.length; i++) {
77+
String a = words[i - 1], b = words[i];
78+
for (int j = 0; j < a.length; j++) {
79+
if (j == b.length) return false;
80+
String aChar = a[j], bChar = b[j];
81+
if ((alpha[aChar] ?? 0) < (alpha[bChar] ?? 0)) break;
82+
if ((alpha[aChar] ?? 0) > (alpha[bChar] ?? 0)) return false;
83+
}
84+
}
85+
return true;
86+
}
87+
}
88+
89+
class C {
90+
List<int> x = List.filled(26, 0);
91+
bool isAlienSorted(List<String> words, String order) {
92+
for (int i = 0; i < order.length; ++i)
93+
x[order.codeUnitAt(i) - 'a'.codeUnitAt(0)] = i;
94+
for (int i = 0; i < words.length; ++i) {
95+
for (int j = i + 1; j < words.length; ++j) {
96+
String a = words[i], b = words[j];
97+
int ml = max(a.length, b.length);
98+
for (int m = 0; m < ml; ++m) {
99+
int p = a.length > m ? x[a.codeUnitAt(m) - 'a'.codeUnitAt(0)] : -1;
100+
int q = b.length > m ? x[b.codeUnitAt(m) - 'a'.codeUnitAt(0)] : -1;
101+
if (p < q) break;
102+
if (p > q) return false;
103+
}
104+
}
105+
}
106+
return true;
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)