Given 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@gmail.com
Explanation:
Replacing one “at” with ‘@’ and one “dot” with ‘.’ modifies S to geeksforgeeks@gmail.comInput: 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 emailstring 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 codeint main(){ // To display the result string email = "geeksforgeeksatgmaildotcom"; cout << (minEmail(email));}// Thi code is contributed by chitranayal. |
Java
// Java program for the above approachclass 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 emaildef 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 Codeif __name__ == '__main__': email = 'geeksforgeeksatgmaildotcom' # To display the result print(minEmail(email)) |
C#
// C# program for the above approachusing 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
<script>// Javascript 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 function minEmail(email) { // Stores string by replacing at // with @ and dot with '.'# such // that the string is valid email let ans = ""; let len = email.length; // append first character ans += email[0]; // Stores index let i = 1; // Check if at(@) already included // or not let 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 // To display the result let email="geeksforgeeksatgmaildotcom"; document.write(minEmail(email)); // This code is contributed by rag2127</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


