web123456

Ordering of strings

public class Permutation{ private static ArrayList<String> list = new ArrayList<>(); public static ArrayList<String> permutation(String str) { if(str == null){ return list; } permutation(str.toCharArray() , 0); return list; } private static void permutation(char[] chars , int ops){ if(ops == chars.length-1){ // This step is needed here because if the list is added directly (), the hash value will be printed at the end when the list is traversed, and the hash value will be printed when the list is traversed. // Here involves toString method override, you can understand. StringBuffer sb = new StringBuffer(); for(char c : chars){ sb.append(c); } if(!list.contains(sb.toString())){ list.add(sb.toString()); } } for(int i = ops ; i<chars.length;i++){ char temp = chars[i]; chars[i] = chars[ops]; chars[ops] = temp; permutation(chars,ops+1); temp = chars[i]; chars[i] = chars[ops]; chars[ops] = temp; } } }