
g0001_0100.s0079_word_search.Solution.swift Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n)
// #2024_06_24_Time_162_ms_(96.28%)_Space_16.3_MB_(46.10%)
class Solution {
func exist(_ board: [[Character]], _ word: String) -> Bool {
var word = Array(word)
var beginnings = [(Int, Int)]()
for r in 0.. Bool{
let (r, c) = startP
let rows = visitedBoard.count
let cols = visitedBoard[0].count
//try four directions
//up
var has = false
if r - 1 >= 0{
if visitedBoard[r-1][c] == false{
if board[r-1][c] == word[targetIndex]{
if ((targetIndex + 1) == word.count){
return true
}
visitedBoard[r-1][c] = true
let tup = (r-1, c)
let res = proceed(tup, board, &visitedBoard, targetIndex+1, word)
visitedBoard[r-1][c] = false
if res == true{
return true
}
}
}
}
//left
if c - 1 >= 0{
if visitedBoard[r][c-1] == false{
if board[r][c-1] == word[targetIndex]{
if ((targetIndex + 1) == word.count){
return true
}
visitedBoard[r][c-1] = true
let tup = (r, c-1)
let res = proceed(tup, board, &visitedBoard, targetIndex+1, word)
visitedBoard[r][c-1] = false
if res == true{
return true
}
}
}
}
//down
if r + 1 < rows{
if visitedBoard[r+1][c] == false{
if board[r+1][c] == word[targetIndex]{
if ((targetIndex + 1) == word.count){
return true
}
visitedBoard[r+1][c] = true
let tup = (r+1, c)
let res = proceed(tup, board, &visitedBoard, targetIndex+1, word)
visitedBoard[r+1][c] = false
if res == true{
return true
}
}
}
}
//right
if c + 1 < cols{
if visitedBoard[r][c+1] == false{
if board[r][c+1] == word[targetIndex]{
if ((targetIndex + 1) == word.count){
return true
}
visitedBoard[r][c+1] = true
let tup = (r, c+1)
let res = proceed(tup, board, &visitedBoard, targetIndex+1, word)
visitedBoard[r][c+1] = false
if res == true{
return true
}
}
}
}
return false
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy