SHARE
TWEET

Untitled

bbescos Jan 28th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top