Question

In: Computer Science

Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...

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.

Solutions

Expert Solution

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


Related Solutions

Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
The intersection (∩) of two sets (s1, s2) is the set of all elements that are...
The intersection (∩) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate elements), and returns the intersection of those two sets (as a list) without using the in operator or any built-in functions, except for range() and len(). Write some code to test your function, as well. Note: Do not use the...
The split method in the String class returns an array of strings consisting of the substrings...
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters. public static String[] split(String s, String regex) For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?,...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT