思路
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f3cf17057574ea8974b7833b468d329d.jpeg)
代码实现
package digui;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Code03_PrintAllSubsquences {
public static List<String> subs(String s) {
char[] chars = s.toCharArray();
String path = "";
List<String> res = new ArrayList<>();
process1(chars, 0, res, path);
return res;
}
public static void process1(char[] str, int index, List<String> ans, String path) {
if (index == str.length) {
ans.add(path);
return;
}
process1(str, index + 1, ans, path);
process1(str, index + 1, ans, path + String.valueOf(str[index]));
}
public static List<String> subsNoRepeat(String s) {
char[] chars = s.toCharArray();
String path = "";
Set<String> set = new HashSet<>();
process2(chars, 0, set, path);
List<String> res = new ArrayList<>(set);
return res;
}
public static void process2(char[] str, int index, Set<String> ans, String path) {
if (index == str.length) {
ans.add(path);
return;
}
process2(str, index + 1, ans, path);
process2(str, index + 1, ans, path + String.valueOf(str[index]));
}
public static void main(String[] args) {
String test = "acccc";
List<String> ans1 = subs(test);
List<String> ans2 = subsNoRepeat(test);
for (String str : ans1) {
System.out.println(str);
}
System.out.println("=================");
for (String str : ans2) {
System.out.println(str);
}
System.out.println("=================");
}
}