Shortest Word Ladder Between Two Words
Implement ladderLength
You are given a start word, a target word and a list of allowed words. A ladder moves from one word to the next by changing exactly one letter, and every word after the start must come from the list. Find the number of words in the shortest ladder from start to target, or 0 if there is none.
Think of the words as nodes of a graph, with an edge between two words that differ in exactly one letter. Because each step costs the same, a breadth-first search returns the shortest ladder.
Example 1:
Input: start = "hare", target = "core", words = ["care","core","bare","hire","cure","hard"]
Output: 3
Example 2:
Input: start = "hare", target = "core", words = ["care","bare","hire","cure","hard"]
Output: 0
Example 3:
Input: start = "mat", target = "cot", words = ["cat","cot"]
Output: 3
+ 14 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ words.length ≤ 12; every word (and start, target) has the same length between 1 and 5, all lowercase letters - ●
start ≠ target; the words in the list are distinct; start may or may not be in the list - ●
A ladder starts at start and ends at target; each next word must be in the list and differ from the previous word in exactly one letter - ●
Return the number of words in the shortest ladder (including start and target), or 0 if the target is not in the list or cannot be reached
start =
target =
words =