Skip to content

Commit 5843473

Browse files
committed
Added Surrounded Regions
1 parent adb4bde commit 5843473

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

Diff for: src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ pub mod valid_palindrome; // 125
167167
pub mod word_ladder; // 127 ✓
168168
pub mod longest_consecutive_sequence; // 128 ✓
169169

170+
pub mod surrounded_regions; // 130 ✓
170171
pub mod palindrome_partitioning; // 131 ✓
171172
pub mod palindrome_partitioning_ii; // 132 ✓
172173

Diff for: src/surrounded_regions.rs

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/// You are given an `m x n` matrix `board` containing letters `X` and `O`, capture regions that
2+
/// are surrounded:
3+
///
4+
/// * Connect: A cell is connected to adjacent cells horizontally or vertically.
5+
///
6+
/// * Region: To form a region connect every `'O'` cell.
7+
///
8+
/// * Surround: The region is surrounded with `'X'` cells if you can connect the region with `'X'`
9+
/// cells and none of the region cells are on the edge of the board.
10+
///
11+
/// A surrounded region is captured by replacing all `'O'`s with `'X'`s in the input matrix
12+
/// `board`.
13+
struct Solution;
14+
15+
impl Solution {
16+
17+
fn capture(board: &mut Vec<Vec<char>>, i: usize, j: usize) {
18+
let m = board.len();
19+
let n = board[0].len();
20+
21+
if board[i][j] == 'O' {
22+
board[i][j] = 'C';
23+
if i > 0 {
24+
Self::capture(board, i - 1, j);
25+
}
26+
if j > 0 {
27+
Self::capture(board, i, j - 1);
28+
}
29+
if i < m-1 {
30+
Self::capture(board, i + 1, j);
31+
}
32+
if j < n-1 {
33+
Self::capture(board, i, j + 1);
34+
}
35+
}
36+
}
37+
38+
pub fn solve(board: &mut Vec<Vec<char>>) {
39+
let m = board.len();
40+
let n = board[0].len();
41+
42+
for i in 0..m {
43+
for j in 0..n {
44+
if board[i][j] == 'O' {
45+
if i == 0 || i == m-1 {
46+
Self::capture(board, i, j);
47+
} else if j == 0 || j == n-1 {
48+
Self::capture(board, i, j);
49+
}
50+
}
51+
}
52+
}
53+
54+
for i in 0..m {
55+
for j in 0..n {
56+
let value = board[i][j];
57+
if value == 'O' {
58+
board[i][j] = 'X';
59+
} else if value == 'C' {
60+
board[i][j] = 'O';
61+
}
62+
}
63+
}
64+
65+
}
66+
67+
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::Solution;
72+
73+
#[test]
74+
fn example_1() {
75+
let mut board = vec![
76+
vec!['X', 'X', 'X', 'X'],
77+
vec!['X', 'O', 'O', 'X'],
78+
vec!['X', 'X', 'O', 'X'],
79+
vec!['X', 'O', 'X', 'X'],
80+
];
81+
Solution::solve(&mut board);
82+
assert_eq!(board, vec![
83+
vec!['X', 'X', 'X', 'X'],
84+
vec!['X', 'X', 'X', 'X'],
85+
vec!['X', 'X', 'X', 'X'],
86+
vec!['X', 'O', 'X', 'X'],
87+
]);
88+
}
89+
90+
#[test]
91+
fn example_2() {
92+
let mut board = vec![
93+
vec!['X'],
94+
];
95+
Solution::solve(&mut board);
96+
assert_eq!(board, vec![
97+
vec!['X'],
98+
])
99+
}
100+
101+
#[test]
102+
fn example_3() {
103+
let mut board = vec![
104+
vec!['O', 'X', 'X', 'O', 'X'],
105+
vec!['X', 'O', 'O', 'X', 'O'],
106+
vec!['X', 'O', 'X', 'O', 'X'],
107+
vec!['O', 'X', 'O', 'O', 'O'],
108+
vec!['X', 'X', 'O', 'X', 'O'],
109+
];
110+
Solution::solve(&mut board);
111+
assert_eq!(board, vec![
112+
vec!['O', 'X', 'X', 'O', 'X'],
113+
vec!['X', 'X', 'X', 'X', 'O'],
114+
vec!['X', 'X', 'X', 'O', 'X'],
115+
vec!['O', 'X', 'O', 'O', 'O'],
116+
vec!['X', 'X', 'O', 'X', 'O'],
117+
]);
118+
}
119+
120+
}

0 commit comments

Comments
 (0)