我一直在谷歌搜索几个小时,无法解决这个问题!我正在尝试创建一个程序,它将返回用户输入的字符串排列的每个大小。我已经想出如何返回与原始输入字符串的长度匹配的所有排列,但我也无法弄清楚如何返回子集!
例如,如果用户输入
“abc”
我想得到结果
一 b C AB AC 公元前 CB ABC ......等等。
基本上所有可能的提供字符串大小的所有可能排列。
知道我做错了什么/对代码进行任何更正来解决这个问题吗?
import java.util.ArrayList;
import java.util.Arrays;
public class Characters extends FindWords {
/**
* Gets letters provided by user and then
* runs the character permutation methods
* within this class.
*/
public void chars (){
//acquire string provided in Main file
String c = getLetters();
//passes the string to the initiate() method
initiate(c);
}
/**
* initiate the permutation easily using only the inputed string as
* the parameter.
*
* @param input
*/
void initiate( String input)
{
//get the length of the given string
int len = input.length();
//an array of the characters created by user
char[ ] in = input.toCharArray( );
//a malleable string
StringBuffer out = new StringBuffer();
//array of bools
boolean[ ] used = new boolean[ len ];
permAction ( in, out, used, len, 0 );
}
/**
* Actual action of the permutation
*
* @param in
* @param out
* @param used
* @param len
* @param x
*/
void permAction ( char[ ] in, StringBuffer out,
boolean[ ] used, int len, int x){
//end the recursion for the word if the counter x is the same length as the string
if( x == len) {
System.out.println ( out.toString());
return;
}
//for every letter in the inputed string
for( int i = 0; i < len; ++i )
{
//if true
if( used[i] ) continue;
//add letter to output string
out.append( in[i] );
used[i] = true;
//recursion call
permAction( in, out, used, len, x + 1 );
used[i] = false;
//decrease length by 1
out.setLength( out.length() - 1 );
}
}
}
提前感谢您的帮助! :)
我在google上找到了一些关于使用powersets的建议,但是我的运气比这个特定的实现更糟糕。
答案 0 :(得分:0)
这是一个基于本书的理念的解决方案&#34; Cracking the Coding Interview&#34; (P54):
/**
* List permutation of a string
*
* @param s the input string
* @return the list of permutation
*/
public static ArrayList<String> permutation(String s) {
// The result
ArrayList<String> res = new ArrayList<String>();
// If input string's length is 1, return {s}
if (s.length() == 1) {
res.add(s);
} else if (s.length() > 1) {
int lastIndex = s.length() - 1;
// Find out the last character
String last = s.substring(lastIndex);
// Rest of the string
String rest = s.substring(0, lastIndex);
// Perform permutation on the rest string and
// merge with the last character
res = merge(permutation(rest), last);
}
return res;
}
/**
* @param list a result of permutation, e.g. {"ab", "ba"}
* @param c the last character
* @return a merged new list, e.g. {"cab", "acb" ... }
*/
public static ArrayList<String> merge(ArrayList<String> list, String c) {
ArrayList<String> res = new ArrayList<String>();
// Loop through all the string in the list
for (String s : list) {
// For each string, insert the last character to all possible postions
// and add them to the new list
for (int i = 0; i <= s.length(); ++i) {
String ps = new StringBuffer(s).insert(i, c).toString();
res.add(ps);
}
}
return res;
}
运行字符串&#34; abcd&#34;:
的输出第1步:合并[a]和b: [ba,ab]
第2步:合并[ba,ab]和c: [cba,bca,bac,cab,acb,abc]
第3步:合并[cba,bca,bac,cab,acb,abc]和d: [dcba,cdba,cbda,cbad,dbca,bdca,bcda,bcad,dbac,bdac,badc,bacd,dcab,cdab,cadb,cabd,dacb,adcb,acdb,acbd,dabc,adbc,abdc,abcd] < / p>
使用递归。
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
(通过Introduction to Programming in Java)
了解如何理解正在发生的事情及其工作原理:(http://learnprogramming.machinesentience.com/java_permutations_recursion)