Internet Address

Easy
0/40
Average time to solve is 15m
profile
Contributed by
8 upvotes
Asked in companies
MAQ SoftwareSpringworks

Problem statement

We know that the address of the Internet resource has format:

<protocol>://<domain>.ru[/<context>]
where,

1. <protocol> can either be equal to "http" (without the quotes) or "ftp" (without the quotes)
2. <domain> is a non-empty string, consisting of lowercase English letters,  
3. The /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters. 

If string isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).

For example,

'http://sun.ru/sunm', and 'ftp://guj.ru' are the valid address.

Given a string S, which contains all lower case characters without any punctuation marks (":", "/", "."), you have to find out and print the internet resource address.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains one string ‘S’.
Output Format:
For each case, we need to print a string representing the internet address.

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= |S| <= 50

Time limit: 1 sec.
Sample Input 1:
2
httpsunrusunm
ftphttpruxyz
Sample Output 1:
http://sun.ru/sunm
ftp://http.ru/xyz
Explanation of Sample Input 1:
Test case 1:
For the first test case of sample output 1, the string contains “http” as the protocol, “sun” as the domain, and “sunm” as the context. So we put up all the three parts in their proper positions.

Test case 2:   
For the second test case of sample output 1, the string contains “ftp” as the protocol, “http” as the domain, and “xyz” as the context. So we put up all the three parts in their proper positions.
Sample Input 2:
1
ftpftpftpru
Sample Output 2:
ftp://ftpftp.ru
Explanation of Sample Input 2:
Test case 1:
For the first test case of sample output 2, the string contains “ftp” as the protocol, “ftpftp” as the domain. Note that we don’t have a context here. So after putting the domain and “.ru”, we don’t need to add the reverse slash “/” here.
Hint

Use the concept of the substring.

Approaches (1)
String Manupilation

Here we can use the property of substring and divide the string into 3 parts. A substring is a contiguous sequence of characters in a string. So we can take a substring of the first 3 characters of the string and a substring of the first 4 characters of the string and check whether that is equal to “ftp” or “http” respectively. Then we can cut the string from that point and start to find the first occurrence of “ru” in the string. Once we get the occurrence, the string before the occurrence of “ru” becomes our domain, and the string remaining after “ru” is our context. We can put all the three parts in their position provided context is not an empty string. If the context is an empty string, then we don’t need to add a reverse slash after “ru”.

 

Algorithm:

 

  • Declare 3 empty strings as protocol, domain, and context.
  • Declare a variable to store the length of the input string.
  • Check whether the substring of the first 3 characters is equal to “ftp” or not
    • If yes, assign “ftp” to the protocol and cut the string from that point.
    • Otherwise, assign “http” to the protocol and cut the string after the fourth position.
  • Update the length of the new string.
  • Run a for loop from i=0 to “length of the string -1” to find the occurrence of first “ru” in the string
    • Take a substring of 2 characters starting from index ‘i’ and check whether that is equal to “ru” or not.
      • If equal, find the substring of the string till the position ‘i’ and put that in the domain and the substring after “ru” to the context.
      • Break the loop
  • Declare a final string to put protocol, domain, and context together.
  • If our context is not an empty string
    • Put them in the format and print
    • Otherwise, put protocol and domain and don’t add a reverse slash at the end.
  • Return the final string.

 


 

Time Complexity

O(N) where N is the length of the input string. 

 

As we iterate over the string to find out the first occurrence of “ru”, we need N-1 iterations. So our time complexity is O(N).

Space Complexity

O(1)

 

We only need constant space. So it’s O(1).

Code Solution
(100% EXP penalty)
Internet Address
Full screen
Console