Word ladder
Hardgraphbfsstringhash set
Turn begin into end by changing one letter per step, where every intermediate word must be in words. Return the number of words in the shortest chain (counting begin and end), or 0 if impossible. end must be in words.
Example 1
Input: begin = "hit", end = "cog", words = ["hot","dot","dog","lot","log","cog"] Output: 5
hit → hot → dot → dog → cog: 5 words.
Example 2
Input: begin = "hit", end = "cog", words = ["hot","dot","dog","lot","log"] Output: 0
Constraints
- 1 ≤ begin.length ≤ 10
- All words have the same length and are lowercase
- 1 ≤ words.length ≤ 5000
- begin ≠ end
Submit checks your solution against 8 tests, including hidden ones.
/**
* @param {string} begin
* @param {string} end
* @param {string[]} words
* @return {int}
*/
function ladderLength(begin, end, words) {
}