Tuesday, November 21, 2017

Palindrome Missing Alphabet

String S which is a palindrome is passed as the input. But just one alphabet A is missing in S. The program must print the missing alphabet A. Note: The FIRST alphabet of S will always be present.

Input Format:
The first line contains S.

Output Format:
The first line contains the missing alphabet A.

Boundary Conditions:
The length of the input string S is between 3 to 100. The FIRST alphabet of S will always be present.

Example Input/Output 1:
Input:
malayaam
Output:
l

Solution 1:
#include <iostream>
using namespace std;
int main(){
    string c;
    cin>>c;
    int l=c.size();
    for(int i=0,j=l-1;i<l/2;i++,j--) {
        if(c[i]!=c[j]&& c[i]==c[j-1]&&(i!=j-1))
        {
                cout<<c[j];
                break;
         }
         else if(c[i]!=c[j])
            {
                cout<<c[i];
                break;
            }
        }
}

Solution 2:
s=input()
n=len(s)
for i in range(n):
    if s.count(s[i])&1==1:
       if n&1==0 and i!=n//2 or n&1==1:
           print(s[i])
           break
Share: