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.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
Example 1: Input: A man, a plan, a canal: Panama Output: F Example 2: Input: race a car Output: F Example 3: Input: Output: T
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
temp.f95:6:24: 6 | LOGICAL :: is_palindrome | 1 Error: Symbol ‘is_palindrome’ at (1) cannot have a type temp.f95:8:14: 8 | is_palindrome = .TRUE. | 1 Error: ‘is_palindrome’ at (1) is not a variable temp.f95:19:22: 19 | is_palindrome = .FALSE. | 1 Error: ‘is_palindrome’ at (1) is not a variable temp.f95:25:18: 25 | IF (is_palindrome) THEN | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:27:4: 27 | ELSE | 1 Error: Unexpected ELSE statement at (1) temp.f95:29:3: 29 | END IF | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:17:4: 17 | DO i = 1, LEN(s)/2 | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:5:21: 5 | CHARACTER(len=*) :: s | 1 Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER temp.f95:11:4: 11 | s = LOWER(s) | 1 Error: Function ‘lower’ at (1) has no IMPLICIT type
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
left
pointing to the beginning of the cleaned string and right
pointing to the end.left
and right
pointers. If they are not equal, return false
as it's not a palindrome.true
.#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;
}
left
pointing to the beginning of the cleaned string and right
pointing to the end.left
and right
pointers. If they are not equal, return false
as it's not a palindrome.true
.