Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Run-time complexity O(n2)
- // Memory complexity O(1)
- /*
- int firstUniqChar(string s) {
- for (int i = 0; i < s.size(); ++i) {
- if (count(s.begin(), s.end(), s[i]) == 1)
- return i;
- }
- return -1;
- }
- */
- // Run-time complexity O(n)
- // Memory complexity O(1)
- // 26 different letters
- int firstUniqChar(string s) {
- vector<int> abc(26, 0);
- for (char c : s) {
- abc[c - 'a'] += 1;
- }
- for (int i = 0; i < s.size(); ++i) {
- if (abc[s[i] - 'a'] == 1)
- return i;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement