-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathanother-zuma.py
More file actions
31 lines (30 loc) · 853 Bytes
/
another-zuma.py
File metadata and controls
31 lines (30 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
"""
@param s: the pearl sequences.
@param k: every k same pearls together will be removed.
@return: return the pearls after the game.
"""
def zumaGaming(self, s, k):
# write your code here.
sarr = []
prev = None
cnt = 0
for c in s:
if c != prev:
if cnt > 0:
sarr.append(prev * cnt)
prev = c
cnt = 1
else:
cnt += 1
if cnt == k:
prev = None
cnt = 0
if prev and (cnt > 0):
sarr.append(prev * cnt)
ns = ''.join(sarr)
if ns != s:
return self.zumaGaming(ns, k)
else:
return ns
# medium: https://www.lintcode.com/problem/another-zuma/