# -*- coding: utf-8 -*-
"""Challenge 1 - difficult case-Khalid AlHarbi

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1_2HkSGM815VLsizjxyZBUmFIz5FEe2D8
"""

def longest_palindromic_substring(s):
    def expand_around_center(s, left, right):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return s[left + 1:right]

    if not s:
        return ""

    longest = ""

    for i in range(len(s)):
        #  Check the symmetric chain with one center
        odd_palindrome = expand_around_center(s, i, i)
        if len(odd_palindrome) > len(longest):
            longest = odd_palindrome

        #Check for symmetric string with double center
        even_palindrome = expand_around_center(s, i, i + 1)
        if len(even_palindrome) > len(longest):
            longest = even_palindrome

    return longest

# Examples of usage
print(longest_palindromic_substring("babad"))  # Output: "bab" or "aba"
print(longest_palindromic_substring("cbbd"))   # Output: "bb"
print(longest_palindromic_substring("a"))      # Output: "a"
print(longest_palindromic_substring("ac"))     # Output: "a" or "c"
print(longest_palindromic_substring("racecar"))# Output: "racecar"