public class TestB { public static void permutation(String str,Set set){ if(str==null) return; int len=str.length(); if(len==0 || len==1)return; permutation(str.toCharArray(), 0,set); } private static void permutation(char[] str, int i, Set set) { if (i >= str.length) return; if (i == str.length - 1) { // System.out.println(String.valueOf(str)); set.add(String.valueOf(str)); } else { for (int j = i; j < str.length; j++) { char temp = str[j]; str[j] = str[i]; str[i] = temp; permutation(str, i + 1,set); temp = str[j]; str[j] = str[i]; str[i] = temp; } } } public static void main(String[] args) { Set<String>set=new HashSet<>(); permutation("acda",set); for(String s:set){ System.out.println(s); } } }