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 = abaExplanation: "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:

`# 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 palindrome ` `def` `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 B ` `def` `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 Code ` `A ` `=` `"ab"` `print` `(findStringB(A)) ` |

*chevron_right*

*filter_none*

**Output:**

baab

## Recommended Posts:

- Find the count of palindromic sub-string of a string in its sorted form
- Find all palindromic sub-strings of a given string | Set 2
- Find all distinct palindromic sub-strings of a given string
- Find the lexicographically largest palindromic Subsequence of a String
- Check if a string contains a palindromic sub-string of even length
- Make palindromic string non-palindromic by rearranging its letters
- Lexicographically first palindromic string
- Count All Palindromic Subsequence in a given String
- Count palindromic characteristics of a String
- Given a string, print all possible palindromic partitions
- Print all palindromic partitions of a string
- Number of strings of length N with no palindromic sub string
- Print all the palindromic permutations of given string in alphabetic order
- Rearrange the string to maximize the number of palindromic substrings
- Find length of longest subsequence of one string which is substring of another string