Skip to content

Commit 34ead4d

Browse files
authored
Update palindrome_partitioning.py
1 parent 370c279 commit 34ead4d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

dynamic_programming/palindrome_partitioning.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def find_minimum_partitions(string: str) -> int:
2020
>>> find_minimum_partitions("ababbbabbababa")
2121
3
2222
"""
23-
n = len(string)
24-
cut = [0] * n
25-
is_palindromic = [[False for i in range(n)] for j in range(n)]
26-
for i in range(n):
23+
length = len(string)
24+
cut = [0] * length
25+
is_palindromic = [[False for i in range(length)] for j in range(length)]
26+
for i, c in enumerate(length):
2727
mincut = i
2828
for j in range(i + 1):
29-
if string[i] == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]):
29+
if c == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]):
3030
is_palindromic[j][i] = True
3131
mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1))
3232
cut[i] = mincut
@@ -36,4 +36,4 @@ def find_minimum_partitions(string: str) -> int:
3636
if __name__ == "__main__":
3737
s = input("Enter the string: ").strip()
3838
ans = find_minimum_partitions(s)
39-
print(f"Minimum number of partitions required for the string is {ans}")
39+
print(f"Minimum number of partitions required for the '{s}' is {ans}")

0 commit comments

Comments
 (0)