The task is following:
Find and calculate the sum of all the possible substrings within a given input master string s, out of which you can form the word "tira" (Course abbr.) by further removing unnecessary letters.
EXAMPLE, return value 11 with input "tixratiyra": 1: tixratiyra, 2: tixratiyra, 3: tixratiyra, 4: tixratiyra, 5: tixratiyra, 6: tixratiyra, 7: tixratiyra, 8: tixratiyra, 9: tixratiyra, 10: tixratiyra, 11: tixratiyra.
I'm able to create a working piece of code but it won't run fast enough, it should be able to perform this task in O(n) time with a maximum input length of 10^5.
My code, working painfully slow:
def count(s):
start = timeit.default_timer()
c = "bcdefghjklmnopqsuvwxyz"
last_char = ""
indexes = set()
unique_indexes = []
last_A = s.rfind("a")
last_R = s.rfind("r", 0, last_A)
last_I = s.rfind("i", 0, last_R)
last_T = s.rfind("t", 0, last_I)
unique_tiras = ""
for i in range(len(s)):
char = s[i]
if char not in c:
if char == "t":
if i <= last_T:
indexes.add("t")
last_char = "t"
unique_tiras += str(i) + "t"
elif char == "i" and last_char != "i":
if i <= last_I and "t" in indexes:
indexes.add("i")
last_char = "i"
unique_tiras = unique_tiras.replace("t", "i")
elif char == "r" and last_char != "r":
if i <= last_R and ("t" and "i") in indexes:
indexes.add("r")
last_char = "r"
unique_tiras = unique_tiras.replace("i", "r")
elif char == "a":
if i <= last_A and ("t" and "i" and "r") in indexes:
last_char = "a"
unique_tiras = unique_tiras.replace("r", f"-{i};")
pairs = unique_tiras.split(";")
unique_tiras = ""
for elements in pairs:
if "-" in elements:
Tindex = elements.split("-")
unique_indexes.append((int(Tindex[0]), int(Tindex[1])))
unique_tiras += Tindex[0] + "r"
else:
unique_tiras += elements
if len(unique_indexes) < 1:
print("found no tira substrings with input '", s[0:20], "'")
print("indexing took a total of", timeit.default_timer()-start, "s")
return 0
print("found a total of", len(unique_indexes), "tira substrings with input '", s[0:20], "'") #, which are the following:
#print(unique_indexes)
print("indexing took a total of", timeit.default_timer()-start, "s")
start = timeit.default_timer()
unique_substrings = set()
for tiras in unique_indexes:
begin = 0
while begin <= tiras[0]:
end = tiras[1]
while end <= len(s) - 1:
unique_substrings.add((begin, end))
end += 1
begin += 1
print("calculating suitable substrings took a total of", timeit.default_timer()-start, "s")
print("found suitable substrings a total of")
return len(unique_substrings)
if __name__ == "__main__":
print(count("ritari")) # 0
print(count("taikurinhattu")) # 4
print(count("ttiirraa")) # 4
print(count("tixratiyra")) # 11
print(count("aotiatraorirratap")) # 42
'tira', and those letters do not appear anywhere else in the master string, how many substring are there?