➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Remove All Adjacent Duplicates in String II

image

My first approach to solve this works but the issue was the order of the return value wasn’t the same in the original string which made it fail image

But here’s the solve script:

def removeDuplicates(s, k):
    ht = {}
    r = []

    for i in s:
        if i in ht:
            ht[i] += 1
        else:
            ht[i] = 1
        
    for key, value in ht.items():
        check  = value % k
        if check < k:
            r.append(key * check)

    return "".join(r)

s = "yfttttfbbbbnnnnffbgffffgbbbbgssssgthyyyy" 
k = 4
r = removeDuplicates(s, k)

print(r)

So I implemented another appraoch which made us of Stack Data Structure

Here’s my solve script: link image