Solution of the problem


#1

string Solution::simplifyPath(string A) {

stack<string> s;

int i;
string str="";
for(i=0;i<A.length();)
{
    str="/";
    if(A[i]=='/')
    {
      i++;
      while(A[i]!='/' && i<A.length())
      {
        //cout<<"chk1 "<<i<<"  ";
        str+=A[i++];
      }
      //cout<<"string "<< str<<"\n";
      if(str=="/..")
      {
        if(!s.empty())
            s.pop();
      }
      else
      if(!(str=="/.") && !(str=="/"))
      {
            s.push(str);
      }
        
    }
    //cout<<i;
    
}
if(s.empty())
    return "/";
else
{
    str="";
    while(!s.empty())
    {
        str=s.top()+str;
        s.pop();
    }
     return str;  
}

}


#2

This exact same solution gave me time limit exceeded. I really don’t know what complexity they expected.


#3

I am also getting TLE after submitting same kind of solution.