Find Unique ID and Domain Name of a Website from a string
Given a string S of size N consisting of unique ID and Domain Name of a unique website, the task is to find the ID and the Domain Name in the given string if the ID is of the form [char, char, char, char, char, digit, digit, digit, digit, char].
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.
Input: S = “We thank ABCDE1234F for visiting us and buying products item AMZrr@!k. For more offers, visit us at www.amazon.com”
ID = ABCDE1234F
Domain = amazon.com
Input: S = “Hi PQRST5678D, it was a pleasure to host you. See www.oyo.com, our official website for future stays”
ID = PQRST5678D
Domain = oyo.com
Approach: The simplest approach to solve the given problem is to split the string into words and find if the split string is ID or Domain. Follow the steps below to solve the problem:
- First split the words of the string separated by space and store them in a vector of string say words.
- Initialize two empty strings, say ID and Domain to store the resultant ID and Domain Name.
- Traverse the vector of string words and perform the following steps:
- Initialize a variable, say flag as false to store if the current string satisfies the format of ID or not.
- If the length of the current string is 10 and if the first 5 characters and the last character is non-alphabets or any of the remaining characters of the string is non-numeric then mark the flag as true.
- If the value of the flag is false, then assign the current string to ID.
- If the first substring of the current string, say SS over the range [0, 2] is “www” and substring over the range [SS.length() – 3, SS.length() – 1] is “com” then assign the domain name i.e., substring over the range [3, SS.length() – 1] to Domain.
- After completing the above steps, print the value of ID and Domain as the result.
Below is the implementation of the above approach:
ID = ABCDE1234F Domain = amazon.com
Time Complexity: O(N)
Auxiliary Space: O(N)