Guest User

Untitled

a guest
Dec 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int firstUniqChar(string s) {
  4. if(s.empty()) {
  5. return -1;
  6. }
  7.  
  8. int hashmap[256] = {0};
  9. int cur = 0, next = 1;
  10.  
  11. const int len = s.length();
  12. hashmap[s[cur]]++;
  13.  
  14. while(next < len) {
  15. hashmap[s[next]]++;
  16.  
  17. //>> if current char shows more than one time,
  18. //>> then find next char which shows at first time
  19. while(cur < len && hashmap[s[cur]] > 1) {
  20. ++cur;
  21. }
  22.  
  23. if(cur == len) {
  24. return -1;
  25. }
  26. else if (hashmap[s[cur]] == 0) {
  27. //>> find a char which shows at first time
  28. hashmap[s[cur]]++;
  29. next = cur; //>> reset next pointer
  30. }
  31.  
  32. ++next;
  33. }
  34.  
  35. return cur;
  36. }
  37. };
Add Comment
Please, Sign In to add comment