summaryrefslogtreecommitdiff
path: root/top-interview-questions/easy/strings/03_first_unique_character.cc
diff options
context:
space:
mode:
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.cc18
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 @@
1class Solution {
2public:
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};