给定表示长度为N的电子邮件地址的字符串S ,任务是通过将“点”替换为‘.’来找到该字符串的最小可能长度。和带有“@”的“at” ,这样字符串代表一个有效的电子邮件地址。
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.
例子:
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.
方法:这个想法是用“.”替换所有“点”子串。和一个“在”与子串“@”,除了从起始或从字符串的结尾。请按照以下步骤解决问题:
- 在索引[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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live