C++ code to find palindrome string whose substring is S

C++Server Side ProgrammingProgramming

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 −

#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
raja
Updated on 29-Mar-2022 12:01:05

Advertisements