Created
January 23, 2020 22:23
-
-
Save muxcmux/4b20bc5c60a83aaf350cffdd1ce99803 to your computer and use it in GitHub Desktop.
nikva idea za time/space complexity, no 100% bachka :D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param {String[]} words | |
# @param {String} order | |
# @return {Boolean} | |
def is_alien_sorted(words, order) | |
sorted = words.sort do |a, b| | |
cmp = [a.length, b.length].max.times do |i| | |
next if a[i] === b[i] | |
break i | |
end | |
if cmp >= a.length || cmp >= b.length | |
a.length <=> b.length | |
else | |
order.index(a[cmp]) <=> order.index(b[cmp]) | |
end | |
end | |
sorted === words | |
end | |
puts is_alien_sorted(["hello","leetcode"], "hlabcdefgijkmnopqrstuvwxyz") | |
puts is_alien_sorted(["word","world","row"], "worldabcefghijkmnpqstuvxyz") | |
puts is_alien_sorted(["apple","app"], "abcdefghijklmnopqrstuvwxyz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment