Ctrl K

Mirror Chunk Split

Hard
Cut the string line into non-empty pieces p1, p2, …, pk that read back the original string when written in order, and make the cut a mirror: the first piece equals the last, the second equals the second to last, and so on, so p_i == p_{k - i + 1} for every i. With an odd number of pieces, the middle piece can be anything. Return the largest possible number of pieces k.
Example 1
line="redbluered" → 3

Cut it as (red)(blue)(red): the first piece equals the last one and the middle piece stands alone. Three is the most possible.

Example 2
line="abcd" → 1

No prefix matches a suffix of the same length, so the only option is the whole string as one piece.

Example 3
line="kayak" → 5

(k)(a)(y)(a)(k): every letter becomes its own piece.

Constraints
  • 1 ≤ len(line) ≤ 1000
  • line consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPeel Off a Repeated Start HardDisjoint Long Palindromes HardWord Near an Anchor HardPhrase From Word Beginnings, Long Edition HardLongest Piece Seen Twice Medium
loading Python…
Input
line="redbluered"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 184 tests on the server, hidden ones included.