Reduce a string to a valid email address of minimum length by replacing specified substrings
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.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++
#include <bits/stdc++.h>
using namespace std;
string minEmail(string email)
{
string ans = "" ;
int len = email.length();
ans += email[0];
int i = 1;
bool notAt = true ;
while (i < len) {
if (i < len - 3 && notAt && email[i] == 'a'
&& email[i + 1] == 't' ) {
ans += '@' ;
i += 1;
notAt = false ;
}
else if (i < len - 4 && email[i] == 'd'
&& email[i + 1] == 'o'
&& email[i + 2] == 't' ) {
ans += '.' ;
i += 2;
}
else {
ans += email[i];
}
i += 1;
}
return ans;
}
int main()
{
string email = "geeksforgeeksatgmaildotcom" ;
cout << (minEmail(email));
}
|
Java
class GFG {
static String minEmail(String email)
{
String ans = new String( "" );
int len = email.length();
ans += email.charAt( 0 );
int i = 1 ;
boolean notAt = true ;
while (i < len){
if (i < len- 3 && notAt
&& email.charAt(i) == 'a' && email.charAt(i+ 1 ) == 't' )
{
ans += '@' ;
i += 1 ;
notAt = false ;
}
else if ( i < len- 4 && email.charAt(i) == 'd'
&& email.charAt(i+ 1 ) == 'o' && email.charAt(i+ 2 ) == 't' )
{
ans += '.' ;
i += 2 ;
}
else
{
ans += email.charAt(i);
}
i += 1 ;
}
return ans;
}
public static void main (String[] args)
{
String email = new String( "geeksforgeeksatgmaildotcom" );
System.out.println(minEmail(email));
}
}
|
Python3
def minEmail(email):
ans = ''
ans + = email[ 0 ]
i = 1
notAt = True
while i < len (email):
if (i < len (email) - 3 and notAt
and email[i:i + 2 ] = = 'at' ):
ans + = '@'
i + = 1
notAt = False
elif i < len (email) - 4 and email[i:i + 3 ] = = 'dot' :
ans + = '.'
i + = 2
else :
ans + = email[i]
i + = 1
return ans
if __name__ = = '__main__' :
email = 'geeksforgeeksatgmaildotcom'
print (minEmail(email))
|
C#
using System;
public class GFG
{
static String minEmail(String email)
{
String ans = "" ;
int len = email.Length;
ans += email[0];
int i = 1;
bool notAt = true ;
while (i < len)
{
if (i < len-3 && notAt
&& email[i] == 'a' && email[i + 1] == 't' )
{
ans += '@' ;
i += 1;
notAt = false ;
}
else if ( i < len-4 && email[i] == 'd'
&& email[i+1] == 'o' && email[i+2] == 't' )
{
ans += '.' ;
i += 2;
}
else
{
ans += email[i];
}
i += 1;
}
return ans;
}
public static void Main(String[] args)
{
String email = "geeksforgeeksatgmaildotcom" ;
Console.WriteLine(minEmail(email));
}
}
|
Javascript
<script>
function minEmail(email)
{
let ans = "" ;
let len = email.length;
ans += email[0];
let i = 1;
let notAt = true ;
while (i < len){
if (i < len-3 && notAt
&& email[i] == 'a' && email[i+1] == 't' )
{
ans += '@' ;
i += 1;
notAt = false ;
}
else if ( i < len-4 && email[i] == 'd'
&& email[i+1] == 'o' && email[i+2] == 't' )
{
ans += '.' ;
i += 2;
}
else
{
ans += email[i];
}
i += 1;
}
return ans;
}
let email= "geeksforgeeksatgmaildotcom" ;
document.write(minEmail(email));
</script>
|
Output:
geeksforgeeks@gmail.com
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
16 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...