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 <= 105s consists of lowercase English letters.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
temp.f95:12:8:
12 | do i = 1, size(tests)
| 1
Error: Symbol āiā at (1) has no IMPLICIT type
temp.f95:6:25:
6 | logical :: results(:)
| 1
Error: Array āresultsā at (1) cannot have a deferred shape
temp.f95:8:19:
8 | tests = ["aba", "abca", "abc"]
| 1
Error: Different CHARACTER lengths (3/4) in array constructor at (1)
temp.f95:9:13:
9 | allocate(results(size(tests)))
| 1
Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER
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
temp.f95:7:47:
7 | function is_palindrome(s) result(is_palindrome)
| 1
Error: RESULT variable at (1) must be different than function name
temp.f95:9:33:
9 | character(len=*), intent(in) :: s
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:10:24:
10 | logical :: is_palindrome
| 1
Error: Symbol āis_palindromeā at (1) has already been host associated
temp.f95:13:14:
13 | is_palindrome = .true.
| 1
Error: Symbol āis_palindromeā at (1) has already been host associated
temp.f95:14:21:
14 | if (len(s) == 1) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:15:18:
15 | is_palindrome = .true.
| 1
Error: Symbol āis_palindromeā at (1) has already been host associated
temp.f95:16:4:
16 | else
| 1
Error: Unexpected ELSE statement in CONTAINS section at (1)
temp.f95:18:12:
18 | if (s(1:len(s)/2) == s(len(s):2:-1)) then
| 1
Error: Syntax error in argument list at (1)
temp.f95:19:22:
19 | is_palindrome = .true.
| 1
Error: Symbol āis_palindromeā at (1) has already been host associated
temp.f95:20:8:
20 | else
| 1
Error: Unexpected ELSE statement in CONTAINS section at (1)
temp.f95:21:22:
21 | is_palindrome = .false.
| 1
Error: Symbol āis_palindromeā at (1) has already been host associated
temp.f95:22:7:
22 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:23:3:
23 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:25:3:
25 | end function is_palindrome
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:31:5:
31 | use palindrome
| 1
Fatal Error: Cannot open module file āpalindrome.modā for reading at (1): No such file or directory
compilation terminated.
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.
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.