Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) {
return 0;
}
int len = s.length();
int max = 1, start = 0, end = 1;
int[] index = new int[256];
Arrays.fill(index, -1);
index[s.charAt(0)] = 0;
while(end < len) {
if(index[s.charAt(end)] >= start) {
start = index[s.charAt(end)] + 1;
}
max = Math.max(max, end - start + 1);
index[s.charAt(end)] = end++;
}
return max;
}
}