題目描述:給定 beginWord、endWord 和字典 wordList,找出從 beginWord 到 endWord 的最短轉換序列長度。每次轉換只能改變一個字母,且每個中間單字必須存在於 wordList 中。若無法抵達則回傳 0。
解題思路:此題本質是在隱式圖上求最短路徑,BFS 能保證找到最少步數。將 wordList 存入雜湊集合以支援 O(1) 查詢。從 beginWord 出發,BFS 的每一層代表一次轉換,層數即為序列長度。對於每個當前單字,嘗試將其每個位置替換為 'a' 到 'z' 的所有字母(共 L×26 種),若替換後的新字存在於 wordSet 中且尚未訪問,則加入佇列並從 wordSet 移除(避免重複訪問)。當彈出的字等於 endWord 時,回傳當前層數。BFS 結束後若未找到,回傳 0。
時間複雜度:O(L² × N) — N 為字典大小,L 為單字長度。BFS 最多訪問 N 個單字,對每個單字嘗試 L 個位置、26 個字母,雜湊集合查詢為 O(L)(字串比較),故總體 O(L² × N)。
空間複雜度:O(L × N) — wordSet 存 N 個長度 L 的字串,BFS 佇列最多存放 O(N) 個字串。
1. Put all words from wordList into a hash set (wordSet)
2. If endWord not in wordSet, return 0
3. Initialize BFS queue with beginWord; remove beginWord from wordSet; steps = 1
4. While queue is not empty:
a. Process all words in the current BFS level (levelSize = queue size)
b. For each word in this level:
- If word == endWord, return steps
- For each position pos in word (0 to L-1):
- For each letter c from 'a' to 'z':
- Replace word[pos] with c
- If modified word exists in wordSet:
- Remove it from wordSet (mark visited)
- Add it to queue
- Restore word[pos] to original character
c. Increment steps
5. Return 0 (endWord unreachable)方法一:雙向 BFS(Bidirectional BFS)
同時從 beginWord 和 endWord 展開 BFS,每次選擇較小的邊界集合展開。當兩端的邊界集合有交集時,找到最短路徑。相比單向 BFS,搜索空間從 O(b^d) 降低到 O(b^(d/2)),其中 b 為分支因子,d 為最短距離。實作較複雜,但對長距離轉換能顯著加速。
方法二:通用模式預處理(Generic Pattern Preprocessing)
預先為每個單字建立「通用模式」(Generic State),如 hit 的模式為 *it、h*t、hi*。用雜湊表將相同模式的單字歸為一組。BFS 時對每個單字查詢其所有模式對應的鄰居,省去逐字母枚舉。建圖 O(L × N),BFS O(L × N),整體 O(L × N),且常數因子較小(省去 26 的枚舉)。適合字典極大的場景。