Question DSA-DP-0020
HardWhat is the output of this code?
s = "aab"
n = len(s)
is_pal = [[False] * n for _ in range(n)]
cuts = [0] * n
for j in range(n):
best = j
for i in range(j + 1):
if s[i] == s[j] and (j - i < 2 or is_pal[i+1][j-1]):
is_pal[i][j] = True
best = 0 if i == 0 else min(best, cuts[i-1] + 1)
cuts[j] = best
print(cuts[-1])
- A.
2
- B.
3
- C.
Sign up free to see the answer and a step-by-step explanation.