英文:
How to find permutations of a string with all distinct characters?
问题
The role of the "rem" variable in the code is to represent the remaining characters of the input string "str" after removing the character at the current position (index "i"). It is used to recursively generate permutations of the string by repeatedly removing one character at a time and adding it to the "prefix" string.
英文:
I have found a piece of code in a book which claims to print all permutations of a string with all distinct characters :-
void permutation(String str) {
permutation(str, "");
}
void permutation(String str, String prefix) {
if (str.length() == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < str.length(); i++) {
String rem = str.substring(0, i) + str.substring(i + 1);
permutation(rem, prefix + str.charAt(i));
}
}
}
What is the role of the rem variable in the code?
答案1
得分: 2
你可以可视化这个过程。
static int indent = 0;
static String indent(int i) { return " ".repeat(i); }
void permutation(String str) {
System.out.println("permutation(\"" + str + "\")");
++indent;
permutation(str, "");
}
void permutation(String str, String prefix) {
System.out.println(indent(indent) + "permutation(\"" + str + "\", \"" + prefix + "\")");
if (str.length() == 0) {
System.out.println(indent(indent + 1) + "--> " + prefix);
} else {
for (int i = 0; i < str.length(); i++) {
String rem = str.substring(0, i) + str.substring(i + 1);
++indent;
permutation(rem, prefix + str.charAt(i));
--indent;
}
}
}
和
permutation("abc");
输出
permutation("abc")
permutation("abc", "")
permutation("bc", "a")
permutation("c", "ab")
permutation("", "abc")
--> abc
permutation("b", "ac")
permutation("", "acb")
--> acb
permutation("ac", "b")
permutation("c", "ba")
permutation("", "bac")
--> bac
permutation("a", "bc")
permutation("", "bca")
--> bca
permutation("ab", "c")
permutation("b", "ca")
permutation("", "cab")
--> cab
permutation("a", "cb")
permutation("", "cba")
--> cba
英文:
You can visualize the process.
static int indent = 0;
static String indent(int i) { return " ".repeat(i); }
void permutation(String str) {
System.out.println("permutation(\"" + str + "\")");
++indent;
permutation(str, "");
}
void permutation(String str, String prefix) {
System.out.println(indent(indent) + "permutation(\"" + str + "\", \"" + prefix + "\")");
if (str.length() == 0) {
System.out.println(indent(indent + 1) + "--> "+ prefix);
} else {
for (int i = 0; i < str.length(); i++) {
String rem = str.substring(0, i) + str.substring(i + 1);
++indent;
permutation(rem, prefix + str.charAt(i));
--indent;
}
}
}
and
permutation("abc");
output
permutation("abc")
permutation("abc", "")
permutation("bc", "a")
permutation("c", "ab")
permutation("", "abc")
--> abc
permutation("b", "ac")
permutation("", "acb")
--> acb
permutation("ac", "b")
permutation("c", "ba")
permutation("", "bac")
--> bac
permutation("a", "bc")
permutation("", "bca")
--> bca
permutation("ab", "c")
permutation("b", "ca")
permutation("", "cab")
--> cab
permutation("a", "cb")
permutation("", "cba")
--> cba
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论