Easily understandeable python O(n) solution with comments


#1

class Solution:
# @param A : string
# @param B : integer
# @return a strings
def solve(self, A, B):
a=len(A)
i=0
d="" #d is used here for the remaining characters
c=A # cis used here for updating the sliced material
while i<len(A):
if(i==len(A)-B and A[i:i+B]==A[i]*B): #last possible case such that if B=2 and A=bcdaa,
c=A[i+B:]
i+=B
elif(i>len(A)-B): #consider aabcd
d+=A[i]
i+=1
elif(A[i:i+B]==A[i]*B): #inbetween
c=A[i+B:]
i+=B
else: #inbetween single characters
d+=A[i]
i+=1
return d