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");
For more sophisticated cases use class Pattern.
More detail on saugata's response (appeared while I was writing this): -
Using
HashSet<Character>
:This is
O(m + n)
, which is asymptotically optimal.