Advertisement
bbescos

Untitled

Jan 28th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1.     // Run-time complexity O(n2)
  2.     // Memory complexity O(1)
  3.     /*
  4.     int firstUniqChar(string s) {
  5.         for (int i = 0; i < s.size(); ++i) {
  6.             if (count(s.begin(), s.end(), s[i]) == 1)
  7.                 return i;
  8.         }
  9.         return -1;
  10.     }
  11.     */
  12.    
  13.     // Run-time complexity O(n)
  14.     // Memory complexity O(1)
  15.     // 26 different letters
  16.     int firstUniqChar(string s) {
  17.         vector<int> abc(26, 0);
  18.        
  19.         for (char c : s) {
  20.             abc[c - 'a'] += 1;
  21.         }
  22.        
  23.         for (int i = 0; i < s.size(); ++i) {
  24.             if (abc[s[i] - 'a'] == 1)
  25.                 return i;
  26.         }
  27.         return -1;
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement