Answers for "Longest length of palindromic sum substring"

0

Longest length of palindromic sum substring

// Longest length of palindromic sum substring
fn longest_palindrome(s: String) -> usize {
    let size = s.len();
    if size == 0 {
        return 0;
    }
    let mut flags = vec![vec![false; size]; size];
    let mut start = 0;
    let mut max = 1;
    let b = s.as_bytes();
    for i in 0..size {
        flags[i][i] = true;
        for j in 0..i {
            flags[j][i] = b[j] == b[i] && (i-j < 3 || flags[j+1][i-1]);
            if flags[j][i] && i - j + 1 > max {
                start = j;
                max = i - j + 1;
            }
        }
    }
    let end = start + max;
    return ((s[start as usize..end as usize]).to_string()).len();
}

fn main() {
    let text = String::from("the quick brown fox haaa");  // 3
    println!("Length of longest palindrome = {} ", longest_palindrome(text)); 
}
Posted by: Guest on August-16-2021

Code answers related to "Longest length of palindromic sum substring"

Browse Popular Code Answers by Language