-
Notifications
You must be signed in to change notification settings - Fork 889
/
WordLadder.swift
60 lines (47 loc) · 1.82 KB
/
WordLadder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Question Link: https://leetcode.com/problems/word-ladder/
* Primary idea: BFS to go over all possible word paths until the word is exactly
* the same as end word, then the path should be the shortest one.
*
* Time Complexity: O(nm), Space Complexity: O(nm)
* n stands for # of words, m stands for length of a word
*
*/
class WordLadder {
func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int {
var wordSet = Set(wordList), wordQueue = [beginWord], count = 1
while !wordQueue.isEmpty {
let size = wordQueue.count
for _ in 0..<size {
let currentWord = wordQueue.removeFirst()
if currentWord == endWord {
return count
}
for word in neighbors(for: currentWord, in: wordSet) {
wordQueue.append(word)
wordSet.remove(word)
}
}
count += 1
}
return 0
}
private func neighbors(for word: String, in wordSet: Set<String>) -> [String] {
var res = [String]()
// change character at every offset of the word
for i in 0..<word.count {
var tempWord = Array(word)
for charToReplace in "abcdefghijklmnopqrstuvwxyz" {
guard charToReplace != tempWord[i] else {
continue
}
tempWord[i] = charToReplace
let tempWordStr = String(tempWord)
if wordSet.contains(tempWordStr) {
res.append(tempWordStr)
}
}
}
return res
}
}