Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79. Word Search
  • Loading branch information
Mahim1997 committed Sep 22, 2022
commit 6405c35f45e8d9dae253dca890aab0a01a1c277c
94 changes: 94 additions & 0 deletions swift/79-Word-Search.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class Solution {
let dirX: [Int] = [-1, 1, 0, 0]
let dirY: [Int] = [0, 0, -1, 1]

func exist(_ board: [[Character]], _ word: String) -> Bool {
let ROWS = board.count
let COLS = board[0].count

var visited = [[Bool]](
repeating: [Bool](repeating: false, count: COLS),
count: ROWS
)

var wordArr: [Character] = Array(word)

for r in 0..<ROWS {
for c in 0..<COLS {
visited[r][c] = true

if dfs(
startRow: r,
startCol: c,
&visited,
board,
&wordArr,
startIdx: 0
) == true {
return true
}

visited[r][c] = false
}
}

return false
}
}

private extension Solution {
func isWithinBounds(row: Int, col: Int, _ board: [[Character]]) -> Bool {
((row >= 0) && (col >= 0) &&
(row < board.count) && (col < board[0].count))
}


func dfs(
startRow row: Int,
startCol col: Int,
_ visited: inout [[Bool]],
_ board: [[Character]],
_ wordArr: inout [Character],
startIdx index: Int
) -> Bool {
let strLen = wordArr.count

// Last index check
if index == (strLen - 1) {
return board[row][col] == wordArr[index]
}

// Edge case
guard index < strLen else { return false }
guard board[row][col] == wordArr[index] else { return false }

for (dX, dY) in zip(dirX, dirY) {
let rowNew = row + dX
let colNew = col + dY

guard isWithinBounds(row: rowNew, col: colNew, board) else { continue }
guard visited[rowNew][colNew] == false else { continue }

// Visit
visited[rowNew][colNew] = true

// Recursive DFS
if dfs(
startRow: rowNew,
startCol: colNew,
&visited,
board,
&wordArr,
startIdx: index + 1
) == true {
return true
}

// Unvisit [for backtrack]
visited[rowNew][colNew] = false
}

return false
}
}