Last active
August 29, 2015 14:05
-
-
Save rohit-jamuar/05c1d1133212a2083c0a to your computer and use it in GitHub Desktop.
Find all palindromic substrings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def get_substrings(input_str): | |
''' | |
Yields all substrings of size in range [1, len(input_str)] | |
''' | |
for step in range(1, len(input_str)+1): | |
temp1 = [] | |
for i in range(len(input_str)): | |
temp2 = input_str[i:i+step] | |
if len(temp2) == step: | |
temp1.append(temp2) | |
yield temp1 | |
def palindrome_partition(input_str): | |
''' | |
Prints all palindromic substrings to STDOUT. | |
''' | |
for elem in get_substrings(input_str): | |
printed_something = False | |
for substr in elem: | |
if substr == substr[::-1]: | |
print substr, | |
printed_something = True | |
if printed_something: | |
print '' | |
printed_something = False | |
if __name__ == '__main__': | |
for string in ["aab", "wow", "madam"]: | |
palindrome_partition(string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment