📌  相关文章
📜  通过替换指定的子字符串将字符串减少为最小长度的有效电子邮件地址

📅  最后修改于: 2021-10-26 02:36:31             🧑  作者: Mango

给定表示长度为N的电子邮件地址的字符串S ,任务是通过将“点”替换为‘.’来找到该字符串的最小可能长度和带有“@”的“at” ,这样字符串代表一个有效的电子邮件地址。

例子:

方法:这个想法是用“.”替换所有“点”子串和一个“在”与子串“@”,除了从起始或从字符串的结尾。请按照以下步骤解决问题:

  • 在索引[1, N – 2] 上遍历字符串。
  • 如果S[i, i + 2]“dot” ,则将其替换为“.” .如果S[i, i + 1]“at” ,则将其替换为“@”
  • 打印更新的字符串作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum length by
// replacing at with @ and dot with '.'
// such that the string is valid email
string minEmail(string email)
{
 
    // Stores string by replacing at
    // with @ and dot with '.'# such
    // that the string is valid email
    string ans = "";
    int len = email.length();
 
    // append first character
    ans += email[0];
 
    // Stores index
    int i = 1;
 
    // Check if at(@) already included
    // or not
    bool notAt = true;
 
    // Iterate over characters of the string
    while (i < len) {
 
        // at can be replaced at most once
        if (i < len - 3 && notAt && email[i] == 'a'
            && email[i + 1] == 't') {
 
            // Update ans
            ans += '@';
 
            // Update i
            i += 1;
 
            // Update notAt
            notAt = false;
        }
 
        // If current substring found dot
        else if (i < len - 4 && email[i] == 'd'
                 && email[i + 1] == 'o'
                 && email[i + 2] == 't') {
 
            // Update ans
            ans += '.';
 
            // Update i
            i += 2;
        }
        else {
 
            // Update ans
            ans += email[i];
        }
 
        // Update i
        i += 1;
    }
    return ans;
}
 
// Driver code
int main()
{
 
    // To display the result
    string email = "geeksforgeeksatgmaildotcom";
    cout << (minEmail(email));
}
 
// Thi code is contributed by chitranayal.


Java
// Java program for the above approach
class GFG {
 
  // Function to find the minimum length by
  // replacing at with @ and dot with '.'
  // such that the string is valid email
  static String minEmail(String email)
  {
 
    // Stores string by replacing at
    // with @ and dot with '.'# such
    // that the string is valid email
    String ans = new String("");
    int len = email.length();
 
    // append first character
    ans += email.charAt(0);
 
    // Stores index
    int i = 1;
 
    // Check if at(@) already included
    // or not
    boolean notAt = true;
 
    // Iterate over characters of the string
    while(i < len){
 
      // at can be replaced at most once
      if (i < len-3 && notAt
          && email.charAt(i) == 'a' && email.charAt(i+1) == 't')
      {
 
        // Update ans     
        ans += '@';
 
        // Update i
        i += 1;
 
        // Update notAt
        notAt = false;
      }
 
      // If current substring found dot
      else if( i < len-4 && email.charAt(i) == 'd'
              && email.charAt(i+1) == 'o' && email.charAt(i+2) == 't')
      {
 
        // Update ans
        ans += '.'; 
 
        // Update i
        i += 2;
      }
      else
      {
 
        // Update ans
        ans += email.charAt(i);
      }
 
      // Update i   
      i += 1;
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // To display the result
    String email = new String("geeksforgeeksatgmaildotcom");
    System.out.println(minEmail(email));
  }
}
 
// This code is contributed by rohitsingh07052.


Python3
# python program for the above approach
 
# Function to find the minimum length by
# replacing at with @ and dot with '.'
# such that the string is valid email
def minEmail(email):
 
    # Stores string by replacing at
    # with @ and dot with '.'# such
    # that the string is valid email
    ans = ''
 
    # append first character
    ans += email[0]
 
    # Stores index
    i = 1
 
    # Check if at(@) already included
    # or not
    notAt = True
 
    # Iterate over characters of the string
    while i < len(email):
 
        # at can be replaced at most once
        if (i < len(email)-3 and notAt
            and email[i:i + 2] == 'at'):
   
            # Update ans     
            ans += '@'
 
            # Update i
            i += 1
             
            # Update notAt
            notAt = False
 
        # If current substring found dot
        elif i < len(email)-4 and email[i:i + 3] == 'dot':
 
            # Update ans
            ans += '.'          
             
            # Update i
            i += 2
        else:
 
            # Update ans
            ans += email[i]
             
        # Update i   
        i += 1
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    email = 'geeksforgeeksatgmaildotcom'
 
    # To display the result
    print(minEmail(email))


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the minimum length by
  // replacing at with @ and dot with '.'
  // such that the string is valid email
  static String minEmail(String email)
  {
 
    // Stores string by replacing at
    // with @ and dot with '.'# such
    // that the string is valid email
    String ans = "";
    int len = email.Length;
 
    // append first character
    ans += email[0];
 
    // Stores index
    int i = 1;
 
    // Check if at(@) already included
    // or not
    bool notAt = true;
 
    // Iterate over characters of the string
    while(i < len)
    {
 
      // at can be replaced at most once
      if (i < len-3 && notAt
          && email[i] == 'a' && email[i + 1] == 't')
      {
 
        // Update ans     
        ans += '@';
 
        // Update i
        i += 1;
 
        // Update notAt
        notAt = false;
      }
 
      // If current substring found dot
      else if( i < len-4 && email[i] == 'd'
              && email[i+1] == 'o' && email[i+2] == 't')
      {
 
        // Update ans
        ans += '.'; 
 
        // Update i
        i += 2;
      }
      else
      {
 
        // Update ans
        ans += email[i];
      }
 
      // Update i   
      i += 1;
    }
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    // To display the result
    String email = "geeksforgeeksatgmaildotcom";
    Console.WriteLine(minEmail(email));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
geeksforgeeks@gmail.com

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程