
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Lexicographically Largest Palindromic Subsequence in Python
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
Advertisements