use std::str::*;
impl Solution {
pub fn partition(s: String) -> Vec<Vec<String>> {
let bytes = s.as_bytes();
let n = bytes.len();
let mut results = vec![];
Self::partition_helper(bytes, (0, n), &mut vec![], &mut results);
results
}
pub fn partition_helper(s: &[u8], remaining: (usize, usize), holder: &mut Vec<(usize, usize)>, acc: &mut Vec<Vec<String>>) {
let l = remaining.1 - remaining.0;
if l == 0 {
let mut results = vec![];
for &(start, end) in holder.iter() {
results.push(unsafe { from_utf8_unchecked(&s[start..=end]) }.to_owned());
}
acc.push(results);
return;
}
let a = remaining.0;
let b = remaining.1;
for i in a..b {
let mut start = i;
let mut end = i;
while start > a && end < (b - 1) && s[start - 1] == s[end + 1] {
start -= 1;
end += 1;
}
if start == a {
holder.push((start, end));
Self::partition_helper(s, (end + 1, b), holder, acc);
holder.pop();
}
}
for i in a..(b - 1) {
let mut start = i;
let mut end = i + 1;
if s[start] != s[end] {
continue;
}
while start > a && end < (b - 1) && s[start - 1] == s[end + 1] {
start -= 1;
end += 1;
}
if start == a {
holder.push((start, end));
Self::partition_helper(s, (end + 1, b), holder, acc);
holder.pop();
}
}
}
}