Palindrome Permutation

🏠 ⬅️ ➡️

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
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.