-
-
Save anonymous/bdf3a70f4dee578b2d23 to your computer and use it in GitHub Desktop.
This file contains 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
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