title: Count the occurence of 'B' in a String S, provided all A's are together
body: <p>Problem Statement</p>
  <p>you are given a string of characters A's and B's
  you want all A's to be together,
  to achieve this you can delete some B's from the string,
  What is the minimum number of B's you can delete to achieve this</p>
  <p>Input :: The First and only Argument is string A</p>
  <p>Output :: Return an Integer,equal to minimum number of B's you have to delete.</p>
  <pre><code>Example String s1 =  &quot;AAABA&quot;      SOP(&quot; output=1B is to be deleted, &quot;)
  Example String s2 =  &quot;AAAAB&quot;      SOP(&quot; output=0B is to be deleted, All A's are together.&quot;);
  Example String s3 =  &quot;AAABBBBA &quot;  SOP(&quot; output= 4B is to be deleted&quot;)
  Example String s4 =  &quot;AAAAAAAAAB&quot; SOP(&quot; output =0B is to be deleted,All A's are together&quot;)
  </code></pre>
  <p>I have implemented this partialy, but can you point out
  where I'm wrong</p>
  <p>Initial Analysis of the algorithm :: I'm iterating the string s1 and I'm trying to find the position of the <code>lastCharacter</code> which is equal to 'A', I am trying to find the the position of B and increment the output,hence keep a count of output and then display the result which is displaying the wrong result. Where am I going wrong? Could you give me pointers to achieve the desired result as stated in Problem Statement</p>
  <pre><code>import java.util.*;
  
  
      class AlgorithmInJava{
  
         public static void main(String args[]){
  
          /* Testing Scenarios */
  
          String s1= &quot;AAABBBBA&quot;;          
  
          // print output =4B
  
          int output = 0;
  
          int n = s1.length();
  
          char lastCharacter = s1.charAt(n-1);
  
          System.out.println(&quot; The Last Character is &quot;+lastCharacter);
  
          int lastCharacterIndex = s1.lastIndexOf(lastCharacter);
  
          System.out.println(&quot;index of lastCharacter :: &quot;+lastCharacterIndex);
  
           
  
          for(int i=0;i&lt;s1.length();i++){
  
            if(lastCharacter == 'A' &amp;&amp; s1.charAt(i) == 'B'){
  
             output++;
  
             System.out.println(&quot; B output is &quot;+output);
  
            }else{
  
            // All A's are together,print output = 0;
  
             System.out.println(&quot; All A's are together,hence B output is :: &quot;+output);
  
           }
  
        }
  
      }
  
  }
  
  </code></pre>