Beautiful Binary String

  • + 0 comments

    Java solution

     static boolean findPattern(int index, String b){
        int n = b.length();
        return (index < n - 2 && b.substring(index, index + 3).equals("010") );
        }
     
      public static int beautifulBinaryString(String b) {
        int count = 0;
        int n = b.length();
        int i = 0;
        while(i < n){
          if(findPattern(i, b)){
            count++;
            i += 3;
            }
          else
            i++;
           
          }
        return count;
        }