Skip to content

Commit fdebb41

Browse files
Add files via upload
1 parent dfa128d commit fdebb41

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Runtime: 48 ms, faster than 62.52% of C++ online submissions for First Unique Character in a String.
2+
// Memory Usage: 12.8 MB, less than 90.63% of C++ online submissions for First Unique Character in a String.
3+
4+
class Solution
5+
{
6+
public:
7+
int firstUniqChar(string s)
8+
{
9+
vector<bool> repeat(26, false);
10+
vector<int> memo(26, -1);
11+
12+
for (int i = 0; i < s.length(); ++i)
13+
{
14+
if (memo[s[i] - 'a'] != -1)
15+
{
16+
repeat[s[i] - 'a'] = true;
17+
}
18+
19+
memo[s[i] - 'a'] = i;
20+
}
21+
22+
int res = -1;
23+
for (int i = 0; i < 26; ++i)
24+
{
25+
if (repeat[i] == false && memo[i] != -1)
26+
{
27+
if (res < 0)
28+
{
29+
res = memo[i];
30+
}
31+
else
32+
{
33+
res = min(res, memo[i]);
34+
}
35+
}
36+
}
37+
38+
return res;
39+
}
40+
};

0 commit comments

Comments
 (0)