-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractUniqueChar.cpp
More file actions
59 lines (41 loc) · 992 Bytes
/
extractUniqueChar.cpp
File metadata and controls
59 lines (41 loc) · 992 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
50
51
52
53
54
55
56
57
58
59
// Extract Unique characters
// Given a string, you need to remove all the duplicates. That means, the output string should contain each character only once. The respective order of characters should remain same.
// Input format :
// String S
// Output format :
// Output String
// Constraints :
// 1 <= Length of S <= 50000
// Sample Input 1 :
// ababacd
#include<iostream>
#include<map>
using namespace std;
char* uniqueChar(char *str){
// Write your code here
unordered_map<char, bool> visited;
queue<char> q;
int i;
for(i = 0; str[i] != 0; i++)
{
if(!visited[str[i]])
{
visited[str[i]] = true;
q.push(str[i]);
}
}
char* newString = new char[i];
i = 0;
while(!q.empty())
{
newString[i] = q.front();
q.pop();
i++;
}
return newString;
}
int main() {
char input[1000000];
cin >> input;
cout << uniqueChar(input) << endl;
}