Remove Consecutive Characters | InterviewBit | Solution

Note: If you want a detailed explanation, here you go: Remove Consecutive Characters | InterviewBit | Solution Explained.

class Solution:
    def solve(self, s, k):
        a = ["."]

        for c in s:
            if a[-1][0] == c:
                a[-1] += c
            else:
                a.append(str(c))
        
        a = a[1:]
        ns = ""
        for temp in a:
            final_len = len(temp)%k
            ns += temp[0]*final_len
        
        return ns

    Leave a Reply

    Your email address will not be published. Required fields are marked *