Find a palindromic string B such that given String A is a subsequence of B
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
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static boolean checkPalindrome(String s)
{
String x = reverse(s);
return x.equals(s);
}
static String findStringB(String A)
{
String B = reverse(A);
B = B + A;
if (checkPalindrome(A))
{
return A;
}
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--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
public static void main(String[] args)
{
String A = "ab" ;
System.out.println(findStringB(A));
}
}
|
Python3
def checkPalindrome(s):
x = s[:: - 1 ]
if (x = = s):
return True
else :
return False
def findStringB(A):
B = A[:: - 1 ]
B = B + A
if (checkPalindrome(A)):
return A
return B
A = "ab"
print (findStringB(A))
|
C#
using System;
class GFG
{
static bool checkPalindrome(String s)
{
String x = reverse(s);
return x.Equals(s);
}
static String findStringB(String A)
{
String B = reverse(A);
B = B + A;
if (checkPalindrome(A))
{
return A;
}
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--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join( "" ,temparray);
}
public static void Main(String[] args)
{
String A = "ab" ;
Console.WriteLine(findStringB(A));
}
}
|
Javascript
<script>
function checkPalindrome(s)
{
let x = reverse(s);
return x == (s);
}
function findStringB(A)
{
let B = reverse(A);
B = B + A;
if (checkPalindrome(A))
{
return A;
}
return B;
}
function reverse(input)
{
let temparray = input.split( "" );
let left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
let temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return (temparray).join( "" );
}
let A = "ab" ;
document.write(findStringB(A));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...