I don’t know why i am getting TLE in my code…help me …It’s O(n) solution
Link to problem:-
dict={'H':1,'C':12,'O':16}
s=input()
stack=[0]
i=0
while i<len(s):
if s[i] in dict:
temp=1
try:
if s[i+1].isdigit():
temp=s[i+1]
val=dict[s[i]]*int(temp)
stack[-1]+=val
i+=2
else:
stack[-1]+=dict[s[i]]
i+=1
except:
stack[-1]+=dict[s[i]]
i+=1
elif s[i]=='(':
stack.append(0)
i+=1
elif s[i]==')':
try:
if s[i+1].isdigit():
temp=int(s[i+1])
val=stack[-1]*temp
stack[-2]+=val
stack.pop()
i+=2
#print('a')
else:
stack[-2]+=stack[-1]
stack.pop()
i+=1
#print('b')
except:
stack[-2]+=stack[-1]
stack.pop()
i+=1
#print('c')
print(stack)
print(stack[0])