- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reduce a string to a valid email address of minimum length by replacing specified substrings
In this problem, we have given the email string containing the ‘dot’ and ‘at’ words. We need to replace them with ‘.’ And ‘@’ characters.
Note – The valid email address should contain the ‘@’ character only once. It should contain any prefixes before the ‘@’ character and the domain name after that. Also, a valid email can contain multiple ‘.’ Characters. Furthermore, the ‘@’ and ‘.’ Characters should not be at the start or end of the email address.
Problem statement – We have given a string str containing the email address, and the length of the string is equal to N. We need to reduce the string by replacing the ‘at’ word with the ‘@’ character and the ‘dot’ string with the ‘.’ character.
Sample examples
Input – str= "contactattutorialspointdotcom"
Output – contact@tutorialspoint.com
Explanation – We have replaced the ‘at’ and dot with ‘@’ and ‘.’ Characters respectively.
Input – str = “atatgmaildotcom”
Output – at@gmail.com
Explanation – Email can contain only one ‘@’ and can’t contain it at start, so the output is like the above
Approach 1
In this approach, we will check if the email contains the substring ‘at’ or ‘dot’ from the current character. We can replace it with the ‘@’ and ‘.’ Character.
Algorithm
Define the ‘len’ variable and store the length of the variable.
Define the ‘minStr’ variable and initialize it with the first character of the original string
Define ‘I’ variable and initialize with 1 to use with loop. Also, define the ‘isAtIncluded’ variable and initialize with false to keep track with the ‘@’ character is included once in the string or not.
Start iterating the string using the loop.
If I < len – 3, the value of isAtIncluded is false, and the substring of length 2 is equal to ‘at’, append the ‘@’ to the ‘minStr’ string. Also, increase the ‘I’ by one
else, if I < len – 4, and the substring of length 3 is equal to the ‘dot’, append the ‘.’ Character to the given string. Also, increase the value of the I by 2.
Else append the current character to the minStr string
return the minstr 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) as we traverse the string.
Space complexity – O(N), as we store the minified string.
In the above code, we always append the first character to the minstr string. So, it will never add ‘@’ or ‘.’ Character at the start. Also, users can use the replace() method to replace the ‘dot’ with the ‘.’ and ‘at’ with the ‘@’ character, but programmers need to make sure that it will add only a single ‘@’ character to the string.