import java.util.HashMap;
import java.util.Map;
public class TestString01 {
public static void main(String[] args) {
longestNodupSubstring("abcdefghijhh");
}
/**
* cursor里面存放字符的在字符串中的位置 lengAt[i]存放以字符string.charAt(i)结尾的最长子字符串的长度
*
* @param string
*/
public static void longestNodupSubstring(String string) {
int len = string.length();
if (len > 0) {
Map<Character, Integer> cursor = new HashMap<Character, Integer>();
cursor.put(string.charAt(0), 0);
int[] lengthAt = new int[string.length()];
lengthAt[0] = 1;
int max = 0;
for (int i = 1; i < len; i++) {
char c = string.charAt(i);
if (cursor.containsKey(c)) {
lengthAt[i] = Math.min(lengthAt[i - 1] + 1, i
- cursor.get(c));
} else {
lengthAt[i] = lengthAt[i - 1] + 1;
}
max = Math.max(max, lengthAt[i]);
cursor.put(c, i);
}
for (int i = 0; i < len; i++) {
if (max == lengthAt[i]) {
System.out.println(string.substring(i - max + 1, i + 1));
}
}
}
}
}
//java 实现 一个字符串中不重复最长子串
public class test5 {
public static void main(String[] args) {
String str = "asdrtyasdfghwew";
System.out.println(noDuplicate(str));
}
public static String noDuplicate(String str) {
String sub = "";
String result = "";
for (int i = 0; i < str.length(); i++) {
String s = "" + str.charAt(i);
if (sub.contains(s)) {
if (sub.length() > result.length()) {
result = sub;
}
sub = "";
}
sub = sub + s;
}
if (sub.length() > result.length()) {
result = sub;
}
return result;
}
}