title: Sliding window solution for min length substring with all distinct characters of the original string
body: <p>Could anyone please tell where am I going wrong. Below is my code.
  Here I am using sliding window solution with left and right as pointers to String.
  "dist_count" contains the count for unique characters in string.</p>
  
  <pre><code>public class Solution {
      public String findSubstring(String s) {
          if (s == null || s.length() == 0)
              return "";
          int dist_count = 0;
          int[] hash = new int[256]; // character hash
          boolean[] visited = new boolean[256];
          // record each character in s to hash
          for (char c : s.toCharArray()) {
              hash[c]++;
          }
          for (char c : s.toCharArray()) {
              if (visited[c] == false) {
                  dist_count++;
                  visited[c] = true;
              }
          }
          System.out.println(dist_count);
          int left = 0, right = 0, count = 0;
          int min_window = Integer.MAX_VALUE;
          int startIndex = -1;
          while (right &lt; s.length()) {
              if (hash[s.charAt(right)] &gt;= 1) {
                  count++;
              }
              hash[s.charAt(right)]--;
              right++;
  
              if (count == dist_count) {
                  while (hash[s.charAt(left)] &gt; 1) {
                      if (hash[s.charAt(left)] &gt; 1)
                          hash[s.charAt(left)]--;
                      left++;
                  }
                  int window = right - left + 1;
                  if (min_window &gt; window) {
                      min_window = window;
                      startIndex = left;
                  }
              }
          }
          return s.substring(startIndex, startIndex + min_window);
      }
  
      public static void main(String args[]) {
          Solution ob = new Solution();
          String str = ob.findSubstring("abdacbfcabegbdfcadcefa");
          System.out.println(str);
      }
  }
  </code></pre>