Longest substring with 2 unique characters in a given string at linear time This is my java implementation based on the solution of Aasmund Eldhuset here.
Please give your opinion on my code (correctness, efficiency and coding conventions):             

public static String longest2Str(String s){
    int len;
    int i = 1;
    int subStart = 0;
    int nextGroupStart = 0;
    int longestStart = 0;
    int longestEnd = 0;
    char firstUnique, secondUnique, c;

    if ( (s == null) || (s.length() &lt; 2) ) {
        return null;
    }

    len = s.length();
    firstUnique = s.charAt(subStart);

    // find first letter in s that isn't equal to the firstUnique 
    for (; (i &lt; len) &amp;&amp; (s.charAt(i) == firstUnique); i++);

    secondUnique = s.charAt(i);
    for (; i &lt; len; i++) {
        c = s.charAt(i);
        if ( (c == firstUnique) || (c == secondUnique) ) {
            if (c != s.charAt(i - 1)) {
                nextGroupStart = i;
            }   
        }
        else {
            if ( i - subStart &gt; longestEnd - longestStart ){
                longestStart = subStart;
                longestEnd = i;
            }
            subStart = nextGroupStart;
            nextGroupStart = i;
            firstUnique = s.charAt(subStart);
            secondUnique = c;
        }
    }

    return s.substring(longestStart, longestEnd);
}
Corner cases

You correctly handle the corner cases of too short input and null input.
But you forgot to handle the case of all same characters, for example "aaa".
On such input the program throws StringIndexOutOfBoundsException on line secondUnique = s.charAt(i);.

Variable scope

The variable c is only needed within the main for loop,
so it would be best to declare it inside the loop.

Style

This is fine:


for (; (i &lt; len) &amp;&amp; (s.charAt(i) == firstUnique); i++);



But it looks a bit strange.
A for loop is easiest to read in its most classing form a counting loop:
it performs some initialization,
then on each iteration it does its main work in the body.
Here, the main work to do is the counting.
So if you transform this to a while loop with a body,
it becomes slightly easier to read.