fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. string s = "loveleetcode";
  7. int count[26] = {0};
  8.  
  9. for(int i = 0; i < s.length(); i++) {
  10. count[s[i] - 'a']++;
  11. }
  12.  
  13. for(int i = 0; i < s.length(); i++) {
  14. if(count[s[i] - 'a'] == 1) {
  15. cout << i << endl;
  16. return 0;
  17. }
  18. }
  19.  
  20. cout << -1 << endl;
  21. return 0;
  22. }
  23.  
  24.  
  25.  
Success #stdin #stdout 0.01s 5320KB
stdin
loveleetcode
stdout
2