
- 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
Find a palindromic string B such that given String A is a subsequence of B in C++
Suppose we have a string A, we have to find another string B, that will be palindrome. And the given string A will be subsequence of B. The subsequence of a string is a string that can be formed by it by deleting some characters without changing the order of remaining characters. Suppose the string is “cotst”, then generated string will be “contest”. For the input of this program we have chosen A = “ab”, the generated string will be “abba”, this is palindrome.
To solve this, we will follow this approach. This is very simple, we will reverse the A, then append the reversed part after A, and form B. So B = A + reverse(A)
Example
#include<iostream> #include<algorithm> using namespace std; bool isPalindrome(string str) { string temp = str; reverse(str.begin(), str.end()); return str == temp; } string formPalindromeStr(string A) { string B = A; reverse(A.begin(), A.end()); A = A + B; if (isPalindrome(B)) return B; return A; } string reverse(string input) { string temp = input; int left, right = 0; right = temp.length() - 1; for (left = 0; left < right; left++, right--) swap(temp[left], temp[right]); return temp; } int main(int argc, char const *argv[]) { string A = "Hello"; cout << "The B is: " << formPalindromeStr(A); }
Output
The B is: olleHHello
- Related Articles
- Count all Palindromic Subsequence in a given String in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Counting all possible palindromic subsequence within a string in JavaScript
- Find all distinct palindromic sub-strings of a given String in Python
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Count subsequence of length three in a given string in C++
- Maximum number of removals of given subsequence from a string in C++
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Print all palindromic partitions of a string in C++
- Program to find out the palindromic borders in a string in python
- Program to check given string is anagram of palindromic or not in Python
- Program to find a good string from a given string in Python

Advertisements