title: Super Reduced String: Problem with my code - Java
body: <p>My solution for the Super Reduced String problem from HackerRank is giving me problems (<a href="https://www.hackerrank.com/challenges/reduced-string/problem" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/reduced-string/problem</a>). The example &quot;baab&quot; fails the test case, and I do not understand why. Here is my code:</p>
  <pre><code>String s = &quot;baab&quot;;
  for (int i = 0; i &lt; s.length() - 1; i++) {
      if (s.charAt(i) == s.charAt(i + 1)) {
          s = s.substring(0, i) + s.substring(i + 2);
          i = 0;
      }
  }
  if (s.isEmpty()) {
      return &quot;Empty String&quot;;
  } else {
      return s;
  }
  </code></pre>
  <p>The expected result is &quot;Empty String&quot;, but the result is &quot;bb&quot;.</p>
  <p>Through debug statements it seems like the for loop does not run a third time in this specific scenario (once for &quot;ba&quot;ab, second for b&quot;aa&quot;b, and third for &quot;bb&quot;). Why?</p>