Menu
CoddyTech

Palindrome Partitioning

A palindrome reads the same from left to right and from right to left. Cut the string s into one or more pieces so that every piece is a palindrome, and return every way to do it.

Write each split as its pieces in order, joined by |: cutting aab into a, a and b gives a|a|b. Return the splits sorted in ascending order, comparing character codes, so | comes after every lowercase letter.

Function

partition(s: string) → string-array
sstring
the string of lowercase letters to cut
Returnsstring-array
every split of s into palindromes, written with | between the pieces, in ascending order

Constraints

  • 1 ≤ s.length ≤ 11
  • s contains only lowercase English letters.
  • A string of 11 equal letters has 1,024 splits, the largest answer.

Examples

Input
s = "aab"
Output
["aa|b", "a|a|b"]
Explanation
Only aa|b and a|a|b have all palindromic pieces; a|ab fails on ab. aa|b sorts first: at the second character it has a where the other has |, which sorts after every letter.
challenge icon

Follow-up

Can you find the smallest number of cuts that splits s into palindromes, for strings of length up to 2,000?

Reset code
def partition(s):
    # Write code here

Test #1

Test #2

Argument

s = "aab"

Expected Output

["aa|b", "a|a|b"]