给定字符串M求其中不重复的最长子串
如 abascopsc 输出[a, b, s, c, o, p]
第一种方式会稍微简单
public class MaxStr {
static LinkedList<String> res = new LinkedList<>();
public static void main(String[] args) {
String s = "abascopsc";
/**
* 方式一 简单
*/
Map<String, String> map = new HashMap<>();
List<String> strings = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
String value = String.valueOf(s.charAt(i));
if (map.get(value) != null) {
continue;
}
map.put(value, value);
strings.add(value);
}
System.out.println(strings);
/**
* 方式二 动态规划(不撤销选择方式)
*/
LinkedList<String> tmp = new LinkedList<>();
List<String> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
list.add(String.valueOf(s.charAt(i)));
}
traverse(list, tmp, 0);
System.out.println(res);
}
private static void traverse(List<String> list, LinkedList<String> tmp, int num) {
if (!res.contains(list.get(num))) {
res.add(list.get(num));
}
System.out.println(tmp);
System.out.println(num);
for (int i = num; i < list.size(); i++) {
if (tmp.contains(list.get(i))) {
continue;
}
tmp.add(list.get(i));
traverse(list, tmp, i + 1);
//不撤销选择
}
}
}