-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst Unique Character in a string.cpp
63 lines (53 loc) · 1.2 KB
/
First Unique Character in a string.cpp
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
62
63
/*
First Unique Character in a String
Solution
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
*/
//My Solution
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char,int> m,p;
vector<char> v;
for(int i=0;i<s.length();i++)
{
p[s[i]] = i;
if(m[s[i]] == 0)
{
v.push_back(s[i]);
}
m[s[i]]++;
}
for(auto n:v)
{
if(m[n] == 1)
{
return p[n];
}
}
return -1;
}
};
//not Mine
class Solution {
public:
int firstUniqChar(string s) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int count[26] = {0};
for (int i = 0; i < s.size(); ++i) {
count[s[i] - 'a'] += 1;
}
for (int i = 0; i < s.size(); ++i) {
if (count[s[i]-'a'] == 1)
return i;
}
return -1;
}
};