Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int firstUniqChar(string s) {
- if(s.empty()) {
- return -1;
- }
- int hashmap[256] = {0};
- int cur = 0, next = 1;
- const int len = s.length();
- hashmap[s[cur]]++;
- while(next < len) {
- hashmap[s[next]]++;
- //>> if current char shows more than one time,
- //>> then find next char which shows at first time
- while(cur < len && hashmap[s[cur]] > 1) {
- ++cur;
- }
- if(cur == len) {
- return -1;
- }
- else if (hashmap[s[cur]] == 0) {
- //>> find a char which shows at first time
- hashmap[s[cur]]++;
- next = cur; //>> reset next pointer
- }
- ++next;
- }
- return cur;
- }
- };
Add Comment
Please, Sign In to add comment