Task: Given a string S. Find the first repeated character in it.
Example:
Input:
1
geeksforgeeks
Output:
e // first repeated character and second time occurred character index is smallest.
Here is the Code:
Program in C++:
- #include<iostream>
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- string s;
- cin>>s;
- map<char,int> m;
- int c=0;
- for(int i=0;i<s.length();i++)
- {
- m[s[i]]++;
- if(m[s[i]]==2)
- {
- cout<<s[i]<<endl;
- c=1;
- break;
- }
- }
- if(c==0)
- {
- cout<<-1<<endl;
- }
- }
- return 0;
- }
Here is the Video:
Comments
Post a Comment