DEV Community

kevin074
kevin074

Posted on

Leetcode Diary: Unique Email Addresses

https://leetcode.com/problems/unique-email-addresses/submissions/

this is a good question to start with if you haven't had leetcode style interviews or it's been a while since you leetcoded.

the problem is rather simple, to process email strings to eliminate certain characters. You should follow three rules:
1.) leave the parts after @ sign untouched
2.) ignore the parts after + but before @
3.) remove all dots
the return value is how many unique emails are there after processing.

below is the pseudo for the easy version:

pseudo:
emailsSet = new Set()

for each email in emails
parts = email.indexOf("@")
local = parts[0]

noPlus = ignorePlus(local)
noPlusNoDot = removeDots(local)

returnEmail = noPlusNoDot + parts[1]

emailsSet.add(returnEmail)
return eamilsSet.length();
Enter fullscreen mode Exit fullscreen mode

If you are aiming for an entry-level job. This is something you should be able to implement in full code without much problem.

If you are more advanced than entry, think of an answer without using javascript built-in functions but the substring().

the thinking is like this:
Q: is there any complex rule that need to refer to previous characters?
A: no, okay then maybe all we need is a loop
Q: how do we loop through?
A: you provide conditions for add or ignore character
Q: is that all there it is?
A: well, you have to be careful about the + and @ conditions. You ignore until you hit the @ after hitting +.
Q: how do you order the conditions, does it even matter?
A: it matters, because once you hit @, you just append everything, so that's one good reason condition ordering matter.

Additionally, once you hit a +, you do not add to the string, so adding to the string must be the final default condition.

With that in mind you should be able to come up with a code like this:

var numUniqueEmails = function(emails) {
    const emailsSet = new Set();
    for (let email of emails) {
        let finalString = ''
        let facedPlus = false;

        for (let i=0; i < email.length; i++) {
            const current = email[i];
            if(current === '@') {
                finalString += email.substring(i)
                break;
            } else if (current === ".") {
                continue;
            } else if (current === "+") {
                facedPlus = true;
                continue;
            } else if (facedPlus) {
                continue;
            } else {
                finalString += current;
            }
        }

        emailsSet.add(finalString);
    }

    return emailsSet.size

}
Enter fullscreen mode Exit fullscreen mode

Please let me know if I missed something that's unclear, thank you!

Please hit me with a follow if you’d like to see more content, I’ll update irregular but somewhat frequently.

If you’d like a 1-1 conversation with me going over your resume or your career trajectory in general, please reach out to me at https://www.fiverr.com/kevtseng

Socials:
www.twitter.com/KevinTseng1991
https://www.linkedin.com/in/kevintseng1991/

Top comments (0)