📜  计算给定数组中存在的不同电子邮件

📅  最后修改于: 2021-05-17 21:01:31             🧑  作者: Mango

给定一个由N个字符串组成的数组arr [] ,其中每个字符串代表一个由英文字母“。”,“ +”“ @”组成的电子邮件地址,任务是根据以下规则:

  • 电子邮件地址可以分为两个子字符串,即‘@’的前缀和后缀,分别是本地名称和域名。
  • “。”本地名称字符串中的字符将被忽略。
  • 在本地名称中,’ + ‘之后的每个字符都将被忽略。

例子:

方法:可以通过按照给定规则填充每个电子邮件并将其存储在HashSet中并打印获得的HashSet的大小来解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个HashSet,例如S ,以在根据给定规则进行填充之后存储所有不同的字符串。
  • 遍历给定数组arr []并执行以下步骤:
    • 找到“ @”的位置并将其存储在变量中,例如pos2
    • 删除所有的“。”使用delete()函数的pos2之前的字符。
    • 更新‘@’的位置,即pos2 = find(’@’)并找到‘+’的位置,并将其存储在变量pos1中,S.find(’+’)
    • 现在,擦除pos1之后和pos2之前的所有字符。
    • 将所有更新的字符串插入HashSet S中
  • 完成上述步骤后,打印HashSet S的大小作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count all the distinct
// emails after preprocessing according
// to the given rules
int distinctEmails(vector& emails)
{
    // Traverse the given array of
    // strings arr[]
    for (auto& x : emails) {
 
        // Stores the position of '@'
        // in the string
        auto pos2 = x.find('@');
 
        // If pos2 < x.size()
        if (pos2 < x.size())
 
            // Erases all the ocurrences
            // of '.' before pos2
            x.erase(
                remove(x.begin(),
                    x.begin() + pos2, '.'),
                x.begin() + pos2);
 
        // Stores the position of the
        // first '+'
        auto pos1 = x.find('+');
 
        // Update the position pos2
        pos2 = x.find('@');
 
        // If '+' exists then erase
        // charcters after '+' and
        // before '@'
        if (pos1 < x.size()
            and pos2 < x.size()) {
            x.erase(pos1, pos2 - pos1);
        }
    }
 
    // Insert all the updated strings
    // inside the set
    unordered_set ans(
        emails.begin(),
        emails.end());
 
    // Return the size of set ans
    return ans.size();
}
 
// Driver Code
int main()
{
    vector arr
        = { "raghav.agg@geeksforgeeks.com",
            "raghavagg@geeksforgeeks.com" };
 
    // Function Call
    cout << distinctEmails(arr);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Function to count all the distinct
# emails after preprocessing according
# to the given rules
def distinctEmails(emails):
   
  ans = set([])
 
  # Traverse the given array of
  # strings arr[]
  for x in emails:
 
    # Stores the position of '@'
    # in the string
    pos2 = x.find('@')
 
    # If pos2 < x.size()
    if (pos2 < len(x)):
 
      # Erases all the ocurrences
      # of '.' before pos2
      p = x[:pos2]
      p = p.replace(".", "")
      x = p + x[pos2:]
 
      # Stores the position of the
      # first '+'
      pos1 = x.find('+')
 
      # Update the position pos2
      pos2 = x.find('@')
 
      # If '+' exists then erase
      # charcters after '+' and
      # before '@'
      if (pos1 > 0 and pos1 < len(x) and
          pos2 < len(x)):
        x = x[:pos1] + x[pos2:]
 
      # Insert all the updated strings
      # inside the set
      ans.add(x)
 
  # Return the size of set ans
  return len(ans)
 
# Driver Code
if __name__ == "__main__":
 
    arr = ["raghav.agg@geeksforgeeks.com",
           "raghavagg@geeksforgeeks.com"]
 
    # Function Call
    print(distinctEmails(arr))
 
# This code is contributed by ukasp


输出:
1

时间复杂度: O(N 2 )
辅助空间: O(N)