impl Solution { fn check(s: &String, start: usize, c: i32) -> bool { let mut count = c; if count < 0 { return false; }
for i in start..s.len() { let c = s.chars().nth(i).unwrap(); if c == '(' { count += 1; } else if c == ')' { if count <= 0 { return false; } count -= 1; } else if c == '*' { return Self::check(s, i + 1, count + 1) || Self::check(s, i + 1, count - 1) || Self::check(s, i + 1, count); } } return count == 0; }
pub fn check_valid_string(s: String) -> bool { return Self::check(&s, 0, 0); } }
|