-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55_FirstCharacterInStream.cpp
More file actions
72 lines (57 loc) · 1.16 KB
/
55_FirstCharacterInStream.cpp
File metadata and controls
72 lines (57 loc) · 1.16 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# include <iostream>
# include <cstdlib>
# include <vector>
# include <string>
using namespace std;
void display_vec(vector<int> &vec)
{
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
}
void display_vec(vector<string> &vec)
{
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
}
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
s.push_back(ch);
freq[ch]++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for (int i = 0; i < s.size(); i++)
if (freq[s[i]] == 1)
return s[i];
return '#';
}
string GetStr()
{
return s;
}
private:
char freq[256] = {0};
string s;
};
int main(int argc, char *argv[])
{
string str = "google";
Solution sol;
char res;
// cout << str << endl;
for (int i = 0; i < str.size(); i++)
{
sol.Insert(str[i]);
cout << sol.GetStr() << endl;
res = sol.FirstAppearingOnce();
cout << res << endl;
}
return 0;
}