20250301 finished.

This commit is contained in:
jackfiled 2025-03-01 11:32:12 +08:00
parent 41bf26def6
commit 2dd2c8dd84
2 changed files with 65 additions and 0 deletions

View File

@ -510,3 +510,5 @@ mod p2502_design_memory_allocator;
mod p1472_design_browser_history;
mod p2353_design_a_food_rating_system;
mod p131_palindrome_partitioning;

View File

@ -0,0 +1,63 @@
/**
* [131] Palindrome Partitioning
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn partition(s: String) -> Vec<Vec<String>> {
let s: Vec<char> = s.chars().collect();
let length = s.len();
let mut result = vec![];
let mut current: Vec<String> = vec![];
let mut dp = vec![vec![true; length]; length];
for i in (0..length).rev() {
for j in i + 1..length {
dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1];
}
}
Self::dfs(&s, 0, &mut dp, &mut current, &mut result);
result
}
fn dfs(
s: &Vec<char>,
pos: usize,
dp: &mut Vec<Vec<bool>>,
current: &mut Vec<String>,
result: &mut Vec<Vec<String>>,
) {
if pos >= s.len() {
result.push(current.clone());
return;
}
for i in pos..s.len() {
if dp[pos][i] {
current.push(s[pos..i + 1].iter().collect());
Self::dfs(s, i + 1, dp, current, result);
current.pop();
}
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_131() {
assert_eq!(
vec![vec_string!("a", "a", "b"), vec_string!("aa", "b")],
Solution::partition("aab".to_owned())
);
}
}