Skip to content

Commit 1fb7b93

Browse files
committed
1138_Alphabet_Board_Path
1 parent 5c73abf commit 1fb7b93

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
613613
|47| [1245. Tree Diameter](https://tinyurl.com/yxq4uykv) | [Python](https://tinyurl.com/wu6rdaw/1245_Tree_Diameter.py), [Swift](https://tinyurl.com/wuja3c4/1245_Tree_Diameter.swift) | [Art 1](https://www.geeksforgeeks.org/longest-path-undirected-tree/) | Medium |📌 Learned, 2 BFS. Important and interesting |
614614
|48| [1376. Time Needed to Inform All Employees](https://tinyurl.com/y3e5bb22) | [Python](https://tinyurl.com/wu6rdaw/1376_Time_Needed_to_Inform_All_Employees.py), [Swift](https://tinyurl.com/wuja3c4/1376_Time_Needed_to_Inform_All_Employees.swift) | [Art 1](https://tinyurl.com/y2lw6s4j) | Medium |📌 BFS, DFS |
615615
|49| [286. Walls and Gates](https://tinyurl.com/y3ollzsl) | [Python](https://tinyurl.com/wu6rdaw/286_Walls_and_Gates.py), [Swift](https://tinyurl.com/wuja3c4/286_Walls_and_Gates.swift) | --- | Medium |📌 BFS, DFS |
616+
|50| [286. Walls and Gates](https://tinyurl.com/y2xk6r98) | [Python](https://tinyurl.com/wu6rdaw/1138_Alphabet_Board_Path.py), [Swift](https://tinyurl.com/wuja3c4/1138_Alphabet_Board_Path.swift) | --- | Medium |📌 BFS |
616617

617618

618619
</p>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Foundation
2+
class Solution {
3+
func alphabetBoardPath(_ target: String) -> String {
4+
5+
let board_array = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
6+
.map { Array($0)
7+
.map { String($0) } }
8+
9+
let target_array = Array(target)
10+
.map { String($0) }
11+
12+
var current_target_index = 0
13+
var queue = [(Int, Int, String)]()
14+
var visitedSet = Set<String>()
15+
var result = ""
16+
17+
queue.append((0, 0, ""))
18+
visitedSet.insert(board_array[0][0])
19+
20+
while queue.isEmpty == false {
21+
var (row, col, path) = queue.removeFirst()
22+
23+
if board_array[row][col] == target_array[current_target_index] {
24+
25+
path += "!"
26+
current_target_index += 1
27+
visitedSet.removeAll()
28+
queue.removeAll()
29+
visitedSet.insert(board_array[row][col])
30+
queue.append((row, col, path))
31+
32+
if current_target_index >= target_array.count {
33+
result = path
34+
break
35+
} else {
36+
continue
37+
}
38+
}
39+
40+
for neighbor in [ (-1,0,"U"), (0,1,"R"), (1,0,"D"), (0,-1,"L") ] {
41+
let (new_row, new_col, direction) = (row + neighbor.0, col + neighbor.1, neighbor.2)
42+
if new_row < board_array.count &&
43+
new_row >= 0 &&
44+
new_col < board_array[new_row].count &&
45+
new_col >= 0 &&
46+
!visitedSet.contains(board_array[new_row][new_col]) {
47+
48+
visitedSet.insert(board_array[new_row][new_col])
49+
queue.append((new_row, new_col, path + direction))
50+
}
51+
}
52+
}
53+
return result
54+
}
55+
}

0 commit comments

Comments
 (0)