Skip to content
Related Articles

Related Articles

Reduce a string to a valid email address of minimum length by replacing specified substrings
  • Last Updated : 25 Feb, 2021

Given a string S representing an email address of length N, the task is to find the minimum possible length of the string by replacing “dot” with ‘.’ and “at” with ‘@’ such that the string represents a valid email address.

An email address can have only one ‘@’ and can have possibly zero or multiple dots (‘.’) such that it can not have ‘@’ or ‘.’ at the start and at the end of the email address.

Examples: 
 

Input: S = “geeksforgeeksatgmaildotcom”
Output: geeksforgeeks@gamil.com
Explanation: 
Replacing one “at” with ‘@’ and one “dot” with ‘.’ modifies S to geeksforgeeks@gamil.com

Input: S = “atatdotdotdot”
Output: at@..dot
Explanation: 
Multiple replacements are possible, but we cannot replace “at” from the start or “dot” from the end of the email address as it won’t form a valid email address.



 

 

 

Approach: The idea is to replace all “dot” substrings with “.” and one “at” substring with “@” except from the start or from the end of the string. Follow the below steps to solve the problem:

 

  • Traverse the string over indices [1, N – 2].
  • If S[i, i + 2] is “dot”, replace it with “.”. If S[i, i + 1] is “at”, replace it with “@”.
  • Print the updated string as the answer.

 

Below is the implementation of the above approach:

 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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
Output: 
geeksforgeeks@gmail.com

 

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :