Given a string s, return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = "aba " Output: true

Example 2:

Input: s = "abca " Output: true Explanation: You could delete the character 'c'.

Example 3:

Input: s = "abc " Output: false

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.

Note: This problem is from LeetCode.
āŒ Compiled
āŒ Executed
āŒ Correct
program main
    implicit none

    integer, parameter :: ntests = 3
    character(len=:), allocatable :: tests(:)
    logical :: results(:)

    tests = ["aba", "abca", "abc"]
    allocate(results(size(tests)))

    results = .false.
    do i = 1, size(tests)
        results(i) = canPalindrome(tests(i))
    end do

    write(*, "(A)") "Results:"
    do i = 1, size(tests)
        write(*, "(A, L1)") trim(tests(i)), results(i)
    end do

contains

    function canPalindrome(s) result(res)
        implicit none
        character(len=*), intent(in) :: s
        logical :: res
        integer :: i, j

        res = .true.
        do i = 1, len(s)/2
            if (s(i:i) /= s(len(s)-i+1:len(s)-i)) then
                res = .false.
                exit
            end if
        end do
    end function canPalindrome

end program main
āŒ Compiled
āŒ Executed
āŒ Correct
module palindrome

implicit none

contains

function is_palindrome(s) result(is_palindrome)

character(len=*), intent(in) :: s
logical :: is_palindrome

! Check if the string is a palindrome
is_palindrome = .true.
if (len(s) == 1) then
    is_palindrome = .true.
else
    ! Check if the string is a palindrome after deleting at most one character
    if (s(1:len(s)/2) == s(len(s):2:-1)) then
        is_palindrome = .true.
    else
        is_palindrome = .false.
    end if
end if

end function is_palindrome

end module palindrome

program main

use palindrome
implicit none

! Test cases
character(len=100) :: s

! Test case 1
s = "aba"
if (is_palindrome(s)) then
    write (*,*) "Test case 1: Pass"
else
    write (*,*) "Test case 1: Fail"
end if

! Test case 2
s = "abca"
if (is_palindrome(s)) then
    write (*,*) "Test case 2: Pass"
else
    write (*,*) "Test case 2: Fail"
end if

! Test case 3
s = "abc"
if (is_palindrome(s)) then
    write (*,*) "Test case 3: Pass"
else
    write (*,*) "Test case 3: Fail"
end if

end program main
šŸŒ Data from online sources
def validPalindrome(s: str) -> bool:
    i, j = 0, len(s) - 1
    while i < j:
        if s[i] != s[j]:
            temp1, temp2 = i + 1, j
            while temp1 < temp2 and s[temp1] == s[temp2]:
                temp1 += 1
                temp2 -= 1
            if temp1 >= temp2:
                return True

            temp1, temp2 = i, j - 1
            while temp1 < temp2 and s[temp1] == s[temp2]:
                temp1 += 1
                temp2 -= 1
            if temp1 >= temp2:
                return True

            return False
        i += 1
        j -= 1
    return True

The algorithm works as follows: 1. Initialize two pointers, i at the start and j at the end of the input string s. 2. Check if characters at positions i and j are equal. If they are not, 1. Check if the resulting substring s[i+1, j] is a palindrome. 2. If it is not, check if the resulting substring s[i, j-1] is a palindrome. 3. If neither of the substrings is a palindrome, return false. 3. Move both pointers towards each other and repeat step 2. 4. If the pointers crossed each other or met, the entire string s can be a palindrome after deleting at most one character, so return true.

The functions for all four languages follow the same logic as explained above.

šŸŒ Data from online sources
bool validPalindrome(string s) {
    for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
        if (s[i] != s[j]) {
            int temp1 = i + 1, temp2 = j;
            while (temp1 < temp2 && s[temp1] == s[temp2]) { temp1++; temp2--; }
            if (temp1 >= temp2) return true;

            temp1 = i; temp2 = j - 1;
            while (temp1 < temp2 && s[temp1] == s[temp2]) { temp1++; temp2--; }
            if (temp1 >= temp2) return true;

            return false;
        }
    }
    return true;
}

The algorithm works as follows: 1. Initialize two pointers, i at the start and j at the end of the input string s. 2. Check if characters at positions i and j are equal. If they are not, 1. Check if the resulting substring s[i+1, j] is a palindrome. 2. If it is not, check if the resulting substring s[i, j-1] is a palindrome. 3. If neither of the substrings is a palindrome, return false. 3. Move both pointers towards each other and repeat step 2. 4. If the pointers crossed each other or met, the entire string s can be a palindrome after deleting at most one character, so return true.

The functions for all four languages follow the same logic as explained above.