import java.util.HashSet;
public class StringIntersection {
// Function to compute the intersection of characters in two strings
public static HashSet<Character> getIntersection(String str1, String str2) {
HashSet<Character> hst1 = new HashSet<>();
for (char ch : str1.toCharArray()) {
hst1.add(ch);
}
HashSet<Character> hst2 = new HashSet<>();
for (char ch : str2.toCharArray()) {
hst2.add(ch);
}
hst1.retainAll(hst2); // Keep only common characters
return hst1;
}
public static void main(String[] args) {
String str1 = "php";
String str2 = "python";
HashSet<Character> intersection = getIntersection(str1, str2);
System.out.println("Intersection: " + intersection);
}
}
/*
run:
Intersection: [p, h]
*/