-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepeated_char.cpp
More file actions
39 lines (32 loc) · 759 Bytes
/
repeated_char.cpp
File metadata and controls
39 lines (32 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
link: https://practice.geeksforgeeks.org/problems/repeated-character2058/1
problem:- Given a string consisting of lowercase english alphabets.
Find the repeated character present first in the string.
Input:
S = "geeksforgeeks"
Output: g
Explanation: g, e, k and s are the repeating
characters. Out of these, g occurs first.
Input:
S = "abcde"
Output: -1
Explanation: No repeating character present.
*/
class Solution
{
public:
char firstRep (string s)
{
map<char ,int> m;
for(int i = 0;i<s.size();i++){
m[s[i]]++;
}
for(int i = 0;i<s.size();i++){
if(m[s[i]] > 1){
return s[i];
break;
}
}
return '#'; // '#' = -1 in ascii
}
};