def longest_palindromic_substring(s):
    def expand_around_center(left, right):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return s[left + 1:right]

    longest = ""
    for i in range(len(s)):
        # Check for odd length palindrome
        palindrome1 = expand_around_center(i, i)
        if len(palindrome1) > len(longest):
            longest = palindrome1

        # Check for even length palindrome
        palindrome2 = expand_around_center(i, i + 1)
        if len(palindrome2) > len(longest):
            longest = palindrome2

    return longest

# مثال
s1 = "babad"
print(f"أطول سلسلة متماثلة في '{s1}' هي: {longest_palindromic_substring(s1)}")

s2 = "cbbd"
print(f"أطول سلسلة متماثلة في '{s2}' هي: {longest_palindromic_substring(s2)}")

s3 = "a"
print(f"أطول سلسلة متماثلة في '{s3}' هي: {longest_palindromic_substring(s3)}")

s4 = "ac"
print(f"أطول سلسلة متماثلة في '{s4}' هي: {longest_palindromic_substring(s4)}")

s5 = "racecar"
print(f"أطول سلسلة متماثلة في '{s5}' هي: {longest_palindromic_substring(s5)}")