title: To Reduce a String
body: <p>I was solving a problem to reduce the form to it's non-reducible form. This was the question.</p>
  <blockquote>
  <p>Shil has a string S , consisting of N lowercase English letters. In one operation, he can delete any pair of adjacent letters with same value. For example, string &quot;aabcc&quot; would become either &quot;aab&quot; or &quot;bcc&quot; after  operation.</p>
  </blockquote>
  <blockquote>
  <p>Shil wants to reduce S as much as possible. To do this, he will repeat the above operation as many times as it can be performed. Help Shil out by finding and printing 's non-reducible form!</p>
  </blockquote>
  <blockquote>
  <p>If the final string is empty, print Empty String; otherwise, print the final non-reducible string.</p>
  </blockquote>
  <blockquote>
  <p>Sample Input 0</p>
  </blockquote>
  <blockquote>
  <p><strong>aaabccddd</strong></p>
  </blockquote>
  <blockquote>
  <p>Sample Output 0</p>
  </blockquote>
  <blockquote>
  <p><strong>abd</strong></p>
  </blockquote>
  <blockquote>
  <p>Sample Input 1</p>
  </blockquote>
  <blockquote>
  <p><strong>baab</strong></p>
  </blockquote>
  <blockquote>
  <p>Sample Output 1</p>
  </blockquote>
  <blockquote>
  <p><strong>Empty String</strong></p>
  </blockquote>
  <blockquote>
  <p>Sample Input 2</p>
  </blockquote>
  <blockquote>
  <p><strong>aa</strong></p>
  </blockquote>
  <blockquote>
  <p>Sample Output 2</p>
  </blockquote>
  <blockquote>
  <p><strong>Empty String</strong></p>
  </blockquote>
  <p>Explanation</p>
  <p>Sample Case 0:
  Shil can perform the following sequence of operations to get the final string:</p>
  <p>Thus, we print .</p>
  <p>Sample Case 1:
  Shil can perform the following sequence of operations to get the final string:
  <strong>aaabccddd -&gt; abccddd</strong></p>
  <p><strong>abccddd   -&gt; abddd</strong></p>
  <p><strong>abddd     -&gt; abd</strong></p>
  <p>Thus we print <strong>abd</strong></p>
  <p>Sample case 1:
  <strong>baab -&gt; bb</strong></p>
  <p><strong>bb   -&gt; Empty String.</strong></p>
  <p>And what I have done till now is try to solve it through StringBuilder in Java.But some of testcases pass while other's don't and I can't find out what's the error?</p>
  <blockquote>
  <p>Here is the code that I have tried so far.</p>
  </blockquote>
  <pre><code>import java.util.Scanner;
  
  public class Solution {
  
  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      StringBuilder sb = new StringBuilder(scan.nextLine());
      for(int i = 0; i &lt; sb.length()-1; i++)
          {
              if(sb.charAt(i) == sb.charAt(i+1))
                  sb.delete(i,i+2);
                  i  = 0;
          }
      if(sb.length() == 0)
          System.out.println(&quot;Empty String&quot;);
      else
          System.out.println(sb.toString());
  }
  </code></pre>
  <p>}</p>
  <p>Inputs like  <strong>aaabccddd</strong></p>
  <p>and <strong>aa</strong> pass.But when the input is <strong>baab</strong> it fails.</p>