Why it shows Time Limit Exceeded?


#1
bool isOperator(char c)

{
if(c==’^’ || c==’+’ || c==’’ || c==’/’ || c==’-’)
return true;
else
return false;
}
int precedence(char c)
{
if(c==’^’)
return 2;
else if(c==’/’ || c==’
’)
return 1;
else if(c==’+’ || c==’-’)
return 0;
else
return -1;
}
string Solution::solve(string A) {
int n=A.size();
stack s;
string str="";
for(int i=0;i<n;i++)
{
if(A[i]>=‘a’ && A[i]<=‘z’)
str= str+A[i];

    else if(A[i]=='(')
        s.push(A[i]);
    
    else if(A[i]==')')
    {
        while(s.top()!='(' && !s.empty())
        {
            str = str + s.top();
            s.pop();
        }
        if(s.top()=='(')
            s.pop();
       
        
    }
    else 
    {
         while(!s.empty() && precedence(A[i]) <= precedence(s.top()) )
            {
                str = str + s.top();
                s.pop();
            }
            s.push(A[i]);
        
    }
 
    
}
while(!s.empty())
{
    str = str + s.top();
     s.pop();
}
return str;

}