1. What I learned
a. method chr()
‘chr(NUMBER)’ returns the character of ASCII code(NUMBER).
a. method ord()
‘ord(CHARACTER)’ returns the ASCII code of CHARACTER.
2. How I sloved
I made a stack and pushed all the elements into it. When the element was ‘#’, I didn’t push it, popped an element twice. The Popped elements are combined and pushed back in the correct order. Then I changed all the elements into characters using ‘chr()’ method.
3. Code
class Solution:
def freqAlphabets(self, s: str) -> str:
stack = []
output = ''
s = list(s)
for e in s:
if e == '#':
a,b = stack.pop(), stack.pop()
stack.append(b+a)
else:
stack.append(e)
for e in stack:
output += chr(int(e)+96)
return output
4. Result
Runtime : 36 ms(34.69%), Memory usage : 13.7 MB(90.95%)
(Runtime can be different by a system even if it is a same code.)