125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false



  • s consists only of printable ASCII characters.

125. Valid Palindrome
struct Solution;

impl Solution {
    fn is_palindrome(s: String) -> bool {
        let s: Vec<char> = s
            .filter(|c| c.is_ascii_alphanumeric())
            .map(|c| c.to_ascii_lowercase())
        let a: String = s.iter().collect();
        let b: String = s.iter().rev().collect();
        a == b

fn test() {
    let s = "A man, a plan, a canal: Panama".to_string();
    let res = true;
    assert_eq!(Solution::is_palindrome(s), res);
    let s = "race a car".to_string();
    let res = false;
    assert_eq!(Solution::is_palindrome(s), res);