-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirst_recurring_char.cpp
More file actions
49 lines (39 loc) · 870 Bytes
/
first_recurring_char.cpp
File metadata and controls
49 lines (39 loc) · 870 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
42
43
44
45
46
47
48
49
//
// Created by Mayank Parasar on 2020-03-26.
//
/*
Given a string, return the first recurring letter that appears.
If there are no recurring letters, return None.
Example:
Input: qwertty
Output: t
Input: qwerty
Output: None
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
char first_recurring_char(string str) {
vector<char> result;
for(auto i : str) {
for(auto k : result) {
if( k == i) {
return (k);
}
}
result.push_back(i);
}
// if reached here.. then there is no repeating character
return '\0'; // return null
}
int main() {
string str = "qwertty";
// string str = "qwerty";
char result_char = first_recurring_char(str);
if(result_char != '\0')
cout << result_char;
else
cout << "None";
return 0;
}