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;
if(s.length()==1) return 1;
char[] c = s.toCharArray();
int longest = 0,index = 0,now = 0;
int [] map = new int[26];
Arrays.fill(map,-1); //下标有可能会出现 0!! 所以不可以用0!!
for(int i=0;i<c.length;i++){
if(map[c[i]-'a']==-1||map[c[i]-'a']<index){ //后者剔除一些错误的存留数据!!
now++;
map[c[i]-'a']=i;
if(now>longest){
longest = now;
}
}else{
now-=(map[c[i]-'a']-index);
index = map[c[i]-'a']+1;
map[c[i]-'a'] = i;
}
}
return longest;
}
}