Accepted solution

Counting Valleys(Hacker Rank)

Open original problem
Source code

int countingValleys(int n, string s)

{

   int count=0,ans=0;

   for(int i=0;i<n;i++)

   {

       if(s[i]=='U')

       {

           count++;

       }

       else

       {

           count--;

       }

      if(s[i]=='U' && count==0) ans++;

   }

 

   return ans;

}
Join the discussion

Add your comment