Skip to content

Commit eefe4bc

Browse files
author
Yi Gu
committed
Refactor code structure to make solutions more readable
1 parent babc029 commit eefe4bc

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

BFS/WordLadder.swift

+31-27
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,45 @@
1010

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

19-
while !queue.isEmpty {
20-
let (word, step) = queue.removeFirst()
15+
while !wordStepQueue.isEmpty {
16+
let (currentWord, currentStep) = wordStepQueue.removeFirst()
2117

22-
if word == endWord {
23-
return step
18+
if currentWord == endWord {
19+
return currentStep
2420
}
2521

26-
// transform word
27-
for i in 0..<word.count {
28-
var wordArray = Array(word)
22+
for word in neighbors(for: currentWord, in: wordSet) {
2923

30-
for char in "abcdefghijklmnopqrstuvwxyz" {
31-
guard char != wordArray[i] else {
32-
continue
33-
}
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))
44-
}
24+
wordStepQueue.append((word, currentStep + 1))
25+
wordSet.remove(word)
4526
}
4627
}
4728

4829
return 0
4930
}
31+
32+
private func neighbors(for word: String, in wordSet: Set<String>) -> [String] {
33+
var res = [String]()
34+
35+
// change character at every offset of the word
36+
for i in 0..<word.count {
37+
var tempWord = Array(word)
38+
39+
for charToReplace in "abcdefghijklmnopqrstuvwxyz" {
40+
guard charToReplace != tempWord[i] else {
41+
continue
42+
}
43+
44+
tempWord[i] = charToReplace
45+
let tempWordStr = String(tempWord)
46+
if wordSet.contains(tempWordStr) {
47+
res.append(tempWordStr)
48+
}
49+
}
50+
}
51+
52+
return res
53+
}
5054
}

DP/EditDistance.swift

+11-14
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@
77

88
class EditDistance {
99
func minDistance(word1: String, _ word2: String) -> Int {
10-
let aChars = [Character](word1.characters)
11-
let bChars = [Character](word2.characters)
12-
let aLen = aChars.count
13-
let bLen = bChars.count
10+
let word1Chars = Array(word1), word2Chars = Array(word2), m = word1.count, n = word2.count
11+
var distances = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
1412

15-
var dp = Array(count: aLen + 1, repeatedValue:(Array(count: bLen + 1, repeatedValue: 0)))
16-
17-
for i in 0...aLen {
18-
for j in 0...bLen {
13+
for i in 0...m {
14+
for j in 0...n {
1915
if i == 0 {
20-
dp[i][j] = j
16+
distances[i][j] = j
2117
} else if j == 0 {
22-
dp[i][j] = i
23-
} else if aChars[i - 1] == bChars[j - 1] {
24-
dp[i][j] = dp[i - 1][j - 1]
18+
distances[i][j] = i
19+
} else if word1Chars[i - 1] == word2Chars[j - 1] {
20+
distances[i][j] = distances[i - 1][j - 1]
2521
} else {
26-
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
22+
distances[i][j] = min(distances[i - 1][j - 1], distances[i - 1][j], distances[i][j - 1]) + 1
2723
}
24+
2825
}
2926
}
3027

31-
return dp[aLen][bLen]
28+
return distances[m][n]
3229
}
3330
}

0 commit comments

Comments
 (0)