The problem I am trying to solve:
Given a string, split the string into two substrings at every possible point. The rightmost substring is a suffix. The beginning of the string is the prefix. Determine the lengths of the common prefix between each prefix and the original string. Sum and return the lengths of the common prefixes. Return an array where each element 'i' is the sum for the string 'i'.
My Solution:
def commonPrefix(l):
size=len(l) #Finding number of strings
sumlist=[] #Initializing list that'll store sums
for i in range(size):
total=0 #Initializing total common prefixes for every string
string=l[i] #Storing each string in string
strsize=len(string) #Finding length of each string
for j in range(strsize):
suff=string[j:] #Calculating every suffix for a string
suffsize=len(suff) #Finding length of each suffix
common=0 #Size of common prefix for each length
for k in range(suffsize):
if(string[k]==suff[k]):
common=common+1
else:
break #If characters at equal positions are not equal, break
total=total+common #Update total for every suffix
sumlist.append(total) #Add each total in list
return sumlist #Return list
My Solution takes up a lot of time, I need help with optimizing it.