Skip to content

Instantly share code, notes, and snippets.

@wannabefro
Created April 18, 2014 21:27
Show Gist options
  • Save wannabefro/11065160 to your computer and use it in GitHub Desktop.
Save wannabefro/11065160 to your computer and use it in GitHub Desktop.
def ladder_length(start_word, end_word, dictionary)
current_word = start_word
counter = 1
while !matching_letters(current_word, end_word, end_word.length - 1)
dictionary.each do |word|
if matching_letters(current_word, word, word.length - 1)
current_word = word
dictionary.delete(word)
counter += 1
end
end
end
counter += 1
end
def matching_letters(word1, word2, matching)
matching_letters = 0
word1.split('').each_with_index do |char, position|
if char == word2[position]
matching_letters += 1
end
end
matching_letters == matching
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment