Published by Dimitris Kougioumtzis · Updated
Build palindrome intervals and minimum prefix cuts, then reconstruct a partition.
Precompute palindromic intervals and select the fewest cuts covering each prefix, recording piece boundaries.
When to use Minimum palindrome partition cuts
Combining interval preprocessing with prefix optimization.
Choosing an alternative
Longest palindrome finds one interval; partitioning must cover the entire string.
Worked example: input and state changes
Example input
aab
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 0, 0, 0, 0 ], "active": [] }Step 2: Palindrome a: best cuts for prefix 1 = 0.
State at this checkpoint
{ "values": [ -1, 0, 0, 0 ], "active": [ 1 ] }Step 3: Palindrome aa: best cuts for prefix 2 = 0.
State at this checkpoint
{ "values": [ -1, 0, 0, 0 ], "active": [ 2 ] }Step 5: Palindrome b: best cuts for prefix 3 = 1.
State at this checkpoint
{ "values": [ -1, 0, 0, 1 ], "active": [ 3 ] }Step 6: Minimum cuts = 1; aa | b.
State at this checkpoint
{ "values": [ -1, 0, 0, 1 ], "active": [], "result": 1, "pieces": [ "aa", "b" ] }
Expected final state
Minimum cuts = 1; aa | b.
Complexity and input bounds
Family: Dynamic programming. Time complexity: O(n²).
Space complexity: O(n squared) flags plus O(n) reconstruction.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
One string of 1–15 ASCII letters.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
For a nonempty string, cuts equal pieces minus one; a full palindrome needs zero cuts.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // 1–15 ASCII letter codes. Returns minimum cuts. output[0] is piece count, followed by start/end-exclusive index pairs. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, output) { let pal = Array(256).fill(0); let cuts = Array(256).fill(0); let parent = Array(256).fill(0); cuts[0] = -1; for (let end = 1; end <= n; end++) { cuts[end] = n; for (let start = 0; start < end; start++) { if (a[start] == a[end - 1] && (end - start <= 2 || pal[(start + 1) * n + end - 2] == 1)) { pal[start * n + end - 1] = 1; if (cuts[start] + 1 < cuts[end]) { cuts[end] = cuts[start] + 1; parent[end] = start; } } } } let begins = Array(256).fill(0); let finishes = Array(256).fill(0); let count = 0; let position = n; while (position > 0) { begins[count] = parent[position]; finishes[count++] = position; position = parent[position]; } output[0] = count; for (let i = 0; i < count; i++) { output[2 * i + 1] = begins[count - 1 - i]; output[2 * i + 2] = finishes[count - 1 - i]; } return cuts[n]; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- Algorithms on Strings, Trees and Sequences
Dan Gusfield (1997). Reference book.