diff --git a/swift/130-Surrounded-Regions.swift b/swift/130-Surrounded-Regions.swift new file mode 100644 index 000000000..876b9ccec --- /dev/null +++ b/swift/130-Surrounded-Regions.swift @@ -0,0 +1,48 @@ +class Solution { + func solve(_ board: inout [[Character]]) { + var rows = board.count + var columns = board[0].count + + // 1. DFS Capture unsurrounded regions (O -> T) + for r in 0.. X) + for r in 0.. O) + for r in 0..= 0, r < board.count, c >= 0, c < board[0].count, board[r][c] == "O" else { + return + } + + board[r][c] = Character("T") + + capture(r - 1, c, &board) + capture(r + 1, c, &board) + capture(r, c - 1, &board) + capture(r, c + 1, &board) + } +} \ No newline at end of file