Skip to content

Commit 45126d3

Browse files
committed
Merge branch 'master' of github.com:soapyigu/LeetCode_Swift
2 parents 4032088 + a9e1400 commit 45126d3

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

BFS/WordLadder.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,41 @@
1010

1111
class WordLadder {
1212
func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int {
13-
var words = Set(wordList)
14-
var qWordStep = [(beginWord, 1)]
13+
guard beginWord.count == endWord.count else {
14+
return 0
15+
}
16+
17+
var queue = [(beginWord, 1)], wordSet = Set<String>(wordList)
1518

16-
while !qWordStep.isEmpty {
17-
let (currentWord, currentStep) = qWordStep.removeFirst()
19+
while !queue.isEmpty {
20+
let (word, step) = queue.removeFirst()
1821

19-
if currentWord == endWord {
20-
return currentStep
22+
if word == endWord {
23+
return step
2124
}
2225

23-
for (i, currentWordChar) in currentWord.enumerated() {
26+
// transform word
27+
for i in 0..<word.count {
28+
var wordArray = Array(word)
29+
2430
for char in "abcdefghijklmnopqrstuvwxyz" {
25-
if char != currentWordChar {
26-
let newWord = currentWord.replace(at: i, to: char)
27-
28-
if words.contains(newWord) {
29-
qWordStep.append((newWord, currentStep + 1))
30-
31-
words.remove(newWord)
32-
}
31+
guard char != wordArray[i] else {
32+
continue
3333
}
34+
35+
wordArray[i] = char
36+
let transformedWord = String(wordArray)
37+
38+
guard wordSet.contains(transformedWord) else {
39+
continue
40+
}
41+
42+
wordSet.remove(transformedWord)
43+
queue.append((transformedWord, step + 1))
3444
}
3545
}
3646
}
3747

3848
return 0
3949
}
4050
}
41-
42-
extension String {
43-
func replace(at index: Int, to char: Character) -> String {
44-
var chars = Array(self)
45-
46-
chars[index] = char
47-
48-
return String(chars)
49-
}
50-
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@
184184
| ----- | -------- | ---------- | ---- | ----- |
185185
[Same Tree](https://oj.leetcode.com/problems/same-tree/)| [Swift](./Tree/SameTree.swift)| Easy| O(n)| O(n)|
186186
[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [Swift](./Tree/SymmetricTree.swift)| Easy| O(n)| O(n)|
187-
[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [Swift](./Tree/InvertBinaryTree)| Easy| O(n)| O(n)|
188-
[Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/)| [Swift](./Tree/BinaryTreeUpsideDown)| Medium| O(n)| O(1)|
187+
[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [Swift](./Tree/InvertBinaryTree.swift)| Easy| O(n)| O(n)|
188+
[Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/)| [Swift](./Tree/BinaryTreeUpsideDown.swift)| Medium| O(n)| O(1)|
189189
[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| [Swift](./Tree/MinimumDepthOfBinaryTree.swift)| Easy| O(n)| O(1)|
190190
[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [Swift](./Tree/MaximumDepthOfBinaryTree.swift)| Easy| O(n)| O(1)|
191191
[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)| [Swift](./Tree/DiameterBinaryTree.swift)| Easy| O(n)| O(1)|

0 commit comments

Comments
 (0)