diff options
author | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
commit | 4689e4e80b479be25f7557d05818f5caa723aafa (patch) | |
tree | 4df25811fe2a9a15b401375178da6537f4b6063f /top-interview-questions/easy/strings/03_first_unique_character.cc |
Diffstat (limited to 'top-interview-questions/easy/strings/03_first_unique_character.cc')
-rw-r--r-- | top-interview-questions/easy/strings/03_first_unique_character.cc | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/top-interview-questions/easy/strings/03_first_unique_character.cc b/top-interview-questions/easy/strings/03_first_unique_character.cc new file mode 100644 index 0000000..871f761 --- /dev/null +++ b/top-interview-questions/easy/strings/03_first_unique_character.cc | |||
@@ -0,0 +1,18 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | int firstUniqChar(string s) { | ||
4 | int count[256] = {}; | ||
5 | |||
6 | for (char c : s) { | ||
7 | count[c]++; | ||
8 | } | ||
9 | |||
10 | for (size_t i = 0; i < s.size(); ++i) { | ||
11 | if (count[s[i]] == 1) { | ||
12 | return i; | ||
13 | } | ||
14 | } | ||
15 | |||
16 | return -1; | ||
17 | } | ||
18 | }; | ||