AlgoUniversity
  • Go Back
Discussion
Palindrome Partitioning :

Author

Ujjwal Srivastava

Difficulty Level : Hard

Submissions : 203

Asked In : Paytm

Marks :50

: 2 | : 0

Given a string, a partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome.

For example, $$$aba|b|bbabb|a|b|aba$$$ is a palindrome partitioning of $$$ababbbabbababa.$$$

Determine the fewest cuts needed for a palindrome partitioning of a given string. For example, minimum of $$$3$$$ cuts are needed for $$$ababbbabbababa.$$$ The three cuts are $$$a|babbbab|b|ababa.$$$ If a string is a palindrome, then minimum $$$0$$$ cuts are needed.

If a string of length $$$n$$$ contains all different characters, then minimum $$$n$$$-$$$1$$$ cuts are needed.

Input

The first line contains $$$n$$$ which denotes the length of the string. The next line contains a string $$$s$$$ consisting of lowercase English letters of length $$$n$$$.$$$( 1 \le n \le 10^3)$$$

Output

Output the minimum cuts required.

Examples

Input
14
ababbbabbababa
Output
3
Input
26
abcdefghijklmnopqrstuvwxyz
Output
25
Input
5
oqquy
Output
3

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.