Skip to content

Instantly share code, notes, and snippets.

@muxcmux
Created January 23, 2020 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muxcmux/4b20bc5c60a83aaf350cffdd1ce99803 to your computer and use it in GitHub Desktop.
Save muxcmux/4b20bc5c60a83aaf350cffdd1ce99803 to your computer and use it in GitHub Desktop.
nikva idea za time/space complexity, no 100% bachka :D
# @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