2 Commits 9e95df2918 ... 6bdc29d871

Author SHA1 Message Date
  Zhang Li 6bdc29d871 feat: palindrome_partition 1 year ago
  Zhang Li 1789298815 feat: surrounded_regions 1 year ago

+ 2 - 1
src/solutions/bt/mod.rs

@@ -13,4 +13,5 @@ mod subsets;
 mod word_search;
 mod generate_parentheses;
 mod subsets2;
-mod restore_ip_addresses;
+mod restore_ip_addresses;
+mod surrounded_regions;

+ 82 - 0
src/solutions/bt/surrounded_regions.rs

@@ -0,0 +1,82 @@
+use std::collections::HashSet;
+
+struct Solution;
+
+impl Solution {
+    pub fn solve(board: &mut Vec<Vec<char>>) {
+        let rows = board.len();
+        let cols = board[0].len();
+        let mut visited = vec![vec![false; cols]; rows];
+        for i in 0..rows {
+            for j in 0..cols {
+                if  board[i][j] == 'X' || visited[i][j] { continue }
+                let mut connected = HashSet::new();
+                let mut res = true;
+                Solution::helper(board, rows, cols, &mut visited, (i, j), &mut res, &mut connected);
+                if res {
+                    // println!("connected: {:?}", connected);
+                    for (ii, jj) in connected.into_iter() {
+                        board[ii][jj] = 'X';
+                    }
+                }
+
+            }
+        }
+
+    }
+
+    fn helper(board: &mut Vec<Vec<char>>, rows: usize, cols: usize,  visited: &mut Vec<Vec<bool>>, cur: (usize, usize), res: &mut bool, connected: &mut HashSet<(usize, usize)> ) {
+        let (ii, jj) = cur;
+        visited[ii][jj] = true;
+        if board[ii][jj] == 'X' {
+            return;
+        }
+        if ii == rows -1 || jj == cols - 1 || ii == 0 || jj == 0{
+            *res = false;
+        }
+
+
+
+        connected.insert((ii, jj));
+        let i = ii as i32;
+        let j = jj as i32;
+        let candidates = vec![(i, j-1), (i, j+1), (i-1, j), (i+1, j)];
+        let candidates = candidates.into_iter().filter(|(i, j) | *i >= 0 && *j >= 0 && *i < rows as i32 && *j < cols as i32).map(|(i, j)| (i as usize, j as usize)).collect::<Vec<(usize, usize)>>();
+
+        for (i, j) in candidates {
+            if visited[i][j] {
+                continue;
+            }
+            Solution::helper(board, rows, cols, visited, (i, j), res, connected);
+
+        }
+
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_solve() {
+        let mut board = vec![vec!['X','X','X','X'],vec!['X','O','O','X'],vec!['X','X','O','X'],vec!['X','O','X','X']];
+        Solution::solve(&mut board);
+        let expected = vec![vec!['X','X','X','X'],vec!['X','X','X','X'],vec!['X','X','X','X'],vec!['X','O','X','X']];
+        assert_eq!(board, expected);
+
+
+    }
+
+    #[test]
+    fn test_solve1() {
+        let mut board = vec![vec!['O','X','O','O','O','O','O','O','O'],vec!['O','O','O','X','O','O','O','O','X'],vec!['O','X','O','X','O','O','O','O','X'],vec!['O','O','O','O','X','O','O','O','O'],vec!['X','O','O','O','O','O','O','O','X'],vec!['X','X','O','O','X','O','X','O','X'],vec!['O','O','O','X','O','O','O','O','O'],vec!['O','O','O','X','O','O','O','O','O'],vec!['O','O','O','O','O','X','X','O','O']];
+        Solution::solve(&mut board);
+        let expected = vec![vec!['O','X','O','O','O','O','O','O','O'],vec!['O','O','O','X','O','O','O','O','X'],vec!['O','X','O','X','O','O','O','O','X'],vec!['O','O','O','O','X','O','O','O','O'],vec!['X','O','O','O','O','O','O','O','X'],vec!['X','X','O','O','X','O','X','O','X'],vec!['O','O','O','X','O','O','O','O','O'],vec!['O','O','O','X','O','O','O','O','O'],vec!['O','O','O','O','O','X','X','O','O']];
+        assert_eq!(board, expected);
+
+
+    }
+
+}

+ 2 - 1
src/solutions/dp/mod.rs

@@ -13,4 +13,5 @@ mod unique_binary_search_trees;
 mod distinct_subsequences;
 mod best_time_to_buy_and_sell_stock;
 mod best_time_to_buy_and_sell_stock2;
-mod best_time_to_buy_and_sell_stock3;
+mod best_time_to_buy_and_sell_stock3;
+mod palindrome_partitioning;

+ 81 - 0
src/solutions/dp/palindrome_partitioning.rs

@@ -0,0 +1,81 @@
+struct Solution;
+
+
+impl Solution {
+    pub fn partition(s: String) -> Vec<Vec<String>> {
+        let mut res = vec![];
+        let valid = Solution::valid_palindrome(&s[..]);
+        let mut cur = vec![];
+        Solution::helper(&s[..], &valid, 0, s.len(), &mut cur, &mut res);
+
+        res
+
+    }
+
+    fn helper(s: &str, valid: &Vec<Vec<bool>>, begin: usize, n: usize, cur: &mut Vec<String>, res: &mut Vec<Vec<String>>) {
+        if begin == n {
+            res.push(cur.clone());
+            return
+        }
+
+        for end in begin..n {
+            if !valid[begin][end] {
+                continue;
+            } else {
+                cur.push(s[begin..(end+1)].to_string());
+                Solution::helper(s, valid, end+1, n, cur, res);
+                cur.pop();
+            }
+        }
+
+    }
+
+    fn valid_palindrome(s: &str) ->  Vec<Vec<bool>> {
+        let s = s.chars().collect::<Vec<char>>();
+        let n = s.len();
+        let mut res = vec![vec![false; n]; n];
+
+        for i in 0..n {
+            res[i][i] = true;
+        }
+        for l in 2..=n {
+            for i in 0..(n-l+1) {
+                let j = i + l - 1;
+                if l == 2 {
+                    if s[i] == s[j] {
+                        res[i][j] = true;
+                    }
+
+                } else {
+                    res[i][j] = res[i+1][j-1] && s[i] == s[j]
+                }
+
+            }
+        }
+
+
+
+        res
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_partition() {
+        let s = String::from("aab");
+
+        let res = Solution::partition(s);
+        assert_eq!(res, vec![vec!["a","a","b"], vec!["aa","b"]]);
+    }
+
+    #[test]
+    fn test_partition1() {
+        let s = String::from("a");
+
+        let res = Solution::partition(s);
+        assert_eq!(res, vec![vec!["a"]]);
+    }
+}