for _ in range(int(input())): p = input() a,b = 1,1 con = [] oper = [] i = 0 while i<len(p): if(p[i]==')'): j = oper.pop() a*=j b*=j a+=con[-1][0] b+=con[-1][1] con.pop() elif(p[i]=='E'): a+=1 elif(p[i]=="N"): b-=1 elif(p[i]=="S"): b+=1 elif(p[i]=="W"): a-=1 else: oper.append(int(p[i])) con.append([a,b]) a,b = 0,0 i+=1
i+=1 if(a<1): a = 1000000000+a if(b<1): b = 1000000000+b a = a%1000000000 b = b%1000000000 if(a==0): a = 1000000000 if(b==0): b = 1000000000 print("Case #",_+1,": ",sep = "",end = "") print(a,b)
Can someone please tell me why my O(N) approach got TLE. Thankyou