fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. int numUniqueEmails(vector<string>& emails) {
  9. string::iterator it;
  10. cout << "Normalising the addresses:"<<endl;
  11. for (int i = 0; i < emails.size(); i++) {
  12. cout<<" "<<emails[i]<<"->";
  13. for (it = emails[i].begin(); it!=emails[i].end() && *it != '@'; it++) {
  14. cout<<*it; //ouch
  15. if (*it == '.') {
  16. emails[i].erase(it);
  17. it--;
  18. }
  19. if (*it == '+') {
  20. while (it!=emails[i].end() && *it != '@') {
  21. emails[i].erase(it);
  22. }
  23. break;
  24. }
  25. }
  26. cout<<"->"<<emails[i]<<endl; //ouch
  27. }
  28. cout << "Sorting."<<endl;
  29. sort(emails.begin(), emails.end());
  30. cout << "Remove dups."<<endl;
  31. for (int j = 0; j < emails.size()-1; j++) {
  32. if (emails[j] == emails[j + 1]) {
  33. emails.erase(emails.begin() + j);
  34. j--;
  35. }
  36. }
  37.  
  38. cout << "Sorted vector:"<<endl;
  39. for (auto &x:emails)
  40. cout<<" "<<x<<endl;
  41. return emails.size();
  42. }
  43. };
  44.  
  45.  
  46. int main() {
  47. //vector<string> v { "hello@x.y", "darkness@y.z", "my.old.friend@z.w" };
  48. //vector<string> v { "hello@x.y", "darkness@y.z", "hello@x.y" };
  49. //vector<string> v { "hel.lo@x.y", "darkness@y.z", "hello@x.y", "my.old.friend@z.w","myoldfriend@z.w" };
  50. vector<string> v { "", "hello", "darkness@", "hello@xy.", "dark.ness@" };
  51. Solution s;
  52. cout << s.numUniqueEmails(v)<<endl;
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
Normalising the addresses:
  ->->
  hello->hello->hello
  darkness@->darkness->darkness@
  hello@xy.->hello->hello@xy.
  dark.ness@->dark.ness->darkness@
Sorting.
Remove dups.
Sorted vector:
  
  darkness@
  hello
  hello@xy.
4