Write a recursive method that takes as input a string s and returns a list containing all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For example, the word ‘binary’ is an anagram of ‘brainy’. Note that the output list should not contain duplicates. Java
Code Screenshot :
Executable Code:
class Main
{
private static void anagram(String can, String
rem)
{
if (rem.length() == 0) {
System.out.println(can);
}
for (int i = 0; i <
rem.length(); i++)
{
String newCan =
can + rem.charAt(i);
String newRem =
rem.substring(0, i) +
rem.substring(i + 1);
anagram(newCan,
newRem);
}
}
public static void main(String[] args)
{
String s = "brainy";
anagram("", s);
}
}
Sample Output :
Get Answers For Free
Most questions answered within 1 hours.