Backtracking
100 / 170BacktrackingLesson 7 of 10About 44 min
The choice is not which item to take, but where the next piece ends.
At each position the search may cut after any number of characters, and most of those cuts lead nowhere. Type the four lines it prints.
def mirrored(piece): return piece == piece[::-1] text = "aab"for end in range(1, len(text) + 1): piece = text[:end] print("cut after", end, "gives", piece, "->", "keep" if mirrored(piece) else "abandon") print("branches worth taking:", sum(1 for end in range(1, len(text) + 1) if mirrored(text[:end])))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.