If you add periods (
If you add a plus (
'.'
) 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 emailsstatic int numUniqueEmails(String[] emails) {HashSet<String> st = new HashSet<String>();// iterate till the length of emailsfor (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 positionif (S.charAt(j) == '.')j++;// if + then breakelse if (S.charAt(j) == '+') {break;}// if not + and not . then// add this into the string and// move forwardelse {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 stackreturn st.size();}}
C++
#include <bits/stdc++.h>using namespace std;//function to count the number of//unique emailsint numUniqueEmails(vector<string>& emails){set<string> st;//iterate till the length of emailsfor(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 positionif(S[j]=='.')j++;//if + then breakelse if(S[j]=='+'){break;}//if not + and not . then//add this into the string and//move forwardelse{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 stackreturn st.size();}int main(){vector<string> emails={"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