I was trying to solve leetcode problem "929. Unique Email Addresses", the code works fine at my computer on Visual Studio Code but when I pasted it on the leetcode, I got address sanitizer heap-buffer-overflow error. The code is shown below:
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
string::iterator it;
for (int i = 0; i < emails.size(); i++) {
for (it = emails[i].begin(); *it != '@'; it++) {
if (*it == '.') {
emails[i].erase(it);
it--;
}
if (*it == '+') {
while (*it != '@') {
emails[i].erase(it);
}
break;
}
}
sort(emails.begin(), emails.end());
for (int j = 0; j < emails.size(); j++) {
if (emails[j] == emails[j + 1]) {
emails.erase(emails.begin() + j);
j--;
}
}
}
return emails.size();
}
};
Can someone let me know why the error occurs?