Largest Palindrome in the Array
Write a program to find the largest palindrome in the String Array
Input Format:
- In First line take an Integer which is size of the array
- In Second line take a list of Strings
Output Format:
print the Largest palindrome String
Example Input:
4
aba abab abaab abaaba
Output:
abaaba
Note:
If there are multiple largest palindromic strings with same length, print the lexicpraphically smallest one.
Solution:
n=int(input()) #largest palindrome in a array
l=[]
x=list(map(str,input().split()))
for i in range(len(x)):
k=x[i]
if(x[i]==k[::-1]): #strng rev is compared to original one
l.append(x[i])
maxlen=0
if(len(l)!=0):
for i in range(len(l)):
if(maxlen<len(l[i])):
maxlen=len(l[i])
m=[]
for j in l:
if(len(j)==maxlen):
m.append(j)
m.sort()
print(m[0]) #getting the lexically smallest one in largest palindrome
else:
print("-1")
Comments
Post a Comment