
- 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
Minimum number of Appends needed to make a string palindrome in C++
Problem statement
Given a string, find minimum characters to be appended to make a string palindrome.
Example
If string is abcac then we can make string palindrome by appending 2 highlighed characters i.e. abcacba
Algorithm
- Check if string is already palindrome, if yes then no need to append any characters.
- One by one remove a character from string and check whether remaining string is palindrome or not
- Repeat above process until string becomes palidrome
- Return the number of characters removed so far as a final answer
Example
#include <iostream> #include <cstring> using namespace std; bool isPalindrome(char *str) { int n = strlen(str); if (n == 1) { return true; } int start = 0, end = n - 1; while (start < end) { if (str[start] != str[end]) { return false; } ++start; --end; } return true; } int requiredAppends(char *str) { if (isPalindrome(str)) { return 0; } return 1 + requiredAppends(str + 1); } int main() { char *str = "abcac"; cout << "Characters to be appended = " << requiredAppends(str) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Characters to be appended = 2
- Related Articles
- Program to check minimum number of characters needed to make string palindrome in Python
- Minimum number of deletions to make a string palindrome in C++.
- Minimum Insertion Steps to Make a String Palindrome in C++
- Minimum number of letters needed to make a total of n in C++.
- C++ program to count minimum number of operations needed to make number n to 1
- Find minimum number of merge operations to make an array palindrome in C++
- Program to count number of minimum swaps required to make it palindrome in Python
- Minimum removal to make palindrome permutation in C++
- C++ program to find minimum how many operations needed to make number 0
- Program to find minimum number of characters to be added to make it palindrome in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- Program to find minimum number of operations to make string sorted in Python
- Find minimum operations needed to make an Array beautiful in C++
- Counting steps to make number palindrome in JavaScript

Advertisements