The method permutations
is taking in a String prefix
and a String s
as its parameters.
The int
type n
is being set to the length of the String s
. (length of a String being how many characters it contains).
Now we move on to the if
-else
statements. The if
statement is saying, if the length of s
is 0, that is, s
is a blank String and does not contain any information, then we are simply printing the String prefix
instead to the console. The method will then skip over the else
part and execute the code after the permutations
method.
If the if
statement's conditions are not met, we are going to run the else
statement is saying, for each character in the String s
, we are going to append (add) that character at the end of prefix
, so for example, if prefix
was originally "hello" and the character was 'U', we would get prefix
to be "helloU". After we are finished appending all of the characters in s
, we are going to be using the result as the new prefix
String.
For the other parameter, the String s
, we are going to be taking part of the String, from character 0 (inclusive) to character at position i
(exclusive). Please note that the String indexes start at 0 and go up to (The length of the String - 1). We are also taking the part of the String from the character at position i + 1 (inclusive) to the last character in the String s
. We are going to use this result as the new s
String.
Then we would be calling the method again, and then the method would execute again with the newly defined Strings if the else
condition is met. This would continue in a loop until the else
condition is not met, at which point the method would stop running and we would move on to the next section of code (if it is present).