

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Count all Palindromic Subsequence in a given String in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Find FIRST & FOLLOW for the following Grammar. S → A a A | B b B A → b B B → ε
- Python a += b is not always a = a + b
- Find largest d in array such that a + b + c = d in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Larger of a^b or b^a in C++
- Find numbers a and b that satisfy the given condition in C++
- Counting all possible palindromic subsequence within a string in JavaScript
- Find three element from different three arrays such that that a + b + c = sum in Python
- Count of all possible values of X such that A % X = B in C++
Advertisements