Valid Palindrome

🏠 ⬅️ ➡️

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama " Output: true Explanation: "amanaplanacanalpanama " is a palindrome.

Example 2:

Input: s = "race a car " Output: false Explanation: "raceacar " is not a palindrome.

Example 3:

Input: s = " " Output: true Explanation: s is an empty string " " after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.

Constraints:

  • 1 <= s.length <= 2 * 105
  • s consists only of printable ASCII characters.

Note: This problem is from LeetCode.
Compiled
Executed
⚠️ Correct
program main
    implicit none

    character(len=:), allocatable :: s
    logical :: result

    ! Example 1
    s = "A man, a plan, a canal: Panama "
    result = isPalindrome(s)
    write(*,*) "Example 1:"
    write(*,*) "Input: ", s
    write(*,*) "Output: ", result
    write(*,*) ""

    ! Example 2
    s = "race a car "
    result = isPalindrome(s)
    write(*,*) "Example 2:"
    write(*,*) "Input: ", s
    write(*,*) "Output: ", result
    write(*,*) ""

    ! Example 3
    s = "  "
    result = isPalindrome(s)
    write(*,*) "Example 3:"
    write(*,*) "Input: ", s
    write(*,*) "Output: ", result
    write(*,*) ""

contains

    function isPalindrome(s) result(result_)
        implicit none
        character(len=*), intent(in) :: s
        character(len=:), allocatable :: lowercase
        integer :: i, j
        logical :: result_

        lowercase = toLowerCase(s)
        i = 1
        j = len(lowercase)
        do while (i < j)
            if (lowercase(i:i) /= lowercase(j:j)) then
                result_ = .false.
                return
            end if
            i = i + 1
            j = j - 1
        end do
        result_ = .true.
    end function isPalindrome

    function toLowerCase(s) result(lowercase)
        implicit none
        character(len=*), intent(in) :: s
        character(len=:), allocatable :: lowercase
        integer :: i

        allocate(character(len=len(s)) :: lowercase)
        do i = 1, len(s)
            select case (s(i:i))
                case ('A':'Z')
                    lowercase(i:i) = char(iachar(s(i:i)) + 32)
                case default
                    lowercase(i:i) = s(i:i)
            end select
        end do
    end function toLowerCase

end program main
Compiled
Executed
Correct
PROGRAM is_palindrome

IMPLICIT NONE

CHARACTER(len=*) :: s
LOGICAL :: is_palindrome

is_palindrome = .TRUE.

! Convert all uppercase letters to lowercase
s = LOWER(s)

! Remove all non-alphanumeric characters
s = TRIM(s)

! Check if the string is a palindrome
DO i = 1, LEN(s)/2
    IF (s(i:i) /= s(LEN(s)-i+1:LEN(s)-i+1)) THEN
        is_palindrome = .FALSE.
        EXIT
    END IF
END DO

! Print the result
IF (is_palindrome) THEN
    WRITE (*,*) "The string is a palindrome."
ELSE
    WRITE (*,*) "The string is not a palindrome."
END IF

END PROGRAM is_palindrome
🌐 Data from online sources
def isPalindrome(s: str) -> bool:
    cleaned = "".join(c.lower() for c in s if c.isalnum())
    left, right = 0, len(cleaned) - 1
    while left < right:
        if cleaned[left] != cleaned[right]:
            return False
        left += 1
        right -= 1
    return True
  1. Clean the given string by removing non-alphanumeric characters and converting uppercase letters to lowercase.
  2. Initialize two pointers: left pointing to the beginning of the cleaned string and right pointing to the end.
  3. Iterate through the cleaning string, comparing the characters at the left and right pointers. If they are not equal, return false as it's not a palindrome.
  4. If the loop completes, it means the input string is a palindrome, so return true.
🌐 Data from online sources
#include <cctype>
#include <algorithm>

bool isPalindrome(std::string s) {
    s.erase(std::remove_if(s.begin(), s.end(), [](char c) { return !std::isalnum(c); }), s.end());
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    int left = 0, right = s.size() - 1;
    while (left < right) {
        if (s[left++] != s[right--]) return false;
    }
    return true;
}
  1. Clean the given string by removing non-alphanumeric characters and converting uppercase letters to lowercase.
  2. Initialize two pointers: left pointing to the beginning of the cleaned string and right pointing to the end.
  3. Iterate through the cleaning string, comparing the characters at the left and right pointers. If they are not equal, return false as it's not a palindrome.
  4. If the loop completes, it means the input string is a palindrome, so return true.