title: code to find longest substring with unique K characters not working for all inputs
body: <p>Given a String, ""aabbcdeeeeggi" and k=3, the code should find longest substring with maximum of k unique characters. For the above input, it should be "deeeeggi".</p>
  
  <p><a href="https://stackoverflow.com/questions/21119224/find-longest-substring-of-n-unique-characters">Here</a> is an elegant O(n) solution for the problem in Python. I am implementing in Java. but I am not getting the desired output for all the inputs.</p>
  
  <pre><code>public class SubStringWithKUniqueCharacters {
  
      public static void main(String[] args){
  
          System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 3));
          System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 2));
      }
  
      public static String longestSubStringWithUniqueK(String input, int k){
          int len = input.length();
          Set&lt;Character&gt; unique = new HashSet&lt;&gt;();
  
          int i = 0;
          int j = 0;
          int count = 0;
          int maxStartIndex = 0;
          int maxEndIndex  = 0;
          int maxLen = 0;
          char[] inputArr = input.toCharArray();
  
          while (i&lt;len){
  
              if (count==k &amp;&amp; j -i &gt; maxLen){
                  maxStartIndex = i;
                  maxEndIndex = j;
                  maxLen = maxEndIndex - maxStartIndex;
              }
               if (count&lt;k &amp;&amp; j&lt;len){
                  if (unique.add(inputArr[j])){
                      count++;
                  }
                  j++;
              }
              else {
                  if (unique.remove(inputArr[i])){
                      count--;
                  }
                  i++;
              }
          }
           return input.substring(maxStartIndex,maxEndIndex);
      }
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>eeeeggi //correct output
  eeeggi //incorrect output
  </code></pre>
  
  <p>I am not able to capture where the bug is. Any help would be much appreciated.
  Thanks</p>