Home  >  Article  >  Backend Development  >  Reduces a string to a valid minimum length email address, by replacing the specified substring

Reduces a string to a valid minimum length email address, by replacing the specified substring

PHPz
PHPzforward
2023-09-06 11:01:06571browse

Reduces a string to a valid minimum length email address, by replacing the specified substring

In this question, we are given an email string containing the words "dot" and "at". We need to replace them with "." and "@" characters.

NOTE - A valid email address should contain only one '@' character. It should contain the '@' character before any prefix and the domain name after it. Additionally, valid emails can contain multiple '.' characters. Additionally, the '@' and '.' characters should not be at the beginning or end of the email address.

Problem Statement Given a string str containing an email address, the length of the string is N. We need to shorten the string by replacing "at" with "@" character and "dot" with "." character in the string.

Example

Enter-str="contactattutorialspointdotcom"

Output – contact@tutorialspoint.com

Description - We replaced the "at" and dot with the "@" and "." characters respectively.

Input – str = “atatgmaildotcom”

Output– at@gmail.com

Note – An email can only contain one “@” and cannot have it at the beginning, so the output is as shown above

method one

In this method we will check if the email contains the substring "at" or "dot" of the current character. We can replace it with "@" and "." characters.

algorithm

  • Define variable 'len' and store the length of the variable.

  • Define the variable 'minStr' and initialize it to the first character of the original string

  • Define the 'I' variable and initialize it to 1 for use in the loop. Also, define the 'isAtIncluded' variable and initialize it to false to track whether the '@' character in the string has been included once.

  • Start using a loop to iterate the string.

  • If i

  • Otherwise, if I

  • Otherwise append the current character to the minStr string

  • Return the smallest string value.

Example

#include <iostream>
#include <iostream>
using namespace std;

// function to minimize the string by replacing at with @ and dot with '.'
string minifyEmail(string original){
   string minstr = "";
   int len = original.length();
   // append the first character to the final string
   minstr += original[0];
   // start index
   int i = 1;
   // Check wether at(@) already included or not
   bool isAtIncluded = false;
   // travere the string
   for (int i = 0; i < len; i++){
      // at can be replaced at most once
      if (i < len - 3 && !isAtIncluded && original.substr(i, 2) == "at"){
      // add '@' to minstr
      minstr += '@';
      // Update isAtIncluded
      isAtIncluded = true;
      i++;
   }
   // If current substring found dot
   else if (i < len - 4 && original.substr(i, 3) == "dot"){
      // add '.' to minstr
      minstr += '.';
      i += 2;
   } else {
      minstr += original[i];
      }
   }
   return minstr;
}
int main(){
   string original = "contactattutorialspointdotcom";
   cout << "The string after minifying in the proper original format is " << minifyEmail(original);
}

Output

The string after minifying in the proper original format is ccontact@tutorialspoint.com

Time complexity - O(N), since we iterate over the string.

Space complexity - O(N) because we store compressed strings.

In the above code, we always append the first character to the minstr string. Therefore, it never adds "@" or "." characters at the beginning. Additionally, users can use the replace() method to replace "dot" with "." and "at" with "@" characters, but programmers need to ensure that it only adds a single "@" character to the string.

The above is the detailed content of Reduces a string to a valid minimum length email address, by replacing the specified substring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete