Given a string s
, return true
if a permutation of the string could form a palindrome and false
otherwise.
Example 1:
Input: s = "code " Output: false
Example 2:
Input: s = "aab " Output: true
Example 3:
Input: s = "carerac " Output: true
Constraints:
1 <= s.length <= 5000
s
consists of only lowercase English letters.program main
implicit none
character(len=:), allocatable :: s
logical :: is_palindrome
! Example 1
s = "code "
print '(L1)', is_palindrome(s)
! Example 2
s = "aab "
print '(L1)', is_palindrome(s)
! Example 3
s = "carerac "
print '(L1)', is_palindrome(s)
contains
function is_palindrome(str) result(res)
implicit none
character(len=*), intent(in) :: str
integer :: i, j
logical :: res
res = .false.
do i = 1, len(str)/2
if (str(i:i) /= str(len(str)-i+1:len(str)-i)) then
exit
end if
end do
if (i == len(str)/2 + 1) then
res = .true.
end if
end function is_palindrome
end program main
temp.f95:21:27: 21 | function is_palindrome(str) result(res) | 1 Error: Procedure ‘is_palindrome’ at (1) has an explicit interface from a previous declaration temp.f95:22:21: 22 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:23:43: 23 | character(len=*), intent(in) :: str | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:23: 24 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:22: 25 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:21: 27 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:28: 29 | do i = 1, len(str)/2 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:30:22: 30 | if (str(i:i) /= str(len(str)-i+1:len(str)-i)) then | 1 Error: Syntax error in argument list at (1) temp.f95:31:20: 31 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:32:15: 32 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:11: 33 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:35:37: 35 | if (i == len(str)/2 + 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:36:24: 36 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:37:11: 37 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:7: 38 | end function is_palindrome | 1 Error: Expecting END PROGRAM statement at (1)
module palindrome
implicit none
contains
function is_palindrome_permutation(s) result(is_palindrome)
! Given a string s, return true if a permutation of the string could form a palindrome, and false otherwise.
character(len=*), intent(in) :: s
logical :: is_palindrome
! Edge case: empty string
if (len_trim(s) == 0) then
is_palindrome = .false.
return
end if
! Check if the string is a palindrome
if (s == reverse(s)) then
is_palindrome = .true.
return
end if
! Check if the string is a permutation of a palindrome
is_palindrome = .false.
do i = 1, len_trim(s) - 1
do j = i + 1, len_trim(s)
if (s(i:j) == reverse(s(i:j))) then
is_palindrome = .true.
return
end if
end do
end do
end function is_palindrome_permutation
end module palindrome
program main
use palindrome
implicit none
! Test cases
character(len=5000) :: s
s = "code "
write (*,*) is_palindrome_permutation(s)
s = "aab "
write (*,*) is_palindrome_permutation(s)
s = "carerac "
write (*,*) is_palindrome_permutation(s)
end program main
temp.f95:28:4: 28 | do i = 1, len_trim(s) - 1 | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:29:8: 29 | do j = i + 1, len_trim(s) | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:21:8: 21 | if (s == reverse(s)) then | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:43:5: 43 | use palindrome | 1 Fatal Error: Cannot open module file ‘palindrome.mod’ for reading at (1): No such file or directory compilation terminated.
def canPermutePalindrome(s: str) -> bool:
count = {}
for c in s:
if c in count:
count[c] += 1
else:
count[c] = 1
odd_count = 0
for value in count.values():
if value % 2:
odd_count += 1
return odd_count <= 1
The algorithm counts the occurrences of each character in the string using a hash map (or dictionary for Python). Then, it iterates through the count values in the hash map and checks how many characters have odd occurrences. If the string can form a palindrome, there should be at most one character having an odd number of occurrences.
The logic is based on the fact that a string can form a palindrome if it meets the following condition: If the string length is even, there should be an even number of occurrences for each character. If the string length is odd, there should be only one character having odd occurrences and the rest should have even occurrences.
The algorithm simply returns true if the odd_count is less than or equal to 1, indicating the string can form a palindrome; otherwise, it returns false.
bool canPermutePalindrome(string s) {
unordered_map<char, int> count;
for (char c : s) {
count[c]++;
}
int odd_count = 0;
for (auto it = count.begin(); it != count.end(); ++it) {
if (it->second % 2) {
odd_count++;
}
}
return odd_count <= 1;
}
The algorithm counts the occurrences of each character in the string using a hash map (or dictionary for Python). Then, it iterates through the count values in the hash map and checks how many characters have odd occurrences. If the string can form a palindrome, there should be at most one character having an odd number of occurrences.
The logic is based on the fact that a string can form a palindrome if it meets the following condition: If the string length is even, there should be an even number of occurrences for each character. If the string length is odd, there should be only one character having odd occurrences and the rest should have even occurrences.
The algorithm simply returns true if the odd_count is less than or equal to 1, indicating the string can form a palindrome; otherwise, it returns false.