title: shortest substring of string in C#
body: <p>I try to write I program that given a string comprised of lowercase lettes in the range ascii[a-z] and determine the length of the smallest substring that contains all of the letters present on the string.</p>
  
  <p>but I got Terminated due to timeout.</p>
  
  <p>How can I improve the sulotion?</p>
  
  <p>I tried:</p>
  
  <pre><code>    public static int shortestSubstring(string s){
          int n = s.Length;
              int max_distinct = max_distinct_char(s, n);
              int minl = n;
              for (int i = 0; i &lt; n; i++)
              {
                  for (int j = 0; j &lt; n; j++)
                  {
                      String subs = null;
                      if (i &lt; j)
                          subs = s.Substring(i, s.Length - j);
                      else
                          subs = s.Substring(j, s.Length - i);
                      int subs_lenght = subs.Length;
                      int sub_distinct_char = max_distinct_char(subs, subs_lenght);
                      if (subs_lenght &lt; minl &amp;&amp; max_distinct == sub_distinct_char)
                      {
                          minl = subs_lenght;
                      }
                  }
              }
              return minl;
      }
          private static int max_distinct_char(String s, int n)
          {
              int[] count = new int[NO_OF_CHARS];
              for (int i = 0; i &lt; n; i++)
                  count[s[i]]++;
  
              int max_distinct = 0;
              for (int i = 0; i &lt; NO_OF_CHARS; i++)
              {
                  if (count[i] != 0)
                      max_distinct++;
              }
              return max_distinct;
          }
  
  }
  
  
  </code></pre>