Funcia
Lv.4dpstring

Palindrome Partitioning Min Cuts

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Return the minimum number of cuts so that every piece of s is a palindrome (0 if s already is one).

Examples

sresult
"aab"1
"racecar"0
"abcd"3

Efficiency

This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.

Function signature

solution(s: string) → int

Constraints

  • 1≤1 \le s.length ≤3,000\le 3{,}000, lowercase letters only

Expected complexity

Time
O(N^2)
Space
O(N)

Examples

sExpected
"aab"1
"racecar"0
"abcd"3

Hint

Hint

cut[i] = min(cut[j] + 1) over palindromic s[j..i). Generate palindromes by expanding around each center (updating cut directly) or precompute a palindrome table, for O(N2)O(N^2). Checking each piece directly is O(N3)O(N^3).

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness407verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.