Word Ladder #191008
-
Discussion TypeQuestion Discussion ContentTransform beginWord → endWord with minimum steps. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
class Solution { }; |
Beta Was this translation helpful? Give feedback.
-
|
this optimal solution |
Beta Was this translation helpful? Give feedback.
-
|
Use BFS to find the shortest transformation from beginWord to endWord. At each step, change one character (a–z) and check if the new word exists in the word list. Store words in an unordered set for fast lookup and remove them once visited to avoid repetition. As soon as you reach the endWord, return the number of steps. If no transformation is possible, return 0. |
Beta Was this translation helpful? Give feedback.
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector& wordList) {
unordered_set st(wordList.begin(), wordList.end());
if (!st.count(endWord)) return 0;
};