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.

Rust Solution

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() {
        Solution::is_palindrome("A man, a plan, a canal: Panama".to_string()),
    assert_eq!(Solution::is_palindrome("race a car".to_string()), false);

Having problems with this solution? Click here to submit an issue on github.