<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:
        #k_dup removal 
        char_count = [] #contains the (character, count_to_char)
        
        for char in s: 
            #Just use a stack to grab counts.
            cur_count = 1
            if char_count and char_count[-1][0] == char: 
                prev_char, prev_count = char_count.pop()
                cur_count = prev_count + 1 
                

            
            if cur_count != k: 
                char_count.append((char, cur_count))
                
        result = ''
        #because we've emulated the deletion of characters 
        for char, repeat in char_count:
            result += char * repeat
            
        return result
# class Solution:
#     def removeDuplicates(self, s: str, k: int) -> str:
#         #string s and a k duplicate removal 
#         #Where choosing k adj and equal letters from s and removing them
#         #That causes the lhs and the rhs of the substr to concatenate 
#         #We make k duplicate removals until there isn't anymore 
#         #Return the final string s.t all dup removal done 
#         #Answer is unique 
#         # 
# #         "dtpdtaaaaaaaaappppppppppppppppppppaaaaaaaaaaxxxxxxxxxxxxxxsssssssssjjjjjjjjjjjjjjjjjjjjxxxxxxxxxxxxxxxxxxxxsssssssjjjjjjjjjjjjjjjjjjjjssssxxxxxxatdwvvpctpggggggggggggggggggggajagglaaaaaaaaaaaaaaaaaaaa"
# # 20
#         # char_count = collections.defaultdict(int)
#         #Use a stack? 
#         stack_str = [] 
#         char_count = 0 
#         #adjacent
#         for pos, char in enumerate(s):
#             if stack_str and stack_str[-1][0] == char: 
#                 char_count = stack_str.pop()[1]
#                 char_count += 1
#             else:
#                 char_count = 1 
                
#             stack_str.append((char, char_count))
#             if stack_str[-1][1] == k:
#                 stack_str.pop()

            
#         res = ""       
#         for char, count in stack_str:
#             res += char * count 
#         return res
            
        
        

# class Solution:
#     def removeDuplicates(self, s: str, k: int) -> str:
#           #String S and a int k 
#             #Just grab the count of the chars then each time
            
#             stack = []
#             #(e,3)
            
#             for char in s: 
#                 count = 1
#                 while stack and stack[-1][0] == char: 
#                     same_char, prev_count = stack.pop()
#                     count = prev_count + 1
                    
                    
#                 if count != k:
#                     stack.append((char, count))
                    
#             res = ''
#             for char, count in stack:
#                 res += char * count
                
#             return res
63