Question:
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 max = 0;
int start = 0;
int[] lastindex = new int[26];
Arrays.fill(lastindex, -1);
for (int i = 0; i < s.length(); i++) {
if (lastindex[s.charAt(i) - 'a'] >= start) {
max = Math.max(i - start, max);
start = lastindex[s.charAt(i) - 'a'] + 1;
}
lastindex[s.charAt(i) - 'a'] = i;
}
return Math.max(s.length()-start, max);
}
}