Change your arr
parameter to be a List. Then instead of the println() just keep adding to this List. Also, when you call permute for the first time, make sure you pass in an allocated List. Like so:
public void ParentFunction()
{
List<String> results = new ArrayList<String>();
Permute(..., ..., results);
// Now you have all results inside "results" variable.
}
public static void Permute(String soFar, String rest, List<String> results)
{
if (rest.isEmpty())
{
//System.out.println(soFar);
results.add(soFar); // ADD TO RESULT LIST
}
else
{
for (int i = 0; i < rest.length(); i++)
{
String next = soFar + rest.charAt(i);
String remaining = rest.substring(0, i) + rest.substring(i + 1);
Permute(next, remaining, results);
}
}
}