This Python program is designed to solve a problem of finding all occurrences of a substring within a string. The user inputs a string and a substring, and the program uses regular expressions to find all matches of the substring within the string. The start and end indices of each match are printed out. If no match is found, the program prints '(-1, -1)'.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by iterating over the input string and using the 're.match' function from Python's 're' module to find matches of the substring starting at each index. If a match is found, the start and end indices are calculated and printed. The 'found_flag' variable is used to track whether any matches have been found, and if no matches are found during the iteration, '(-1, -1)' is printed after the loop.
import re
s = input().strip()
k = input().strip()
s_len = len(s)
found_flag = False
for i in range(s_len):
match_result = re.match(k, s[i:])
if match_result:
start_index = i + match_result.start()
end_index = i + match_result.end() - 1
print((start_index, end_index))
found_flag = True
if found_flag == False:
print("(-1, -1)")
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.