Longest Palindrome

🏠 ⬅️ ➡️

Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.

Letters are case sensitive, for example, "Aa " is not considered a palindrome here.

Example 1:

Input: s = "abccccdd " Output: 7 Explanation: One longest palindrome that can be built is "dccaccd ", whose length is 7.

Example 2:

Input: s = "a " Output: 1 Explanation: The longest palindrome that can be built is "a ", whose length is 1.

Constraints:

  • 1 <= s.length <= 2000
  • s consists of lowercase and/or uppercase English letters only.

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

    character(len=:), allocatable :: s
    integer :: n

    call get_command_argument(1, s)
    n = longest_palindrome(s)
    write (*, '(I0)') n

contains

    function longest_palindrome(s) result(n)
        implicit none
        character(len=*), intent(in) :: s
        integer :: i, j, n

        do i = 1, len(s) - 1
            do j = i + 1, len(s)
                if (s(i:j) == s(i:j)) then
                    n = max(n, j - i + 1)
                end if
            end do
        end do
    end function longest_palindrome

end program main
Compiled
Executed
Correct
module longest_palindrome

implicit none

contains

function longest_palindrome_length(s) result(length)

character(len=*), intent(in) :: s
integer :: length

! Local variables
integer :: i, j, n
character(len=1) :: c
logical :: is_palindrome

! Initialize variables
length = 0
n = len(s)

! Iterate over the string
do i = 1, n

    ! Get the character at position i
    c = s(i:i)

    ! Check if the character is a palindrome
    is_palindrome = .true.
    do j = i+1, n
        if (c /= s(j:j)) then
            is_palindrome = .false.
            exit
        end if
    end do

    ! If the character is a palindrome, update the length
    if (is_palindrome) then
        length = max(length, j-i+1)
    end if

end do

end function longest_palindrome_length

end module longest_palindrome

program test_longest_palindrome

use longest_palindrome
implicit none

! Test case 1:
print *, longest_palindrome_length("abccccdd")
! Output: 7

! Test case 2:
print *, longest_palindrome_length("a")
! Output: 1

! Test case 3:
print *, longest_palindrome_length("aa")
! Output: 2

! Test case 4:
print *, longest_palindrome_length("abababa")
! Output: 7

! Test case 5:
print *, longest_palindrome_length("abababab")
! Output: 8

end program test_longest_palindrome
🌐 Data from online sources
def longestPalindrome(s: str) -> int:
    charCount = [0] * 128
    result = 0
    for c in s:
        charCount[ord(c)] += 1
    for count in charCount:
        result += count // 2 * 2
        if result % 2 == 0 and count % 2 == 1:
            result += 1
    return result
  1. Create an array charCount of size 128 (to cover the ASCII range of characters) initialized with zeros.
  2. Iterate through each character in the input string s and increment the corresponding count in the charCount array.
  3. Initialize a variable result to store the length of the longest palindrome to be built.
  4. Iterate through the charCount array and for each count, add the count divided by 2 (integer division) times 2 to the result. This adds the number of pairs of characters that can be used in the palindrome.
  5. If the result so far is even and the count has an odd value, increment the result by 1. This accounts for the center character in an odd-length palindrome.
  6. Return the final result, which is the length of the longest palindrome that can be built.
🌐 Data from online sources
int longestPalindrome(string s) {
    int charCount[128] = {0};
    int result = 0;
    for (char c : s)
        charCount[c]++;
    for (int count : charCount) {
        result += count / 2 * 2;
        if (result % 2 == 0 && count % 2 == 1)
            result++;
    }
    return result;
}
  1. Create an array charCount of size 128 (to cover the ASCII range of characters) initialized with zeros.
  2. Iterate through each character in the input string s and increment the corresponding count in the charCount array.
  3. Initialize a variable result to store the length of the longest palindrome to be built.
  4. Iterate through the charCount array and for each count, add the count divided by 2 (integer division) times 2 to the result. This adds the number of pairs of characters that can be used in the palindrome.
  5. If the result so far is even and the count has an odd value, increment the result by 1. This accounts for the center character in an odd-length palindrome.
  6. Return the final result, which is the length of the longest palindrome that can be built.