Skip to content

Instantly share code, notes, and snippets.

/big_o.rb Secret

Created December 9, 2015 20:57
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 anonymous/bdf3a70f4dee578b2d23 to your computer and use it in GitHub Desktop.
Save anonymous/bdf3a70f4dee578b2d23 to your computer and use it in GitHub Desktop.
word_chain = ['rap', 'trap', 'party', 'pastry']
exit unless word_chain.first.length === 3 # O(1)
result = word_chain.each_with_index do |word, index| # O(n)
next if index == 0 # O(1)
previous_word = word_chain[index - 1] # O(1)
split_word = word.split(//) # O(1)
split_previous_word = previous_word.split(//) # O(1)
break unless split_word.length == split_previous_word.length + 1 # O(1)
break unless (split_word - split_previous_word).length < 2 # O(1)
end
p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment