<p>You are given a string <code>s</code> and an integer <code>k</code>, a <code>k</code> <strong>duplicate removal</strong> consists of choosing <code>k</code> adjacent and equal letters from <code>s</code> and removing them, causing the left and the right side of the deleted substring to concatenate together.</p>

<p>We repeatedly make <code>k</code> <strong>duplicate removals</strong> on <code>s</code> until we no longer can.</p>

<p>Return <em>the final string after all such duplicate removals have been made</em>. It is guaranteed that the answer is <strong>unique</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcd&quot;, k = 2
<strong>Output:</strong> &quot;abcd&quot;
<strong>Explanation: </strong>There&#39;s nothing to delete.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;deeedbbcccbdaa&quot;, k = 3
<strong>Output:</strong> &quot;aa&quot;
<strong>Explanation: 
</strong>First delete &quot;eee&quot; and &quot;ccc&quot;, get &quot;ddbbbdaa&quot;
Then delete &quot;bbb&quot;, get &quot;dddaa&quot;
Finally delete &quot;ddd&quot;, get &quot;aa&quot;</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;pbbcggttciiippooaais&quot;, k = 2
<strong>Output:</strong> &quot;ps&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= k &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> only contains lowercase English letters.</li>
</ul>

class Solution:
    def removeDuplicates(self, s: str, k: int) -> str:
        if k>len(s):
            return s

        c = []
        se = []
        cc = 0
        for i in range(len(s)):
            if i==0:
                cc +=1
            else:
                if s[i]==s[i-1]:
                    cc +=1
                else:
                    se.append(s[i-1])
                    c.append(cc)
                    cc = 1
        c.append(cc)
        se.append(s[-1])
        c = [cc%k for cc in c]
        i = [kk for kk in range(len(c)) if c[kk]!=0]
        c = [c[ii] for ii in i]
        se = [se[ii] for ii in i]
        # print(c)
        # print(se)
        assert(len(c)==len(se))

        i=0
        while i<len(se):
            
            if i>0 and se[i]==se[i-1]:
                c[i-1] += c[i]
                se.pop(i)
                c.pop(i)
                i -=1

            if c[i]>=k:
                c[i] -= c[i]//k*k
                if c[i]==0:
                    c.pop(i)
                    se.pop(i)
                else:
                    i +=1
            else:
                i +=1
        
        return "".join(c[kk]*se[kk] for kk in range(len(se)))
23700