Palindrome partitioning

Recursion FAQs (Hard) Hard

Given a string s partition string s such that every substring of partition is palindrome. Return all possible palindrome partition of string s.

Examples:

Input : s = "aabaa"

Output : [ [ "a", "a", "b", "a", "a"] , [ "a", "a", "b", "aa"] , [ "a", "aba", "a"] , [ "aa", "b", "a", "a"] , [ "aa", "b", "aa" ] , [ "aabaa" ] ]

Explanation : Above all are the possible ways in which the string can be partitioned so that each substring is a palindrome.

Input : s = "baa"

Output : [ [ "b", "a", "a"] , [ "b", "aa" ] ]

Explanation : Above all are the possible ways in which the string can be partitioned so that each substring is a palindrome.

Input : s = "ab"

Constraints

  • 1<= s.length <= 16
  • s contains only lowercase English letters.

Hints

  • Use recursion to explore all possible partitions. Backtrack to remove the last added substring and try other possibilities.
  • Use a helper function to check if a substring is a palindrome.

Company Tags

Instacart PayPal Airbnb Walmart American Express Shopify Medtronic Seagate Technology Cloudflare Morgan Stanley Siemens Healthineers Qualcomm Unity Technologies Electronic Arts Pinterest Mastercard PwC Robinhood Salesforce Stripe Visa Ubisoft Wayfair Byju's Freshworks Google Microsoft Amazon Meta Apple Netflix Adobe