Python program to check whether the string is Symmetrical or Palindrome
The task of checking whether a string is symmetrical or palindrome in Python involves two main operations . A string is symmetrical if its first half matches the second half, considering the middle character for odd-length strings. A string is palindrome if it reads the same forward and backward.
For example, with s = "amaama", the first half "ama" matches the second half "ama", making it symmetrical. Also, since "amaama" reads the same in both directions, it is a palindrome. However, for s = "abcba", it is a palindrome but not symmetrical, as "ab" is not equal to "ba".
Using string slicing
This method uses Python’s slicing feature to directly compare parts of the string. It is the fastest and simplest way to check for symmetry and palindrome properties.
s = "abaaba"
half = len(s) // 2
sym = s[:half] == s[half:][::-1] if len(s) % 2 == 0 else s[:half] == s[half+1:][::-1]
# Check if palindrome
pal = s == s[::-1]
print("Symmetrical" if sym else "Not Symmetrical")
print("Palindrome" if pal else "Not Palindrome")
Output
Symmetrical Palindrome
Explanation:
- For symmetry, the string is split into two halves. If even, both halves are compared if odd, the middle character is ignored.
- For palindrome, the string is checked against its reverse (s[::-1]).
Table of Content
Using two pointers
This approach uses two pointers, one starting from the left and the other from the right, to compare characters. It avoids unnecessary operations and works efficiently for large strings.
s = "amaama"
# Check palindrome using two pointers
pal = True
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
pal = False
break
i += 1
j -= 1
# Check symmetry using two halves comparison
half = len(s) // 2
sym = True
for i in range(half):
if len(s) % 2 == 0:
if s[i] != s[i + half]:
sym = False
break
else:
if s[i] != s[i + half + 1]:
sym = False
break
print("Symmetrical" if sym else "Not Symmetrical")
print("Palindrome" if pal else "Not Palindrome")
Output
Symmetrical Palindrome
Explanation:
- For palindrome, two pointers compare characters from both ends, moving inward. If a mismatch is found, it's not a palindrome.
- For symmetry, the first half is compared with the second. If even, halves align directly; if odd, the middle character is skipped.
Using recursion
The recursive method repeatedly compares characters from both ends until the middle is reached. It provides a structured way to solve the problem but may use more memory for deep recursion.
def is_palindrome(s, i=0):
if i >= len(s) // 2:
return True
return s[i] == s[-(i + 1)] and is_palindrome(s, i + 1)
def is_symmetrical(s, half, i=0):
if i >= half:
return True
if len(s) % 2 == 0:
return s[i] == s[i + half] and is_symmetrical(s, half, i + 1)
else:
return s[i] == s[i + half + 1] and is_symmetrical(s, half, i + 1)
s = "amaama"
half = len(s) // 2
print("Symmetrical" if is_symmetrical(s, half) else "Not Symmetrical")
print("Palindrome" if is_palindrome(s) else "Not Palindrome")
Output
Symmetrical Palindrome
Explanation:
- For palindrome, recursion checks if characters from both ends match, moving inward until the middle is reached.
- For symmetry, recursion compares the first half with the second, skipping the middle character if the length is odd.
Using reversed iteration
This method compares the string with its reversed version using reversed() function. While straightforward, it is slower than slicing and two-pointer techniques.
s = "amaama"
# Check palindrome using reversed()
pal = list(s) == list(reversed(s))
# Check symmetry
half = len(s) // 2
sym = s[:half] == s[half:] if len(s) % 2 == 0 else s[:half] == s[half+1:]
print("Symmetrical" if sym else "Not Symmetrical")
print("Palindrome" if pal else "Not Palindrome")
Output
Symmetrical Palindrome
Explanation:
- For palindrome, the string is converted to a list and compared with its reversed version.
- For symmetry, the first half is compared with the second, skipping the middle character if the length is odd.