最长不含重复字符的子字符串
public class Offer54 {
public static void main(String[] args) {
System.out.println(new Offer54().LongestSubStringWithoutDuplication("asfahjfabswr"));
}
public int LongestSubStringWithoutDuplication(String str) {
if (str == null || str.length() == 0) return 0;
int preLength = 0;
int curLength = 0;
int maxLength = 0;
int[] pos = new int[26];
for (int i = 0; i < pos.length; i++) {
pos[i] = -1;
}
for (int i = 0; i < str.length(); i++) {
int letterNumber = str.charAt(i) - 'a';
if (pos[letterNumber] < 0 || i - pos[letterNumber] > preLength) {
curLength = preLength + 1;
} else {
curLength = i - pos[letterNumber];
}
pos[letterNumber] = i;
if (curLength > maxLength)
maxLength = curLength;
preLength = curLength;
}
return maxLength;
}
}