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...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +. (b) Analyze time and space of part (a) (c)Implement part (a) in your favorite programming language without using 0+0 operator. You should read the two numeric strings from a FILE input.in and output their addition to standard output. You Solution will be test on 10,000 test cases. Each test case will include...
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
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...
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
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...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT