Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Palindrome Partitioning Editorial

DSA Editorial, Solution and Code

Practice Problem Link: https://workat.tech/problem-solving/practice/palindromic-partitioning

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s, partition s such that every substring of the partition is a palindrome. Find all possible ways of palindromic partitioning.

Approach

The approach to this problem is to use recursive backtrack to find every possible partition of the string and check which of the partitions have palindrome substrings only. We traverse the string from the start considering all the substrings and after finding a palindrome substring we search for the palindromes in the remaining string recursively.

Analysis 

  • Time Complexity: Exponential
  • Auxiliary Space Complexity: O(1)

Implementation

C++
bool isPalindrome(string &s, int start, int end)
{
    while (start < end)
    {
        if (s[start] != s[end]) {
            return false;
		}
        start++;
        end--;
    }
    return true;
}
void findAllPalindromes (string &s, int start, vector<string> &currentPalindrome, vector<vector<string>> &allPalindromes) {
	if(start >= s.size()) {
		allPalindromes.push_back(currentPalindrome);
		return;
	}
	for (int i = start; i < s.size(); i++) {
		if (isPalindrome(s, start, i)) {
			currentPalindrome.push_back(s.substr(start, i - start + 1));
			findAllPalindromes (s, i + 1, currentPalindrome, allPalindromes);
			currentPalindrome.pop_back();
		}
	}
}
vector<vector<string> > getPartitions(string s) {
    vector<vector<string>> allPalindromes;
	vector<string> currentPalindrome;
	findAllPalindromes (s, 0, currentPalindrome, allPalindromes);
	return allPalindromes;
}
Java
class Solution {
	boolean isPalindrome(String s, int start, int end)
	{
		while (start < end)
		{
			if (s.charAt(start) != s.charAt(end)) {
				return false;
			}
			start++;
			end--;
		}
		return true;
	}
	void findAllPalindromes (String s, int start, List<String> currentPalindrome, List<List<String>> allPalindromes) {
		if(start >= s.length()) {
			allPalindromes.add(new ArrayList<> (currentPalindrome));
			return;
		}
		for (int i = start; i < s.length(); i++) {
			if (isPalindrome(s, start, i)) {
				currentPalindrome.add(s.substring(start, i + 1));
				findAllPalindromes (s, i + 1, currentPalindrome, allPalindromes);
				currentPalindrome.remove(currentPalindrome.size() - 1);
			}
		}
	}
	List<List<String>> getPartitions(String s) {
	    List<List<String>> allCombinations = new ArrayList<List<String>> ();
		List<String> currentCombination = new ArrayList<String> ();
		findAllPalindromes (s, 0, currentCombination, allCombinations);
		return allCombinations;
	}
}
Related Content
Best Time to Buy and Sell Stocks
Best Time to Buy and Sell Stocks II
Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock IV
Climbing Stairs
Coin Change
Collect Jewels
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Decode Ways
Edit Distance (Levenshtein Distance)
Minimum Egg Drops
Generate Parentheses
Kth Permutation Sequence
Letter Combinations of a Phone Number
Longest Common Subsequence (LCS)
Longest Increasing Subsequence (LIS)
Longest Palindromic Substring (LPS)
Maximum path sum in matrix
Maximum Product Subarray
Maximum Sum Increasing Subsequence
N Queens Problem
Palindrome Partitioning 2
Rat In A Maze
Regular Expression Matching
Restore IP Addresses
Rod Cutting
String Permutations
Subset Sum
Subset Sum
Subset Sum 2
Subsets
Subsets - II
Sudoku Solver
Trapping Rain Water
Tug of War
Unique Paths
Wildcard Matching
Word Break
Word Break
Word Break - II
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet