We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents c87bd13 + 476798f commit 2639758Copy full SHA for 2639758
solution/387.First Unique Character in a String/Solution.cpp
@@ -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