Find a palindromic string B such that given String A is a subsequense of B - GeeksforGeeks

28 Jul, 2021
Given a string . Find a string , where B is a palindrome and A is a subsequence of B.
A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, “cotst” is a subsequence of “contest”.
A palindrome is a string that reads the same forward or backward.
Examples: 
Input : A = "aba"
Output : B = aba
Explanation : "aba" is a subsequence of "aba" 
which is a palindrome.

Input : A = "ab"
Output : B = abba
Approach: Let reverse(s) be the reverse of a string . Now, s + reverse(s) will always have as a subsequence (as first half) and it is a palindrome.Therefore, B = A + reverse(A).
Below is the implementation of the above approach:  
C++14
Java
Python3
C#
Javascript
// C++ program to find a palindromic string B// such that given String A is a subsequense of B#include <bits/stdc++.h>using namespace std; // Function to check if a string is palindromebool checkPalindrome(string s){    // Reversing a string    string x = s;    reverse(s.begin(), s.end());     // check if reversed string is equal    // to given string    return s == x;} // Function to find a palindromic string B// such that given String A is a subsequense of Bstring findStringB(string A){    // Reversing the string A    string B = A;    reverse(A.begin(), A.end());    A = A + B;     // If the string A is already a palindrome    // return A    if (checkPalindrome(B))        return B;     // else 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 values of left and right        swap(temparray[left], temparray[right]);     return temparray;} // Driver Codeint main(int argc, char const *argv[]){    string A = "ab";    cout << findStringB(A) << endl;    return 0;} // This code is contributed by// sanjeev2552
// Java program to find a palindromic string B// such that given String A is a subsequense of B class GFG{     // Function to check if a string is palindrome    static boolean checkPalindrome(String s)    {        // Reversing a string        String x = reverse(s);                 // check if reversed string is equal        // to given string        return x.equals(s);    }         // Function to find a palindromic string B    // such that given String A is a subsequense of B    static String findStringB(String A)    {         // Reversing the string A        String B = reverse(A);        B = B + A;         // If the string A is already a palindrome        // return A        if (checkPalindrome(A))        {            return A;        }         // else return B        return B;    }     static String reverse(String input)    {        char[] temparray = input.toCharArray();        int left, right = 0;        right = temparray.length - 1;         for (left = 0; left < right; left++, right--)        {            // Swap values of left and right            char temp = temparray[left];            temparray[left] = temparray[right];            temparray[right] = temp;        }        return String.valueOf(temparray);    }         // Driver Code    public static void main(String[] args)    {        String A = "ab";        System.out.println(findStringB(A));    }} // This code contributed by Rajput-Ji
# Python program to find a palindromic string B# such that given String A is a subsequense of B # Function to check if a string is palindromedef checkPalindrome(s):    # Reversing a string    x = s[::-1]    # check if reversed string is equal    # to given string    if(x == s):        return True    else:        return False         # Function to find a palindromic string B# such that given String A is a subsequense of Bdef findStringB(A):         # Reversing the string A    B = A[::-1]         B = B + A         # If the string A is already a palindrome    # return A    if(checkPalindrome(A)):        return A           # else return B       return B # Driver CodeA ="ab"print(findStringB(A))
// C# program to find a palindromic string B// such that given String A is a subsequense of Busing System; class GFG{      // Function to check if a string is palindrome    static bool checkPalindrome(String s)    {        // Reversing a string        String x = reverse(s);                  // check if reversed string is equal        // to given string        return x.Equals(s);    }          // Function to find a palindromic string B    // such that given String A is a subsequense of B    static String findStringB(String A)    {          // Reversing the string A        String B = reverse(A);        B = B + A;          // If the string A is already a palindrome        // return A        if (checkPalindrome(A))        {            return A;        }          // else return B        return B;    }      static String reverse(String input)    {        char[] temparray = input.ToCharArray();        int left, right = 0;        right = temparray.Length - 1;          for (left = 0; left < right; left++, right--)        {            // Swap values of left and right            char temp = temparray[left];            temparray[left] = temparray[right];            temparray[right] = temp;        }        return String.Join("",temparray);    }          // Driver Code    public static void Main(String[] args)    {        String A = "ab";        Console.WriteLine(findStringB(A));    }} // This code has been contributed by 29AjayKumar
<script> // Javascript program to find a palindromic string B// such that given String A is a subsequense of B // Function to check if a string is palindromefunction checkPalindrome(s){         // Reversing a string    let x = reverse(s);           // Check if reversed string is equal    // to given string    return x == (s);} // Function to find a palindromic string B// such that given String A is a subsequense of Bfunction findStringB(A){         // Reversing the string A    let B = reverse(A);    B = B + A;     // If the string A is already a palindrome    // return A    if (checkPalindrome(A))    {        return A;    }         // Else return B    return B;} function reverse(input){    let temparray = input.split("");    let left, right = 0;    right = temparray.length - 1;     for(left = 0; left < right; left++, right--)    {                 // Swap values of left and right        let temp = temparray[left];        temparray[left] = temparray[right];        temparray[right] = temp;    }    return (temparray).join("");} // Driver Codelet A = "ab";document.write(findStringB(A)); // This code is contributed by rag2127 </script>
baab
 
Rajput-Ji
29AjayKumar
sanjeev2552
rag2127
palindrome
Reverse
Greedy
Strings
Strings
Greedy
palindrome
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Program for First Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Max Flow Problem Introduction
Write a program to reverse an array or string
Reverse a string in Java
C++ Data Types
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack