forked from Google-Developer-Student-Club-CCOEW/Competitive-Programming-2023-GDSC-CUMMINS-X-GDSC-MMCOE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst non-repeating character in a stream #61.txt
61 lines (49 loc) Β· 1.8 KB
/
First non-repeating character in a stream #61.txt
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
#include <bits/stdc++.h>
using namespace std;
string FirstNonRepeating(string str) //Function definition of FirstNonRepeating function
{
vector<char> a; //Declare a vector a
string ans = ""; //Declare a string ans for storing the answer
unordered_map<char, int> mpp; //Declare an unordered map to keep count of frequency of characters in string
for(int i=0; i<str.length(); i++) //Trace the string
{
if(mpp[str[i]] > 0) //Check if the frequency of that character in > 0
{
if(a.empty() == 0 && find(a.begin(), a.end(), str[i]) != a.end()) //Find the character in vector a
{
a.erase(find(a.begin(), a.end(), str[i])); //Erase the character in vector a
}
if(a.empty()) //Check if vector a is empty
{
ans += '#'; //Append # to string ans
}
else
{
ans += a[0]; //Append a[0] to ans string
}
}
else //If frequency is 0
{
mpp[str[i]]++; //Increase the frequency
a.push_back(str[i]); //Push that character in vector a
ans += a[0]; //Append a[0] to ans string
}
}
return ans; //Return ans string
}
int main()
{
string str;
cout<<"Enter a String in Lower Case : "<<endl; //Take string in lower case characters
cin>>str;
for(int i=0; i<str.length(); i++)
{
if(isupper(str[i])) //Check if that string has any upper case characters
{
cout<<"\nInvalid String!"<<endl;
return 0;
}
}
cout<<"\nOutput : "<<FirstNonRepeating(str)<<endl; //Call the FirstNonRepeating function
return 0;
}