In: Computer Science
Java RECURSIVE methods:
=> intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2.
=> union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2.
=>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
CODE:
Kindly upvote if
this helped
public class Recursive {
        
        public static String intersection(String s1 , String s2) {
                String temp = "";
                int length = s1.length();
                int length2 = s2.length();
                for(int i=0;i<length;i++) {
                        for(int j=0;j<length2;j++) {
                                if(s1.charAt(i) == s2.charAt(j)) {   // if same char exists making a new string
                                        if(temp.indexOf(s1.charAt(i))<0) { // if the character is not present, then only add to new string
                                                temp+=s1.charAt(i);                                             
                                        }
                                        break; // come out of inner loop as we have found the char
                                }
                        }
                }
                
                return temp;
        }
        
public static String union(String s1 , String s2) {
        String temp = "";
        int length = s1.length();
        int length2 = s2.length();
        char t;
        for(int i=0;i<length;i++) {
                t = s1.charAt(i);
                if(temp.indexOf(t)<0) {
                        temp+=s1.charAt(i);
                }
        }
        for(int i=0;i<length2;i++) {
                t = s2.charAt(i);
                if(temp.indexOf(t)<0) {
                        temp+=s2.charAt(i);
                }
        }
        
        return temp;
        }
public static String difference(String s1 , String s2) {
        String temp = "";
        int length = s1.length();
        int length2 = s2.length();
        boolean flag = true;
        for(int i=0;i<length;i++) {
                for(int j=0;j<length2;j++) {
                        if(s1.charAt(i) == s2.charAt(j)) {   // if same char exists making a new string
                                flag = false;
                        }
                }
                if(flag) { // if it only occurs in s1
                        temp+=s1.charAt(i);
                }
                flag = true; // change to def value
        }
        
        return temp;
}
        
public static void main(String[] args) {
         System.out.println(difference("aman", "kaushik"));
         System.out.println(union("aman", "kaushik"));
         System.out.println(intersection("aman", "kaushik"));
}
}
- Each character should appear once in INTERSECTION , UNION and
DIFFERENCE as PER SET Theroy.
- Implemented same way.
OUTPUT
Snapshot
