public class BinaryAlgorithm { /** * In the linux system, 1, 2, and 4 represent execute, write, and read respectively * actually in binary * 0001 * 0010 * 0100 * It can be seen that the sum of any non-empty subset is different * Use this feature to calculate a subset of a set */ public List<String> getStrSubstringSet(String str){ List<String> result = new ArrayList<String>(); char[] chars = str.toCharArray(); int length = chars.length;//string length int countStr = 2<<length - 1;//How many non-empty subsets are there for (int i = 1; i <= countStr; i++) { String binaryString = Integer.toBinaryString(i);//Convert the number of subsets to binary int zeroCount = length - binaryString.length(); for (int j = 0; j < zeroCount; j++) binaryString = "0"+binaryString;//Complete the length to be the same as the character length char[] binaryStr = binaryString.toCharArray();//Binary string StringBuilder sb = new StringBuilder(); for (int j = 0; j < binaryStr.length; j++) { if(binaryStr[j]=='1') sb.append(str.charAt(j));//If it is 1 here, record it } result.add(sb.toString()); } return result; } public static void main(String[] args) { BinaryAlgorithm ba = new BinaryAlgorithm(); System.out.println(ba.getStrSubstringSet("abcd")); } }