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
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.
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.
1 <= T <= 5
1 <= |S| <= 50
Time limit: 1 sec.
2
httpsunrusunm
ftphttpruxyz
http://sun.ru/sunm
ftp://http.ru/xyz
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.
1
ftpftpftpru
ftp://ftpftp.ru
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.
Use the concept of the substring.
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”.
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).
O(1)
We only need constant space. So it’s O(1).