Untitled
unknown
plain_text
2 years ago
922 B
1
Indexable
Never
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string> wordList) { set<string> myset; bool isPresent = false; for (auto word : wordList) { if (word == endWord) { isPresent = true; } myset.insert(word); } if (isPresent == false) return 0; queue<string> myqueue; myqueue.push(beginWord); int count = 0; while (!myqueue.empty()) { int size = myqueue.size(); count++; while (size) { string curr = myqueue.front(); myqueue.pop(); for (int i = 0; i < curr.length(); i++) { string temp = curr; for (char tempLetter = 'a'; tempLetter <= 'z'; tempLetter++) { temp[i] = tempLetter; if (temp == endWord) return count + 1; if (myset.find(temp) != myset.end()) { myqueue.push(temp); myset.erase(temp); } } } size--; } } return 0; } };