Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Unique Email Addresses


Solution


    class Solution {
    public:
        int numUniqueEmails(vector<string>& emails) {
            unordered_map<string, int> map;
            int ans = 0;
            for(int i = 0; i < emails.size(); i++) {
                string name = "";
                string dom = "";
                int j = 0;
                while(emails[i][j] != '@') {
                    if(emails[i][j] == '.') {
                        j++;
                        continue;
                    } 
                    else if(emails[i][j] == '+') {
                        break;
                    }
                    else {
                        name += emails[i][j];
                        j++;
                    }
                }
                while(emails[i][j] != '@') {
                    j++;
                }
                j++;
                while(j < emails[i].length()) {
                    dom += emails[i][j];
                    j++;
                }
                if(map[name + '@' + dom] == 0) {
                    ans++;
                    map[name + '@' + dom]++;
                }
            }
            return ans;
        }
    };