Count distinct emails present in a given array
Last Updated :
14 Feb, 2023
Given an array arr[] consisting of N strings where each string represents an email address consisting of English alphabets, ‘.’, ‘+’ and ‘@’, the task is to count the number of distinct emails present in the array according to the following rules:
- An email address can be split into two substrings, the prefix and suffix of ‘@’, which are the local name and domain name respectively.
- The ‘.’ character in the string in the local name is ignored.
- In the local name, every character after ‘+‘ is ignored.
Examples:
Input: arr[] = {“raghav.agg@geeksforgeeks.com”, “raghavagg@geeksforgeeks.com”}
Output: 1
Explanation: Removing all the ‘.’s before ‘@’ modifies the strings to {“raghavagg@geeksforgeeks.com”, “raghavagg@geeksforgeeks.com”}. Therefore, the total number of distinct emails present in the string are 1.
Input: arr[] = {“avruty+dhir+gfg@geeksforgeeks.com”, “avruty+gfg@geeksforgeeks.com”, “av.ruty@geeksforgeeks.com”}
Output: 1
Approach: The given problem can be solved by storing each email in a HashSet after populating it according to the given rule and print the size of the HashSet obtained. Follow the steps below to solve the problem:
- Initialize a HashSet, say S, to store all the distinct strings after populating according to the given rules.
- Traverse the given array arr[] and perform the following steps:
- Find the position of ‘@’ and store it in a variable, say pos2.
- Delete all the ‘.’ characters before pos2 using erase() function.
- Update the position of ‘@’ i.e., pos2 = find(‘@’) and find the position of ‘+’ and store it in a variable say pos1 as S.find(‘+’).
- Now, erase all the characters after pos1 and before pos2.
- Insert all the updated strings in a HashSet S.
- After completing the above steps, print the size of HashSet S as the result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int distinctEmails(vector<string>& emails)
{
for ( auto & x : emails) {
auto pos2 = x.find( '@' );
if (pos2 < x.size())
x.erase(
remove (x.begin(),
x.begin() + pos2, '.' ),
x.begin() + pos2);
auto pos1 = x.find( '+' );
pos2 = x.find( '@' );
if (pos1 < x.size()
and pos2 < x.size()) {
x.erase(pos1, pos2 - pos1);
}
}
unordered_set<string> ans(
emails.begin(),
emails.end());
return ans.size();
}
int main()
{
vector<string> arr
= { "raghav.agg@geeksforgeeks.com" ,
"raghavagg@geeksforgeeks.com" };
cout << distinctEmails(arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int distinctEmails(String emails[])
{
HashSet<String> ans = new HashSet<>();
for (String x : emails) {
int pos2 = x.indexOf( '@' );
if (pos2 < x.length()) {
String p = x.substring( 0 , pos2);
p = p.replace( "." , "" );
x = p + x.substring(pos2);
int pos1 = x.indexOf( '+' );
pos2 = x.indexOf( '@' );
if (pos1 > 0 && pos1 < x.length()
&& pos2 < x.length())
x = x.substring( 0 , pos1)
+ x.substring(pos2);
ans.add(x);
}
}
return ans.size();
}
public static void main(String args[])
{
String arr[] = { "raghav.agg@geeksforgeeks.com" ,
"raghavagg@geeksforgeeks.com" };
System.out.println(distinctEmails(arr));
}
}
|
Python3
def distinctEmails(emails):
ans = set ([])
for x in emails:
pos2 = x.find( '@' )
if (pos2 < len (x)):
p = x[:pos2]
p = p.replace( "." , "")
x = p + x[pos2:]
pos1 = x.find( '+' )
pos2 = x.find( '@' )
if (pos1 > 0 and pos1 < len (x) and
pos2 < len (x)):
x = x[:pos1] + x[pos2:]
ans.add(x)
return len (ans)
if __name__ = = "__main__" :
arr = [ "raghav.agg@geeksforgeeks.com" ,
"raghavagg@geeksforgeeks.com" ]
print (distinctEmails(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int distinctEmails(String[] emails)
{
HashSet<String> ans = new HashSet<String>();
for ( int i = 0; i < emails.Length; i++) {
string x = emails[i];
int pos2 = x.IndexOf( '@' );
if (pos2 < x.Length) {
String p = x.Substring(0, pos2);
p = p.Replace( "." , "" );
x = p + x.Substring(pos2);
int pos1 = x.IndexOf( '+' );
pos2 = x.IndexOf( '@' );
if (pos1 > 0 && pos1 < x.Length
&& pos2 < x.Length)
x = x.Substring(0, pos1)
+ x.Substring(pos2);
ans.Add(x);
}
}
return ans.Count;
}
static public void Main()
{
String[] arr = { "raghav.agg@geeksforgeeks.com" ,
"raghavagg@geeksforgeeks.com" };
Console.WriteLine(distinctEmails(arr));
}
}
|
Javascript
function distinctEmails(emails) {
var ans = new Set([]);
for ( var x of emails) {
var pos2 = x.indexOf( '@' );
if (pos2 < x.length) {
let p = x.substring(0, pos2);
p = p.replace(/\./g, '' );
x = p + x.substring(pos2);
var pos1 = x.indexOf( '+' );
pos2 = x.indexOf( '@' );
if (pos1 > 0 && pos1 < x.length && pos2 < x.length) {
x = x.substring(0, pos1) + x.substring(pos2);
}
ans.add(x);
}
}
return ans.size;
}
const arr = [
'raghav.agg@geeksforgeeks.com' ,
'raghavagg@geeksforgeeks.com' ,
];
console.log(distinctEmails(arr));
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...