Check if suffix and prefix of a string are palindromes in Python

A string has palindromic prefix and suffix when both the beginning and end portions read the same forwards and backwards. This problem requires finding if a string contains at least one palindrome as a prefix and at least one palindrome as a suffix.

For example, in the string "levelishighforracecar", the prefix "level" and suffix "racecar" are both palindromes.

Algorithm

The solution follows these steps ?

  • Find the first palindromic prefix of length ? 2
  • If no palindromic prefix exists, return False
  • Find any palindromic suffix of length ? 2
  • If both palindromic prefix and suffix exist, return True

Helper Function

First, we need a function to check if a string is a palindrome ?

def is_palindrome(s):
    return s == s[::-1]

# Test the helper function
print(is_palindrome("level"))   # Palindrome
print(is_palindrome("hello"))   # Not palindrome
True
False

Complete Solution

Here's the complete implementation to check for palindromic prefix and suffix ?

def is_palindrome(s):
    return s == s[::-1]

def solve(s):
    length = len(s)
    
    # Find palindromic prefix of length >= 2
    prefix_found = False
    for i in range(2, length + 1):
        if is_palindrome(s[0:i]):
            prefix_found = True
            break
    
    # If no palindromic prefix found, return False
    if not prefix_found:
        return False
    
    # Find palindromic suffix of length >= 2
    for i in range(2, length + 1):
        if is_palindrome(s[length - i : length]):
            return True
    
    return False

# Test with example string
s = "levelishighforracecar"
print(solve(s))
True

Testing Different Cases

Let's test the function with various string examples ?

def is_palindrome(s):
    return s == s[::-1]

def solve(s):
    length = len(s)
    
    prefix_found = False
    for i in range(2, length + 1):
        if is_palindrome(s[0:i]):
            prefix_found = True
            break
    
    if not prefix_found:
        return False
    
    for i in range(2, length + 1):
        if is_palindrome(s[length - i : length]):
            return True
    
    return False

# Test cases
test_strings = [
    "levelishighforracecar",  # True: "level" prefix, "racecar" suffix
    "abcdefghijk",            # False: no palindromes
    "madam_hello_noon",       # True: "madam" prefix, "noon" suffix
    "hello"                   # False: no palindromic prefix/suffix >= length 2
]

for string in test_strings:
    result = solve(string)
    print(f'"{string}" ? {result}')
"levelishighforracecar" ? True
"abcdefghijk" ? False
"madam_hello_noon" ? True
"hello" ? False

How It Works

The algorithm works in two phases:

  1. Prefix Check: Iterates through substrings starting from index 0, checking if each substring of length 2 or more is a palindrome
  2. Suffix Check: If a palindromic prefix is found, checks substrings ending at the last character for palindromes of length 2 or more

The function returns True only when both a palindromic prefix and suffix are found, False otherwise.

Conclusion

This solution efficiently checks for palindromic prefixes and suffixes by iterating through substrings of increasing length. The algorithm ensures both conditions are met before returning True.

Updated on: 2026-03-25T14:28:33+05:30

433 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements