Tuesday 19 July 2016

xyBalance

Home

Goto Problem

We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.

xyBalance("aaxbby") → true
xyBalance("aaxbb") → false
xyBalance("yaaxbb") → false
public boolean xyBalance(String str)
{
 int i=0;
 boolean res=true;
 while(i<str.length())
 {
 if(str.charAt(i)=='x')
 {
  for(int j=i;j<str.length();j++)
  {
      if(str.charAt(j)=='y')
     {
        res=true;
        break;
      }
    else
         res=false;
  }
   }
 i++;
  }
 return res;
 }

No comments:

Post a Comment