Sol:
import sys | |
while True: | |
line = sys.stdin.readline() | |
if line == '': | |
break | |
line = line.strip() | |
n = len(line) | |
totalUniqueSubstrings = 0 | |
for i in range(1, n + 1): | |
unique = [True] * (n-i+1) | |
for j in range(0, n-i+1): | |
substring = line[j:j+i] | |
#this will check if the string is palindrom | |
if substring == substring[::-1]: | |
for k in range(0, j): | |
if line[k:k+i] == substring: | |
unique[j] = False | |
break | |
else: | |
unique[j] = False | |
totalUniqueSubstrings = totalUniqueSubstrings + unique.count(True) | |
print("The string '{}' contains {} palindromes.".format(line, totalUniqueSubstrings)) |
Không có nhận xét nào:
Đăng nhận xét