forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-compressed-string-iterator.py
More file actions
41 lines (31 loc) · 947 Bytes
/
design-compressed-string-iterator.py
File metadata and controls
41 lines (31 loc) · 947 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
32
33
34
35
36
37
38
39
40
41
# Time: O(1)
# Space: O(1)
import re
class StringIterator(object):
def __init__(self, compressedString):
"""
:type compressedString: str
"""
self.__result = re.findall(r"([a-zA-Z])(\d+)", compressedString)
self.__index, self.__num, self.__ch = 0, 0, ' '
def next(self):
"""
:rtype: str
"""
if not self.hasNext():
return ' '
if self.__num == 0:
self.__ch = self.__result[self.__index][0]
self.__num = int(self.__result[self.__index][1])
self.__index += 1
self.__num -= 1
return self.__ch
def hasNext(self):
"""
:rtype: bool
"""
return self.__index != len(self.__result) or self.__num != 0
# Your StringIterator object will be instantiated and called as such:
# obj = StringIterator(compressedString)
# param_1 = obj.next()
# param_2 = obj.hasNext()