Loading content...
You are given a vocabulary transformation puzzle where you need to convert a source word into a destination word by following these rules:
A transformation chain is a sequence: sourceWord → word₁ → word₂ → ... → wordₖ where:
Your task is to determine the length of the shortest transformation chain (counting all words in the chain including both the source and destination words). If no valid transformation chain exists, return 0.
sourceWord = "hit"
destinationWord = "cog"
dictionary = ["hot","dot","dog","lot","log","cog"]5One valid shortest transformation chain is: "hit" → "hot" → "dot" → "dog" → "cog". This chain contains 5 words total. Each step changes exactly one character: h→o in position 1, then d→o in position 0, then o→d in position 2, and finally d→c in position 0.
sourceWord = "hit"
destinationWord = "cog"
dictionary = ["hot","dot","dog","lot","log"]0The destination word "cog" is not present in the dictionary. Without it being available as a valid word, there is no possible way to complete the transformation chain. Hence, we return 0.
sourceWord = "cat"
destinationWord = "bat"
dictionary = ["bat","rat","mat","hat"]2The source word "cat" can be directly transformed to "bat" by changing the first character 'c' to 'b'. Since "bat" is in the dictionary and it's our destination, the chain is simply "cat" → "bat" with length 2.
Constraints