Social Icons

Saturday, April 3, 2010

equalisNot: Solutions for Javabat :04


Question:

Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true

 

Source

My Solution

public boolean equalIsNot(String str) {
  int is=0 , not=0;
      for(int i=0 ;  i<str.length(); i++){
         i=str.indexOf("is",i);
         if(i!=-1){
            is++;
            i++;
         }
         else
             break;
      }
      for(int i=0 ; i<str.length(); i++){
         i=str.indexOf("not",i);
         if(i!=-1){
            not++;
            i=i+2;
         }
         else
             break;
      }
      return is==not;
    
}
 
 
Blogger Templates http://slots.to/