Find the lexicographically largest palindromic Subsequence of a String
Last Updated :
15 Sep, 2022
Given a string S . The task is to find the lexicographically largest subsequence of the string which is a palindrome.
Examples:
Input : str = "abrakadabra"
Output : rr
Input : str = "geeksforgeeks"
Output : ss
The idea is to observe a character a is said to be lexicographically larger than a character b if it's ASCII value is greater than that of b.
Since the string has to be palindromic, the string should contain the largest characters only, as if we place any other smaller character in between the first and last character then it will make the string lexicographically smaller.
To find the lexicographically largest subsequence, first find the largest characters in the given string and append all of its occurrences in the original string to form the resultant subsequence string.
Below is the implementation of the above approach:
C++
// CPP program to find the largest
// palindromic subsequence
#include <bits/stdc++.h>
using namespace std;
// Function to find the largest
// palindromic subsequence
string largestPalinSub(string s)
{
string res;
char mx = s[0];
// Find the largest character
for (int i = 1; i < s.length(); i++)
mx = max(mx, s[i]);
// Append all occurrences of largest character
// to the resultant string
for (int i = 0; i < s.length(); i++)
if (s[i] == mx)
res += s[i];
return res;
}
// Driver Code
int main()
{
string s = "geeksforgeeks";
cout << largestPalinSub(s);
}
Java
// Java program to find the largest
// palindromic subsequence
class GFG
{
// Function to find the largest
// palindromic subsequence
static String largestPalinSub(String s)
{
String res = "";
char mx = s.charAt(0);
// Find the largest character
for (int i = 1; i < s.length(); i++)
mx = (char)Math.max((int)mx,
(int)s.charAt(i));
// Append all occurrences of largest
// character to the resultant string
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == mx)
res += s.charAt(i);
return res;
}
// Driver Code
public static void main(String []args)
{
String s = "geeksforgeeks";
System.out.println(largestPalinSub(s));
}
}
// This code is contributed by
// Rituraj Jain
Python3
# Python3 program to find the largest
# palindromic subsequence
# Function to find the largest
# palindromic subsequence
def largestPalinSub(s):
res = ""
mx = s[0]
# Find the largest character
for i in range(1, len(s)):
mx = max(mx, s[i])
# Append all occurrences of largest
# character to the resultant string
for i in range(0, len(s)):
if s[i] == mx:
res += s[i]
return res
# Driver Code
if __name__ == "__main__":
s = "geeksforgeeks"
print(largestPalinSub(s))
# This code is contributed by
# Rituraj Jain
C#
// C# program to find the largest
// palindromic subsequence
using System;
class GFG
{
// Function to find the largest
// palindromic subsequence
static string largestPalinSub(string s)
{
string res = "";
char mx = s[0];
// Find the largest character
for (int i = 1; i < s.Length; i++)
mx = (char)Math.Max((int)mx,
(int)s[i]);
// Append all occurrences of largest
// character to the resultant string
for (int i = 0; i < s.Length; i++)
if (s[i] == mx)
res += s[i];
return res;
}
// Driver Code
public static void Main()
{
string s = "geeksforgeeks";
Console.WriteLine(largestPalinSub(s));
}
}
// This code is contributed by Ryuga
PHP
<?php
// PHP program to find the largest
// palindromic subsequence
// Function to find the largest
// palindromic subsequence
function largestPalinSub($s)
{
$res="";
$mx = $s[0];
// Find the largest character
for ($i = 1; $i < strlen($s); $i++)
{
$mx = max($mx, $s[$i]);
}
// Append all occurrences of largest character
// to the resultant string
for ($i = 0; $i < strlen($s); $i++)
{
if ($s[$i] == $mx)
{
$res.=$s[$i];
}
}
return $res;
}
// Driver Code
$s = "geeksforgeeks";
echo(largestPalinSub($s));
// This code is contributed by princiraj1992
?>
JavaScript
<script>
// JavaScript program to find the largest
// palindromic subsequence
// Function to find the largest
// palindromic subsequence
function largestPalinSub(s)
{
let res = "";
let mx = s[0];
// Find the largest character
for (let i = 1; i < s.length; i++)
mx = String.fromCharCode(Math.max(mx.charCodeAt(),
s[i].charCodeAt()));
// Append all occurrences of largest
// character to the resultant string
for (let i = 0; i < s.length; i++)
if (s[i] == mx)
res += s[i];
return res;
}
let s = "geeksforgeeks";
document.write(largestPalinSub(s));
</script>
Complexity Analysis:
- Time Complexity: O(N), where N is the length of the string.
- Auxiliary Space: O(1)
Similar Reads
Lexicographically largest sub-sequence of the given string Given string str containing lowercase characters, the task is to find the lexicographically largest sub-sequence of str.Examples: Input: str = "abc" Output: c All possible sub-sequences are "a", "ab", "ac", "b", "bc" and "c" and "c" is the largest among them (lexicographically)Input: str = "geeksfor
7 min read
Lexicographically all Shortest Palindromic Substrings from a given string Given a string s of size N. The task is to find lexicographically all the shortest palindromic substrings from the given string. Examples: Input: s= "programming" Output: a g i m n o p r Explanation: The Lexicographical shortest palindrome substring for the word "programming" will be the single char
4 min read
Find a palindromic string B such that given String A is a subsequence of B Given a string A . Find a string B , 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 subs
6 min read
Count All Palindromic Subsequence in a given String Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s.Example: Input: s = "abcd"Output: 4Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d"Input: s = "aab"Output: 4Explanation: palindromic subsequenc
15+ min read
Lexicographically largest subsequence containing all distinct characters only once Given a string S, the task is to find the lexicographically largest subsequence that can be formed using all distinct characters only once from the given string. Examples: Input: S = ababcOutput: bacExplanation:All possible subsequences containing all the characters in S exactly once are {"abc", "ba
8 min read
Longest Palindromic Substring using hashing in O(nlogn) Given a string S, The task is to find the longest substring which is a palindrome using hashing in O(N log N) time. Input: S: âforgeeksskeegforâ, Output: âgeeksskeegâ Input: S: âGeeksâ, Output: âeeâ Hashing to Solve the Problem:The hashing approach to solving the longest palindromic substring proble
11 min read