-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob159.cpp
More file actions
41 lines (34 loc) · 954 Bytes
/
Easy_Prob159.cpp
File metadata and controls
41 lines (34 loc) · 954 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
40
41
/* Given a string, return the first recurring character in it, or null if there is no recurring character.
For example, given the string "acbbac", return "b". Given the string "abcdef", return null. */
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include<vector>
#include <utility>
using namespace std;
char findFirstRecurringChar(string str) {
map<char, int> dict;
char* recurringChar = NULL;
for (char c : str) {
if (dict.count(c) == 0) {
dict[c] = 0;
} else {
dict[c] += 1;
recurringChar = &c;
break;
}
}
// return recurringChar ;
if (recurringChar == NULL) {
return '\0' ;
} else {
return *recurringChar;
}
}
int main(int argc, char *argv[])
{
string str1 = "abcddefb";
string str2 = "abcdef";
cout << findFirstRecurringChar(str1) << endl;
cout << findFirstRecurringChar(str2) << endl;
};