title: How to find the longest substring containing two unique repeating characters
body: <p>The task is to find the longest substring in a given string that is composed of any two unique repeating characters<br>
  Ex. in an input string "aabadefghaabbaagad", the longest such string is "aabbaa"<br></p>
  
  <p>I came up with the following solution but wanted to see if there is a more efficient way to do the same<br></p>
  
  <pre><code>import java.util.*; 
  
  public class SubString {
      public static void main(String[] args) { 
      //String inStr="defghgadaaaaabaababbbbbbd";
      String inStr="aabadefghaabbaagad";
      //String inStr="aaaaaaaaaaaaaaaaaaaa";
      System.out.println("Input string is         "+inStr);
  
      StringBuilder sb = new StringBuilder(inStr.length());
      String subStr="";
      String interStr="";
      String maxStr="";
      int start=0,length=0, maxStart=0, maxlength=0, temp=0;
  
      while(start+2&lt;inStr.length())   
      {    int i=0;
           temp=start;
           char x = inStr.charAt(start);
           char y = inStr.charAt(start+1);
           sb.append(x);
           sb.append(y);
  
           while( (x==y) &amp;&amp; (start+2&lt;inStr.length()) )
           {    start++;
                y = inStr.charAt(start+1);
                sb.append(y);
           }
  
           subStr=inStr.substring(start+2);
           while(i&lt;subStr.length())
           {    if(subStr.charAt(i)==x || subStr.charAt(i)==y )
                {    sb.append(subStr.charAt(i));
                     i++;
                }
                else
                     break;
           }
  
           interStr= sb.toString();
           System.out.println("Intermediate string "+ interStr);
           length=interStr.length();
           if(maxlength&lt;length)
           {    maxlength=length;
                length=0;
                maxStr = new String(interStr);
                maxStart=temp;
           }
  
           start++;
           sb.setLength(0);
     }
  
     System.out.println("");
     System.out.println("Longest string is "+maxStr.length()+" chars long "+maxStr);  
  }
  }
  </code></pre>