Suppose we have a string S; we have to find the lexicographically largest palindromic subsequence of that string.
So, if the input is like "tutorialspointtutorial", then the output will be "uu"
To solve this, we will follow these steps −
ans := blank string
max_val := s[0]
for i in range 1 to size of s, do
max_val := maximum of max_val, s[i]
for i in range 0 to size of s, do
if s[i] is same as max_val, then
ans := ans + s[i]
return ans
Example
Let us see the following implementation to get better understanding −
def largest_palindromic_substr(s): ans = "" max_val = s[0] for i in range(1, len(s)): max_val = max(max_val, s[i]) for i in range(0, len(s)): if s[i] == max_val: ans += s[i] return ans s = "tutorialspointtutorial" print(largest_palindromic_substr(s))
Input
"tutorialspointtutorial"
Output
uu