-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathleetcode-127-word-ladder.swift
186 lines (137 loc) · 5.74 KB
/
leetcode-127-word-ladder.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/word-ladder/
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time.
Each transformed word must exist in the word list.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of elements in nums.
Space complexity O(n) where n is the amount of elements in nums.
*/
func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int {
// Set of words for O(1) contains complexity
var words: Set<String> = Set(wordList)
// If the endword is not part of the list, return 0
guard words.contains(endWord) else {
return 0
}
// Enqueue the first element at level 1
var queue: Queue = Queue()
queue.enqueue(beginWord, at: 1)
// While we still have elements in the queue
while !queue.isEmpty {
// Empty the whole queue
for _ in 0..<queue.count {
// Get current word and level
let (word, level) = queue.dequeue()!
// Get character Array from the word for easy index access
var characters: [Character] = Array(word)
/**
The idea is now to go through every character
of the character array and replace every character
one by one with every letter of the alphabet
and check if the new word is part of the word list.
*/
for (j, character) in characters.enumerated() {
for letter in "abcdefghijklmnopqrstuvwxyz" where letter != character {
// Swap the current letter
characters[j] = letter
// Get the new word
let newWord = String(characters)
// If it's the end word, then we're done
if newWord == endWord {
return level + 1
}
// If not, check if it's part of the word list
if words.contains(newWord) {
// If so, add it to the queue at the next level
queue.enqueue(newWord, at: level + 1)
/**
And remove it from the word list, so we don't run into
stackoverflow where we're in a cycle of the same
2 words over and over again.
*/
words.remove(newWord)
}
}
/**
Lastly set the original character back
for the next iteration.
*/
characters[j] = character
}
}
}
/**
If we never returnes a level above
it means, that the word is not in the set.
Technically we should never arrive here,
as we already checked for this case.
So might want to add additional error logs here.
*/
return 0
}
/**
Simple Queue Stack implementation.
All operations are O(1) in time and O(n) in space.
Where n is the number of Strings of the wordList
*/
struct Queue {
private var enqueueArray: [(word: String, level: Int)] = []
private var dequeueArray: [(word: String, level: Int)] = []
init() {}
public var count: Int {
return enqueueArray.count + dequeueArray.count
}
public var isEmpty: Bool {
return count < 1
}
public mutating func enqueue(_ element: String, at level: Int) {
enqueueArray.append((word: element, level: level))
}
public mutating func dequeue() -> (word: String, level: Int)? {
if dequeueArray.isEmpty {
dequeueArray = enqueueArray.reversed()
enqueueArray.removeAll()
}
return dequeueArray.popLast()
}
}