Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a string is Colindrome in Python
A colindrome is a special type of string that consists of concatenated palindromes, each exactly 6 characters long. To check if a string is a colindrome, we need to verify that its length is divisible by 6 and that each 6-character segment is a palindrome.
For example, the string "aabbaamnoonm" is a colindrome because it contains two 6-character palindromes: "aabbaa" and "mnoonm".
Algorithm
To solve this problem, we follow these steps:
- Check if the string length is divisible by 6
- Divide the string into 6-character segments
- Verify each segment is a palindrome
- Return True only if all segments are palindromes
Implementation
Here's the complete solution using a helper function to check palindromes:
def is_palindrome(s):
return s == s[::-1]
def is_colindrome(s):
# Check if length is divisible by 6
if len(s) % 6 != 0:
return False
# Check each 6-character segment
for i in range(0, len(s), 6):
segment = s[i:i+6]
if not is_palindrome(segment):
return False
return True
# Test with example
s = "aabbaamnoonm"
print(f"'{s}' is colindrome: {is_colindrome(s)}")
# Test segments individually
print(f"First segment 'aabbaa': {is_palindrome('aabbaa')}")
print(f"Second segment 'mnoonm': {is_palindrome('mnoonm')}")
'aabbaamnoonm' is colindrome: True First segment 'aabbaa': True Second segment 'mnoonm': True
Testing with Different Examples
Let's test the function with various inputs to understand its behavior:
def is_palindrome(s):
return s == s[::-1]
def is_colindrome(s):
if len(s) % 6 != 0:
return False
for i in range(0, len(s), 6):
if not is_palindrome(s[i:i+6]):
return False
return True
# Test cases
test_strings = [
"aabbaamnoonm", # Valid colindrome
"abccbadefged", # Invalid - second segment not palindrome
"abcdef", # Invalid - not palindrome
"racecar", # Invalid - length not divisible by 6
"abccbaxyzyx" # Valid colindrome
]
for test in test_strings:
result = is_colindrome(test)
print(f"'{test}' -> {result}")
'aabbaamnoonm' -> True 'abccbadefged' -> False 'abcdef' -> False 'racecar' -> False 'abccbaxyzyx' -> True
How It Works
The algorithm works by:
- Length Check: First verifying the string length is divisible by 6
-
Segmentation: Using
range(0, len(s), 6)to iterate through 6-character chunks -
Palindrome Verification: Checking each segment using string slicing
s[::-1] - Early Return: Returning False immediately if any segment fails the palindrome test
Time and Space Complexity
- Time Complexity: O(n), where n is the length of the string
- Space Complexity: O(1), using constant extra space
Conclusion
A colindrome is a string composed of 6-character palindromes concatenated together. The solution efficiently checks length divisibility and validates each segment, making it suitable for strings of any valid colindrome length.
