Skip to content

Commit 476798f

Browse files
committed
add 387 folder & cpp
1 parent a6d3f2c commit 476798f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int firstUniqChar(string s) {
4+
vector<int> cnts(26, 0), pos(26, s.size()) ;
5+
6+
7+
for (int i = s.size()-1; i >= 0; --i)
8+
{
9+
int index = s[i] - 'a' ;
10+
cnts[index]++ ;
11+
pos[index] = i ;
12+
}
13+
14+
int p = s.size() ;
15+
for (int i = 0; i < 26; ++i)
16+
{
17+
if (cnts[i] == 1 && pos[i] < p)
18+
p = pos[i] ;
19+
}
20+
21+
return p != s.size()? p: -1 ;
22+
}
23+
};

0 commit comments

Comments
 (0)