Need a Java function to find intersection of two strings. i.e. characters common to the strings.
Example:
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
Need a Java function to find intersection of two strings. i.e. characters common to the strings.
Example:
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
Most basic approach:
Found same question here, refer this
Implementing an efficent algorithm to find the intersection of two strings
I have used
TreeSet
. AndretainAll()
inTreeSet
to get matched elements.Extract the characters
Put them in a Set Find the intersection
I think the algorithm you are looking for is the problem of the longest common subsequence
By means of Guava this task seems much easier: