C++ Code to Find Palindrome String with Substring



Suppose we have a string S with n letters. We have to find another string T, such that T is palindrome and S is subsequence of T.

So, if the input is like S = "ab", then the output will be "aabaa" (other answers are also available)

Steps

To solve this, we will follow these steps −

res := S reverse the array S res := res + S return res

Example

Let us see the following implementation to get better understanding −

Open Compiler
#include <bits/stdc++.h> using namespace std; string solve(string S){ string res = S; reverse(S.begin(), S.end()); res += S; return res; } int main(){ string S = "ab"; cout << solve(S) << endl; }

Input

ab

Output

abba
Updated on: 2022-03-29T12:01:05+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements