Skip to content

Commit ce6fbca

Browse files
Create 0953-verifying-an-alien-dictionary.go
Accepted submission: _https://leetcode.com/submissions/detail/870816086/_
1 parent e8b926e commit ce6fbca

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func isAlienSorted(words []string, order string) bool {
2+
// first differing char
3+
// if word A is prefix of word B, word B must be AFTER word A
4+
orderInd := make(map[byte]int)
5+
for i := 0; i < len(order); i++ {
6+
orderInd[order[i]] = i
7+
}
8+
9+
for i := 0; i < len(words) - 1; i++ {
10+
w1, w2 := words[i], words[i + 1]
11+
12+
for j := 0; j < len(w1); j++ {
13+
if j == len(w2) {
14+
return false
15+
}
16+
17+
if w1[j] != w2[j] {
18+
if orderInd[w2[j]] < orderInd[w1[j]] {
19+
return false
20+
}
21+
break
22+
}
23+
}
24+
}
25+
return true
26+
}

0 commit comments

Comments
 (0)