Find a palindromic string B such that given String A is a subsequense of B | C ++ program to find a palindromic string B such that given String A is a subsequense of B ; Function to check if a string is palindrome ; Reversing a string ; check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; else return B ; Swap values of left and right ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPalindrome ( string s ) { string x = s ; reverse ( s . begin ( ) , s . end ( ) ) ; return s == x ; } string findStringB ( string A ) { string B = A ; reverse ( A . begin ( ) , A . end ( ) ) ; A = A + B ; if ( checkPalindrome ( B ) ) return B ; return A ; } string reverse ( string input ) { string temparray = input ; int left , right = 0 ; right = temparray . length ( ) - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) swap ( temparray [ left ] , temparray [ right ] ) ; return temparray ; } int main ( int argc , char const * argv [ ] ) { string A = " ab " ; cout << findStringB ( A ) << endl ; return 0 ; }