This article introduces the problem of Java implementing string arrangement and combination for your reference. The specific content is as follows
import java.util.ArrayList; import java.util.Collections; /** * Enter a string and print out all arrangements of characters in the string in dictionary order. For example, input the string abc, then print out all the strings abc, acb, bac, * bca, cab and cba that can be arranged by the characters a, b, c. * * @author pomay * */ public class Solution_stringarrange { public ArrayList<String> Permutation(String str) { if (str == null) return null; ArrayList<String> list = new ArrayList<String>(); char[] pStr = str.toCharArray(); Permutation(pStr, 0, list); Collections.sort(list); return list; } static void Permutation(char[] str, int i, ArrayList<String> list) { // If empty if (str == null) return; // If i points to the last character if (i == str.length - 1) { if (list.contains(String.valueOf(str))) return; list.add(String.valueOf(str)); } else { // i points to the first character of the string currently doing the arrangement operation for (int j = i; j < str.length; j++) { // Exchange the first character of the string doing the arrangement operation with all the following characters char temp = str[j]; str[j] = str[i]; str[i] = temp; // After the exchange, perform recursive arrangement of the strings after i. Permutation(str, i + 1, list); // After each round is over, switch back to perform the next round of arrangement temp = str[j]; str[j] = str[i]; str[i] = temp; } } } public static void main(String[] args) { String str = "aab"; Solution_stringarrange changestring = new Solution_stringarrange(); ArrayList<String> list = changestring.Permutation(str); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } } combination:
Either select the first character in the string of length n, then select m-1 characters in the remaining strings of length n-1, or do not select the first character in the string of length n, then select m characters in the remaining strings of length n-1
import java.util.ArrayList; import java.util.List; /** * Enter a string and print out all combinations of characters in the string in dictionary order. For example, input the string abc, print out all the strings a,b,c,ab,ac,bc *,abc . Question to find the combination of n characters with length m* * @author pomay * */ public class Solution_stringcombination { // Find the combination of all characters in a string abc>a,b,c,ab,ac,bc,abc public static void perm(String s) { List<String> result = new ArrayList<String>(); // Start with one for (int i = 1; i <= s.length(); i++) { combination(s, i, result); } } // Select m characters from string s public static void combination(String s, int m, List<String> result) { // If m==0, then the recursive ends. output current result if (m == 0) { for (int i = 0; i < result.size(); i++) { System.out.print(result.get(i)); } System.out.print(", "); return; } if (s.length() != 0) { // Select the current element result.add(s.charAt(0) + ""); // Substring usage, intercept the string combination from 1 to end of n combination(s.substring(1, s.length()), m - 1, result); result.remove(result.size() - 1); // Don't select the current element combination(s.substring(1, s.length()), m, result); } } public static void main(String[] args) { String str = "abc"; perm(str); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.