From 4689e4e80b479be25f7557d05818f5caa723aafa Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 5 Feb 2025 18:36:31 -0800 Subject: Initial commit. --- .../easy/strings/03_first_unique_character.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 top-interview-questions/easy/strings/03_first_unique_character.cc (limited to 'top-interview-questions/easy/strings/03_first_unique_character.cc') 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 @@ +class Solution { +public: + int firstUniqChar(string s) { + int count[256] = {}; + + for (char c : s) { + count[c]++; + } + + for (size_t i = 0; i < s.size(); ++i) { + if (count[s[i]] == 1) { + return i; + } + } + + return -1; + } +}; -- cgit v1.2.3