Open jason89521 opened 3 months ago
Link: https://leetcode.com/problems/word-ladder/description/
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { unordered_set<string> dict(wordList.begin(), wordList.end()); queue<string> strQueue; strQueue.push(beginWord); int level = 0; while (!strQueue.empty()) { level++; auto levelSize = strQueue.size(); for (int i = 0; i < levelSize; i++) { auto str = strQueue.front(); strQueue.pop(); if (str == endWord) { return level; } for (int j = 0; j < str.length(); j++){ auto c = str[j]; for (int letter = 'a'; letter <= 'z'; letter++) { str[j] = letter; auto it = dict.find(str); if (it != dict.end()) { if (str != beginWord) { strQueue.push(*it); } dict.erase(it); } } str[j] = c; } } } return 0; } };
Time complexity:
Practice Dates
Description
Link: https://leetcode.com/problems/word-ladder/description/
Solution
Performance
Time complexity: