My answer to this problem is very verbose and I didn’t like it but it works so ¯_(ツ)_/¯.
class Solution {
fun isAlienSorted(words: Array<String>, order: String): Boolean {
if(words.size == 1) return true
var orderChars = order.toCharArray().toList()
for(index in 1 until words.size){
val a = words[index-1]
val b = words[index]
when {
orderChars.indexOf(a[0]) > orderChars.indexOf(b[0]) -> return false
orderChars.indexOf(a[0]) == orderChars.indexOf(b[0]) -> {
if(a.length > b.length) return false
var ctr = 1
while(a[ctr] == b[ctr] && ctr < a.length && ctr < b.length){
ctr++
}
if(orderChars.indexOf(a[ctr]) > orderChars.indexOf(b[ctr])) return false
}
}
}
return true
}
}
Runtime: 172 ms, faster than 35.00% of Kotlin online submissions for Verifying an Alien Dictionary.
Memory Usage: 34.9 MB, less than 100.00% of Kotlin online submissions for Verifying an Alien Dictionary.