Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
Given two words,
Example 1:
Example 2:
while(0 < queue.length){let map = {} if (!wordList.includes(endWord)) return 0 if (!wordList.includes(beginWord)) { wordList.push(beginWord) }for (let word of wordList) { for (let i = 0; i < word.length; i++) { let pattern = word.substring(0, i) + '*' + word.substring(i + 1) if (!map[pattern]) map[pattern] = [] map[pattern].push(word) } }while (0 < queue.length) { const [currentWord, level] = queue.shift() for (let i = 0; i < currentWord.length; i++) { let pattern = currentWord.substring(0, i) + '*' + currentWord.substring(i + 1) // *ot, h*t, ho* // for each pattern i will find form th map let neighbours = map[pattern] || [] for (let neighbour of neighbours) { if (neighbour === endWord) { return level + 1 } if (!seen.has(neighbour)) { seen.add(neighbour) queue.push([neighbour, level + 1]) } } } } return 0