From 5af36c7bcbfd94a865261adfda3360330ceaa781 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Sat, 11 Nov 2023 22:51:34 -0500 Subject: [PATCH] adding queue problem --- leetcode/p387_first_unique_char.cpp | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 leetcode/p387_first_unique_char.cpp 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; + } +};