
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
Here we shall discuss a C++ Program to find number of ways to Partition a word in such a way that each word is a Palindrome.
Algorithms
Begin Take the word as input. Function partitionadd(vector<vector<string> > &u, string &s, vector<string> &tmp, int index): if (index == 0) tmp.clear() for i = index to length-1 st = st + s[i] if (checkPalin(st)) tmp.push_back(st) if (i+1 < length) partitionadd(u, s, tmp, i+1) else u.push_back(tmp) tmp = curr return End Begin Function partition(string st, vector<vector<string> >&u): vector<string> tmp addStrings(u, st, tmp, 0) printSol(u) //to print the solution return End
Example
#include <bits/stdc++.h> using namespace std; bool checkPalin(string s) { //check if string is palindrome or not. int length = s.length(); length--; for (int i=0; i<length; i++) { if (s[i] != s[length]) return false; length--; } return true; } void printSol(vector<vector<string> > part) { //print the solution for (int i = 0; i < part.size(); ++i) { for(int j = 0; j < part[i].size(); ++j) cout << part[i][j] << " "; cout << endl; } return; } void partitionadd(vector<vector<string> > &u, string &s, vector<string> &tmp, int index) { int length = s.length(); //store length of the string string st; vector<string> curr = tmp; // goes through all indexes and recursively add remaining partitions if current string is palindrome. if (index == 0) tmp.clear(); for (int i = index; i < length; ++i) { st = st + s[i]; if (checkPalin(st)) { tmp.push_back(st); if (i+1 < length) partitionadd(u,s,tmp,i+1); else u.push_back(tmp); tmp = curr; } } return; } void partition(string st, vector<vector<string> >&u) //generate all palindromic partitions of 'str' and stores the result in 'm'. { vector<string> tmp; addStrings(u, st, tmp, 0); printSol(u); return; } int main() { string s = "tutorials"; vector<vector<string> > part; cout<<"the number of partitions:"<<endl; partition(s, part); return 0; }
Output
the number of partitions: t u t o r i a l s tut o r i a l s
- Related Articles
- Print longest palindrome word in a sentence in C Program
- Program to partition two strings such that each partition forms anagram in Python
- C program to Replace a word in a text by another given word
- C++ program to read file word by word?
- C# program to find the index of a word in a string
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- Program to find number of ways we can split a palindrome in python
- Bell Numbers - Number of ways to Partition a Set in C++
- Find frequency of each word in a string in C#
- Program to find number of subsequence that are present inside word list in python
- Write a java program to reverse each word in string?
- Write a java program to tOGGLE each word in string?
- C++ program to find Second most repeated word in a sequence
- C++ Program to Print first letter of each word using regex

Advertisements