如何使用不同的“r”数字制作所有字符串组合?

我想编写一个函数,它接受一个“整数”和一个“字符串数组”,然后返回所有可能的组合。


考虑到所有字符串都是不同的


这是一个例子:


ArrayList<Strings> strings={a,b,c}

int n= strings.size();

k= 1;

String sol="";

while(k!=n+1){

sol+=function(strings,k);//<== this is the problem

k++;

}


String[] solution= sol.split(" ");

并在删除重复元素之后:


//Solution expected:={a,b,c,ab,bc,ac,abc} //the elements should not be repeated


梵蒂冈之花
浏览 191回答 1
1回答

慕雪6442864

现在这是获取字符串排列的一个有趣的转折,说起来容易做起来难。无论如何,如果您确实有一种方法可以根据为每个排列提供的最小/最大长度值为您提供所需的排列,那么实现起来相对简单,而且我确信仅StackOverflow就有很多这样的代码示例。通常排列是在简单的字符串上完成的,"abc"而不是包含在某种类型的单个字符/字符串集合中的元素,如{'a', 'b', 'c'}或{"a", "b", "c"}。为了有效地获得所需的排列,您需要将 ArrayList 元素转换为字符串,以便{"a", "b", "c"}将"abc". 除非每个元素实际上包含多个字符串字符,否则在集合中继续操作是没有意义的,因此将集合转换为字符串是有意义的。首先,您需要一种执行字符串排列的方法,我确信 StackOverflow 中有很多方法可以解决。在任何情况下,我都会为您提供我对排列方法的再现,该方法允许您提供一个字符串,提供每个排列所需的字符长度,并删除重复项(或不删除)。该方法只接受一个字符串,以保持方法的灵活性。然而,它确实返回一个 String 的 ArrayList,您当然可以通过对代码进行一些修改来更改为返回您喜欢的任何内容。如果您确实更改了代码,请记住该方法是递归的:/**&nbsp;* This method will generate all possible permutations of the supplied input&nbsp;* string and return all those permutations within an String&nbsp;* ArrayList.<br><br>&nbsp;*&nbsp;* @param inputString&nbsp; &nbsp; &nbsp;(String) The string to acquire permutations&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from.<br>&nbsp;*&nbsp;* @param length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Integer) The desired min/max string length of&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returned permutations. In other words, if you only&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; want all permutations consisting of 3 characters&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then supply the number 3. If you want all possible&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permutations to be returned then supply the number&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.<br>&nbsp;*&nbsp;&nbsp;* @param removeDuplicates (Boolean) Supply boolean true to ensure there will&nbsp;&nbsp;* be no duplicate permutations within the returned ArrayList. Supply false if&nbsp;&nbsp;* duplicates are desired. You can get duplicate permutations if there are&nbsp;&nbsp;* duplicate characters within the supplied input string, for example:<pre>&nbsp;*&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; "aabbcc"&nbsp;*&nbsp;&nbsp;* can return these permutations:&nbsp;&nbsp;*&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; a, a, b, b, c, c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 Character Permutations. See the Duplicates?&nbsp;*&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; aa, ab, ac, aa, ab, ac,&nbsp; &nbsp; &nbsp;2 Character Permutations&nbsp;*&nbsp; &nbsp; &nbsp; ba, bb, bc, ba, bb, bc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;See the Duplicates?&nbsp;*&nbsp; &nbsp; &nbsp; ca, cb, cc, ca, cb, cc&nbsp;*&nbsp;*&nbsp; &nbsp; &nbsp; aab, aac, aba, abb, abc,&nbsp; &nbsp; 3 Character Permutations&nbsp;*&nbsp; &nbsp; &nbsp; aca, acb, acc, aab, aac,&nbsp; &nbsp; &nbsp; &nbsp; See the Duplicates?&nbsp;*&nbsp; &nbsp; &nbsp; aba, abb, abc, aca, acb,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; acc, baa, bab, bac, bba,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; bbc, bca, bcb, bcc, baa,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; bab, bac, bba, bbc, bca,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; bcb, bcc, caa, cab, cac,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; cba, cbb, cbc, cca, ccb,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; caa, cab, cac, cba, cbb,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; cbc, cca, ccb&nbsp;*&nbsp;&nbsp;* However if boolean true is supplied to remove duplicates the results would&nbsp;&nbsp;* be:&nbsp;*&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; a, b, c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 Character Max Permutations. No Duplicates.&nbsp;*&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; aa, ab, ac, ba, bb, bc,&nbsp; &nbsp; &nbsp;2 Character Max Permutations&nbsp;*&nbsp; &nbsp; &nbsp; ca, cb, cc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; No Duplicates&nbsp;*&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; aab, aac, aba, abb, abc,&nbsp; &nbsp; 3 Character Max Permutations&nbsp;*&nbsp; &nbsp; &nbsp; aca, acb, acc, baa, bab,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; No Duplicates&nbsp;*&nbsp; &nbsp; &nbsp; bac, bba, bbc, bca, bcb,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; bcc, caa, cab, cac, cba,&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; cbb, cbc, cca, ccb</pre>&nbsp;*&nbsp;&nbsp;*&nbsp;&nbsp;* @param recursiveResult (String) FOR INTERNAL RECURSIVE USE ONLY! DO NOT&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SUPPLY A ARGUMENT HERE!<br>&nbsp;*&nbsp;* @return (String ArrayList)&nbsp;*/public ArrayList<String> getPermutations(String inputString, final int length,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; boolean removeDuplicates, String... recursiveResult) {&nbsp; &nbsp; String currentResult = "";&nbsp; &nbsp; if (recursiveResult.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; currentResult = recursiveResult[0];&nbsp; &nbsp; }&nbsp; &nbsp; //Convert the inputString to a ArrayList of characters...&nbsp; &nbsp; ArrayList<Character> possibleChars = new ArrayList<>(inputString.length());&nbsp; &nbsp; for (int i = 0; i < inputString.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; possibleChars.add(inputString.charAt(i));&nbsp; &nbsp; }&nbsp; &nbsp; ArrayList<String> result = new ArrayList<>(possibleChars.size());&nbsp; &nbsp; for (char append : possibleChars) {&nbsp; &nbsp; &nbsp; &nbsp; String permutation = currentResult + append; //create a new string with an additional character&nbsp; &nbsp; &nbsp; &nbsp; if (permutation.length() == length || length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(permutation); //add the permutation to the result&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (possibleChars.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //make a new list with the appendable characters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Character> possibleCharsUpdated = (ArrayList) possibleChars.clone();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //from that list, exclude the character we just appended&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possibleCharsUpdated.remove(new Character(append));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Convert the new character ArrayList to a String&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //of characters for the recursion...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String passToRecursion = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < possibleCharsUpdated.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; passToRecursion += possibleCharsUpdated.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //merge the result of a recursive call of this method and the result we already had&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.addAll(getPermutations(passToRecursion, length, true, permutation));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Remove duplicates if desired...&nbsp; &nbsp; // LinkedHashSet doesn't allow for Duplicates&nbsp;&nbsp; &nbsp; // and automatically removes them.&nbsp; &nbsp; if (removeDuplicates) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> tmpArray = new ArrayList<>(new LinkedHashSet<>(result));&nbsp; &nbsp; &nbsp; &nbsp; result.clear();&nbsp; &nbsp; &nbsp; &nbsp; result.addAll(tmpArray);&nbsp; &nbsp; &nbsp; &nbsp; tmpArray.clear();&nbsp; &nbsp; }&nbsp; &nbsp; return result;}现在要从您提供的内容中获得您想要的结果,它会是这样的:// Create String ArrayListArrayList<String> strings = new ArrayList<>();strings.add("a"); strings.add("b"); strings.add("c");// Convert ArrayList to a simple stringString stringToPermutate = String.join("", strings);// Hold the number of elements within the strings ArrayListint n = strings.size();// Counter for while loop condition which will// ultimately determine the Permutation Character// Length for each iteration within the WHILE loop.int k = 1;// Prepare to build a result string that will hold// the result from each call to the getPermutations// method (which by the way is a recursive method).StringBuilder sol = new StringBuilder();while (k <= n) {&nbsp; &nbsp; // Call method to permutate our simple string based&nbsp; &nbsp; // on the Permutation Character Length stipulated by k&nbsp; &nbsp; ArrayList<String> permutations = getPermutations(stringToPermutate, k, true);&nbsp; &nbsp; // Convert ArrayList to a comma (, ) delimited string&nbsp; &nbsp; String listToString = String.join(", ", permutations);&nbsp;&nbsp; &nbsp; // Ternary used here instead of IF/ELSE&nbsp; &nbsp; sol.append(sol.toString().equals("") ? listToString : ", " + listToString);&nbsp; &nbsp; // Increment counter&nbsp; &nbsp; k++;}// Split the contents of sol into a string ArrayString[] solution = sol.toString().split(", ");// Print solution String Array to Console window.System.out.println(Arrays.toString(solution));这是您最终应该在控制台窗口中显示的内容:[a, b, c, ab, ac, ba, bc, ca, cb, abc, acb, bac, bca, cab, cba]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java