Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. char firstNotRepeatingCharacter(std::string s)
  2. {
  3. for(int searchedChar = 0; searchedChar < s.size();)
  4. {
  5. std::vector <int> duplicatePositions;
  6.  
  7. for(int possibleDuplicate = searchedChar+1; possibleDuplicate < s.size(); possibleDuplicate++)
  8. {
  9. if(s.at(searchedChar) == s.at(possibleDuplicate))
  10. {
  11. duplicatePositions.push_back(possibleDuplicate);
  12. }
  13. }
  14.  
  15. if(duplicatePositions.size() > 0)
  16. {
  17. for(int i = 0; i < duplicatePositions.size(); i++)
  18. {
  19. s.erase(s.begin()+(duplicatePositions.at(i)-i));
  20. }
  21.  
  22. s.erase(s.begin()+searchedChar);
  23. }
  24. else
  25. {
  26. return s.at(searchedChar);
  27. }
  28. }
  29.  
  30. return '_';
  31. }
  32.  
  33.  
  34. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  35.  
  36. char firstNotRepeatingCharacter(string s) {
  37. int count[26] = {};
  38. int order[26];
  39. int i = 0;
  40. for (c : s)
  41. if (!count[c-'a']++)
  42. order[i++] = c-'a';
  43. for (int j = 0; j < i; ++j)
  44. if (count[order[j]] == 1)
  45. return order[j]+'a';
  46. return '_';
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement