Question

In: Computer Science

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.

Solutions

Expert Solution

import java.util.*;

//Created class anagram
public class Anagram {
        //Adding hashset to remove all duplicates
         HashSet<String> set=new HashSet(); 
         //Recursion method for anagram
    public HashSet<String> anag(String s1, String s2)  {
        if(s1.length() == 0)    {
                set.add(s2);
        }
        for(int i = 0; i < s1.length(); i++) {
            anag(s1.substring(0, i) + s1.substring(i+1, s1.length()), s1.charAt(i) + s2);
        }
                return set;

    }
    public static void main(String[] args)  {
        //Object for class anagram
        Anagram ana = new Anagram();
        String word; 
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a word: ");
        word=scan.nextLine();
        System.out.println("The anagrams are: ");
        HashSet<String> set=ana.anag(word, ""); 
        //Iterator for hashset
        Iterator<String> i=set.iterator();  
        while(i.hasNext())  
        {  
        System.out.println(i.next());  
        }  
    }
}

Sample input and output:

Enter a word: cat
The anagrams are:
atc
cta
tca
act
tac
cat

Enter a word: liril
The anagrams are:
lliri
lrili
rilil
rlili
ilrli
ililr
ilirl
illri
liril
lilir
irill
irlli
lriil
lrlii
liirl
riill
rilli
lilri
ilril
illir
rliil
iirll
rllii
iillr
lirli
liilr
llrii
lliir
irlil
iilrl


Related Solutions

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...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
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...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans 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 an ArrayList 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 ArrayList and then reverse the...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT