TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
palindrome_partitioning.cpp File Reference

Implements Palindrome Partitioning algorithm, giving you the minimum number of partitions you need to make. More...

#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <vector>
Include dependency graph for palindrome_partitioning.cpp:

Go to the source code of this file.

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 
namespace  palindrome_partitioning
 Functions for Palindrome Partitioning algorithm.
 

Functions

int dynamic_programming::palindrome_partitioning::pal_part (const std::string &str)
 
static void test ()
 Test Function.
 
int main ()
 Main function.
 

Detailed Description

Implements Palindrome Partitioning algorithm, giving you the minimum number of partitions you need to make.

palindrome partitioning uses dynamic programming and goes to all the possible partitions to find the minimum you are given a string and you need to give minimum number of partitions needed to divide it into a number of palindromes [Palindrome Partitioning] (https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/) overall time complexity O(n^2) For example: example 1:- String : "nitik" Output : 2 => "n | iti | k" For example: example 2:- String : "ababbbabbababa" Output : 3 => "aba | b | bbabb | ababa"

Author
[Sujay Kaushik] (https://github.com/sujaykaushik008)

Definition in file palindrome_partitioning.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 128 of file palindrome_partitioning.cpp.

128 {
129 test(); // execute the test
130 return 0;
131}
static void test()
Test Function.

◆ pal_part()

int dynamic_programming::palindrome_partitioning::pal_part ( const std::string & str)

Function implementing palindrome partitioning algorithm using lookup table method.

Parameters
strinput string
Returns
minimum number of partitions

Definition at line 45 of file palindrome_partitioning.cpp.

45 {
46 int n = str.size();
47
48 // creating lookup table for minimum number of cuts
49 std::vector<std::vector<int> > cuts(n, std::vector<int>(n, 0));
50
51 // creating lookup table for palindrome checking
52 std::vector<std::vector<bool> > is_palindrome(n,
53 std::vector<bool>(n, false));
54
55 // initialization
56 for (int i = 0; i < n; i++) {
57 is_palindrome[i][i] = true;
58 cuts[i][i] = 0;
59 }
60
61 for (int len = 2; len <= n; len++) {
62 for (int start_index = 0; start_index < n - len + 1; start_index++) {
63 int end_index = start_index + len - 1;
64
65 if (len == 2) {
66 is_palindrome[start_index][end_index] =
67 (str[start_index] == str[end_index]);
68 } else {
69 is_palindrome[start_index][end_index] =
70 (str[start_index] == str[end_index]) &&
71 is_palindrome[start_index + 1][end_index - 1];
72 }
73
74 if (is_palindrome[start_index][end_index]) {
75 cuts[start_index][end_index] = 0;
76 } else {
77 cuts[start_index][end_index] = INT_MAX;
78 for (int partition = start_index; partition <= end_index - 1;
79 partition++) {
80 cuts[start_index][end_index] =
81 std::min(cuts[start_index][end_index],
82 cuts[start_index][partition] +
83 cuts[partition + 1][end_index] + 1);
84 }
85 }
86 }
87 }
88
89 return cuts[0][n - 1];
90}

◆ test()

static void test ( )
static

Test Function.

Returns
void

Definition at line 98 of file palindrome_partitioning.cpp.

98 {
99 // custom input vector
100 std::vector<std::string> custom_input{"nitik", "ababbbabbababa", "abdc"};
101
102 // calculated output vector by pal_part Function
103 std::vector<int> calculated_output(3);
104
105 for (int i = 0; i < 3; i++) {
106 calculated_output[i] =
108 custom_input[i]);
109 }
110
111 // expected output vector
112 std::vector<int> expected_output{2, 3, 3};
113
114 // Testing implementation via assert function
115 // It will throw error if any of the expected test fails
116 // Else it will give nothing
117 for (int i = 0; i < 3; i++) {
118 assert(expected_output[i] == calculated_output[i]);
119 }
120
121 std::cout << "All tests passed successfully!\n";
122}