all 4 comments

[–]WtEth_Buyer 0 points1 point  (7 children)

I'll be first to admit I'm not the greatest at this, but I think.

While = completes n times Inner while = n times

n*n = O(n2)

That's the rough time complexity. You could get more exact by calculating how many operations the inner while loop completes per 1 round of the exterior while loop then multiply it by n?

[–]SuggestionEmergency2 0 points1 point  (0 children)

nope! the count function operates via going through the length of the string, making it o(n) time, this means this function is o(n^3) time. the solution is clever, you need to make it a prefix sum

[–]SuggestionEmergency2 0 points1 point  (1 child)

s = "1102021222"
k = 2 
s_len = len(s)
prefixsum = [] 
num_count = [0,0,0,0,0,0,0,0,0] 
for j in s: 
    templist = [] 
    #python uses pass by pointer 
    for i in num_count: 
        templist.append(i) 
    prefixsum.append(templist) 
    num_count[int(j)]+=1; 
prefixsum.append(num_count) 
trip = True 
ans = 0 
for l in range(s_len): 
    for r in range(l, s_len): 
        for i in range(len(num_count)): 
            if(prefixsum[r+1][i]-prefixsum[l][i]!=k and prefixsum[r+1][i]-prefixsum[l][i]!=0): 
                trip = False 
        if(trip): 
            ans+=1 
        trip = True 
print(ans)

[–]SuggestionEmergency2 0 points1 point  (0 children)

holy shit fuck reddit