-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
55 lines (45 loc) · 1.02 KB
/
1.cpp
File metadata and controls
55 lines (45 loc) · 1.02 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
#include <iostream>
using namespace std;
/*
* Complete the 'getString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING input_str as parameter.
*/
string getString(string input_str)
{
string s = input_str;
int n = input_str.size();
int count[26] = {0};
int vis[26] = {0};
for (int i = 0; i < n; i++)
{
count[s[i] - 'a']++;
}
string res = "";
for (int i = 0; i < n; i++)
{
count[s[i] - 'a']--;
if (!vis[s[i] - 'a'])
{
while (res.size() > 0 && res.back() < s[i] && count[res.back() - 'a'] > 0)
{
vis[res.back() - 'a'] = 0;
res.pop_back();
}
res += s[i];
vis[s[i] - 'a'] = 1;
}
}
return res;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string input_str;
getline(cin, input_str);
string result = getString(input_str);
fout << result << "\n";
fout.close();
return 0;
}