Thursday, September 21, 2017

First Repeating Character From Last

A string S is passed as the input. S has at least one repeating character. The program must print the first repeating character C from the last.

Input Format:
The first line contains S.

Output Format:
The first line contains C.

Boundary Conditions: Length of S will be from 3 to 100.

Example Input/Output 1:
Input:
abcdexyzbwqpooplj
Output:
p

Solution:
#include <bits/stdc++.h>
using namespace std;
int main()
{
 string s;
 getline(cin,s);
 int i;
 map<char,int> m;
 for(i=s.length()-1;i>=0;i--)
        m[s[i]]++;
 for(i=s.length()-1;i>=0;i--)
        if(m[s[i]]>1)
             break;
 i==0?cout<<"0":cout<<s[i];
}
Share: