start and target of the same length and a dictionary dictionary. Walk from start to target along a chain of words where each word differs from the previous one in exactly one letter at one position. Every word in the chain except start itself must come from the dictionary; start does not have to be in it.
Return the number of words in the shortest such chain, counting both start and target. If target cannot be reached, return 0.start='cold', target='warm', dictionary=['cord', 'card', 'ward', 'warm', 'word', 'worm'] → 5
A shortest chain is cold → cord → card → ward → warm, which has 5 words. Going through word and worm also gives 5.
start='cold', target='warm', dictionary=['cord', 'card', 'ward'] → 0
The word "warm" is not in the dictionary, so no chain can end there.
start='ab', target='cb', dictionary=['cb'] → 2
The words differ in one letter, so the chain has two words.
start.length ≤ 10target.length == start.lengthdictionary.length ≤ 5000dictionary[i].length == start.lengthstart, target and dictionary[i] consist of lowercase English letters onlystart ≠ targetstart='cold', target='warm', dictionary=['cord', 'card', 'ward', 'warm', 'word', 'worm']
5
Run checks the first 3 tests in your browser. Submit checks all 14 tests on the server, hidden ones included.