class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
if (n==0) return 0;
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int ans = 0;
for(int i= 0,j=0;i<n;i++){
if(map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
ans = Math.max(ans,i-j+1);
}
return ans;
}
}
网友评论