53 lines
1.0 KiB
Python
53 lines
1.0 KiB
Python
"""
|
|
name: Nicholas Tamassia
|
|
|
|
Honor Code and Acknowledgments:
|
|
|
|
This work complies with the JMU Honor Code.
|
|
|
|
Comments here on your code and submission.
|
|
"""
|
|
|
|
|
|
def is_palindrome(s: str, start: int, end: int) -> bool:
|
|
return s[start:end] == s[start:end][::-1]
|
|
|
|
|
|
def palindrome_partitions(s: str) -> int:
|
|
|
|
memo: list[int] = [-1] * (len(s) + 1)
|
|
|
|
def recurse(start: int) -> int:
|
|
nonlocal memo
|
|
|
|
if start == len(s):
|
|
return 1
|
|
|
|
if memo[start] != -1:
|
|
return memo[start]
|
|
|
|
sum = 0
|
|
|
|
for i in range(start + 1, len(s) + 1):
|
|
if is_palindrome(s, start, i):
|
|
sum += recurse(i)
|
|
|
|
memo[start] = sum
|
|
return sum
|
|
|
|
return recurse(0)
|
|
|
|
|
|
# All modules for CS 412 must include a main method that allows it
|
|
# to imported and invoked from other python scripts
|
|
def main():
|
|
n: int = int(input())
|
|
|
|
for _ in range(0, n):
|
|
in_str = input().strip()
|
|
print(palindrome_partitions(in_str))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|