Returning distinct characters in combined Strings
What comes to performance, you can improve it significantly:

public static String getDistinctCharString(String s1, String s2) {
    Set&lt;Character&gt; set = new HashSet&lt;&gt;(s1.length() + s2.length());

    char[] chars1 = s1.toCharArray();
    char[] chars2 = s2.toCharArray();

    for (char c : chars1) {
        set.add(c);
    }

    for (char c : chars2) {
        set.add(c);
    }

    StringBuilder sb = new StringBuilder(set.size());

    for (char c : set) {
        sb.append(c);
    }

    return sb.toString();
}


The point is that you deal with characters rather than substrings of length 1. 

My demonstration gives me the following output:




longest() in 2558 ms.
getDistinctCharString() in 342 ms.
Algorithms agree: true




Hope that helps.