-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD14_NonRepeatingCharacter.cpp
More file actions
60 lines (47 loc) · 1.7 KB
/
D14_NonRepeatingCharacter.cpp
File metadata and controls
60 lines (47 loc) · 1.7 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
// Given a string s consisting of lowercase Latin Letters. Return the first non-repeating character in s. If there is no non-repeating character, return '$'.
// Note: When you return '$' driver code will output -1.
// Examples:
// Input: s = "geeksforgeeks"
// Output: 'f'
// Explanation: In the given string, 'f' is the first character in the string which does not repeat.
// Input: s = "racecar"
// Output: 'e'
// Explanation: In the given string, 'e' is the only character in the string which does not repeat.
// Input: s = "aabbccc"
// Output: -1
// Explanation: All the characters in the given string are repeating
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
char firstUniqChar(string s) {
unordered_map<char, int> freq_map;
// Step 1: Count the frequency of each character
for (char c : s) {
freq_map[c]++;
}
// Step 2: Find the first character with frequency 1
for (char c : s) {
if (freq_map[c] == 1) {
return c; // return the first non-repeating character
}
}
// Step 3: If no such character exists, return '$'
return '$';
}
};
int main() {
Solution solution;
string s;
cin >> s; // Read input string
char result = solution.firstUniqChar(s);
// Output result as per the requirement
if (result == '$') {
cout << -1 << endl; // Output -1 when there is no non-repeating character
} else {
cout << result << endl; // Output the first non-repeating character
}
return 0;
}