Created
September 29, 2017 23:20
-
-
Save andreytwostep/821fa8fe334948efd89c5f4fe26da0da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# https://leetcode.com/problems/decode-string/description/ | |
# | |
class Solution(object): | |
def decodeString(self, s): | |
""" | |
:type s: str | |
:rtype: str | |
""" | |
nums = [] # 3 | |
letters = [] # | |
result = None | |
prev = '' | |
for i in s: | |
if i.isdigit(): | |
val = nums.pop() if prev.isdigit() and len(nums) else '' | |
nums.append(val + i) | |
# print 'num: ' + i | |
elif i.isalpha(): | |
val = letters.pop() if prev.isalpha() and len(letters) else '' | |
letters.append(val + i) | |
elif i == ']': | |
print nums | |
print letters | |
if result == None: | |
result = int(nums.pop()) * letters.pop() | |
else: | |
result | |
# result += int(nums.pop()) * letters.pop() | |
print result | |
prev = i | |
return result | |
str = Solution(); | |
print str.decodeString('3[a2[c]]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment