diff --git a/leetcode/p387_first_unique_char.cpp b/leetcode/p387_first_unique_char.cpp new file mode 100644 index 0000000..7e3ec7b --- /dev/null +++ b/leetcode/p387_first_unique_char.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int firstUniqChar(string s) { + int len = s.length(); + std::unordered_map map1; + // traverse the string and count the frequence of each character. + for(int i=0;i myQueue; + for(int i=0;i1){ + myQueue.pop(); + index++; + }else{ + // found the non-repeating character + break; + } + } + // if not found, return -1. + if(index>=len){ + return -1; + } + return index; + } +};