Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Palindrome Partitioning problem solution

YASH PAL, 31 July 2024

In this Leetcode Palindrome Partitioning problem solution, we have Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. A palindrome string is a string that reads the same backward as forward.

Leetcode Palindrome Partitioning problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.

Problem solution in Python.

class Solution(object):
    def partition(self, s):
        if not s:
            return []
        memo = {}
        def partition(cur):
            if cur==len(s):
                return [[]]
            if cur in memo:
                return memo[cur]
            res = []
            for i in xrange(cur,len(s)):
                if s[cur:i+1]==s[cur:i+1][::-1]:
                    for tmp in partition(i+1):
                        res.append([s[cur:i+1]]+tmp)
            memo[cur] = res
            return res
        return partition(0)

Problem solution in Java.

class Solution {
    List<List<String>> result = new ArrayList<>();
    
    public List<List<String>> partition(String s) {
        helper(s, 0, new ArrayList<>());
        return result;
    }
    
    public void helper(String s, int index, List<String> combination){
        if (index == s.length()) {
            result.add(new ArrayList<>(combination));
            return;
        }

        for (int i = index; i < s.length(); i++) {

            if (isValid(s.substring(index, i + 1))) {
                combination.add(s.substring(index, i + 1));
                helper(s, i + 1, combination);
                combination.remove(combination.size() - 1);
            }

        }
    }
    
    public boolean isValid(String s){
        if(s.length() == 1){
            return true;
        }
        int left  = 0;
        int right = s.length() - 1;
        
        while(left < right){
            if(s.charAt(left) != s.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        
        return true;
    }
}

Problem solution in C++.

public:
    vector<vector<string>> partition(string_view s) {
        const int n = size(s);
        auto is_poly = [](string_view str) {
            for (int i = 0; i < size(str) / 2; ++i) {
                if (str[i] != str[size(str) - i - 1]) {
                    return false;
                }
            }
            return true;
        };
        vector<vector<string>> res;
        function<void(int i)> backtrack;
        vector<string> current;
        backtrack = [&](int idx) {
            if (idx == n) {
                res.push_back(current);
                return;
            }
            string str;
            for (int i = idx; i < n; ++i) {
                str += s[i];
                if (is_poly(str)) {
                    current.push_back(str);
                    backtrack(i + 1);
                    current.pop_back();
                }
            }
        };
        backtrack(0);
        return res;
    }

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes