forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path387.cpp
More file actions
36 lines (29 loc) · 809 Bytes
/
387.cpp
File metadata and controls
36 lines (29 loc) · 809 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
//387. First Unique Character in a String
/*Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.*/
/*
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
*/
//Brute force solution, traverse string s 2 times. First time, store counts of every character into the hash table, second time, find the first character that appears only once.
class Solution {
public:
int firstUniqChar(string s)
{
unordered_map<char, int> mpp;
for (auto &i : s)
{
mpp[i]++;
}
for (int i = 0; i < s.size(); i++)
{
if (mpp[s[i]] == 1)
return i;
}
return -1;
}
};
// This code is contributed by DvnOshin.