Unique Email Addresses

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, "alice.z@gmail.com" and "alicez@gmail.com" forward to the same email address.  (Note that this rule does not apply to domain names.)
If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example, m.y+name@email.com will be forwarded to my@email.com.  (Again, this rule does not apply to domain names.)

Example:

Input:   emails[]={"test.email+alex@gmail.com","test.e.mail+bob.cathy@gmail.com" ,"testemail+david@gma.il.com"}
Output: 2

Approach

Java


import java.util.HashSet;

public class UniqueEmailAddresses {
    public static void main(String[] args) {
        String emails[] = { "test.email+alex@gmail.com"
                "test.e.mail+bob.cathy@gmail.com",
                "testemail+david@gma.il.com" };
        System.out.println(numUniqueEmails(emails));
    }

    // function to count the number of
    // unique emails
    static int numUniqueEmails(String[] emails) {
        HashSet<Stringst = new HashSet<String>();

        // iterate till the length of emails
        for (int i = 0; i < emails.length; i++) {
            String str = "";
            int j = 0;
            String S = emails[i];

            // iterate till the size of current
            // email or we get '@'
            while (j < S.length() && S.charAt(j) != '@') {
                // if . then move to next position
                if (S.charAt(j) == '.')
                    j++;
                // if + then break
                else if (S.charAt(j) == '+') {
                    break;
                }
                // if not + and not . then
                // add this into the string and
                // move forward
                else {
                    str += S.charAt(j);
                    j++;
                }

            }

            // iterate we get '@'
            while (j < S.length() && S.charAt(j) != '@')
                j++;
            str += S.substring(j, S.length());
            st.add(str);
        }
        // return the size of stack
        return st.size();
    }
}

C++

#include <bits/stdc++.h>
using namespace std;


//function to count the number of
//unique emails
int numUniqueEmails(vector<string>& emails
{
        set<stringst;

        //iterate till the length of emails
        for(int i=0;i<emails.size();i++)
         {
           string str="";
           int j=0;
           string S=emails[i];

           //iterate till the size of current 
           //email or we get '@' 
            while(j<S.size()&&S[j]!='@')
             {
                 //if . then move to next position
               if(S[j]=='.')
                  j++;
             //if + then break
                else if(S[j]=='+')
                {
                    break;                    
                }
               //if not + and not . then 
               //add this into the string and
               //move forward 
                else
                {
                    str+=S[j];
                    j++;
                }
                
             }

        //iterate we get '@'
        while(j<S.size()&&S[j]!='@')
               j++;
         str+=S.substr(j);
            st.insert(str);
         }
        //return the size of stack
      return st.size();
}
int main()
{
    vector<stringemails={"test.email+alex@gmail.com","test.e.mail+bob.cathy@gmail.com"
,"testemail+david@gma.il.com"};
    cout<<numUniqueEmails(emails);
    return 0;
}


No comments:

Post a Comment