387_first_unique_character_in_a_string.cpp 536 B

12345678910111213141516171819202122232425262728293031
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. using namespace std;
  5. class Solution
  6. {
  7. public:
  8. int firstUniqChar(string s)
  9. {
  10. vector<int> table(26);
  11. for (int i = 0; i < s.size(); i++)
  12. {
  13. table[s[i] - 'a'] += 1;
  14. }
  15. for (int i = 0; i < s.size(); i++)
  16. {
  17. if (table[s[i] - 'a'] == 1)
  18. return i;
  19. }
  20. return -1;
  21. }
  22. };
  23. int main()
  24. {
  25. string s = "altcleetcodde";
  26. Solution sol;
  27. cout << sol.firstUniqChar(s) << endl;
  28. }