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