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.
import java.util.HashMap;
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length()==0){
return 0;
}
String result="";
int max=0,cur=0;
HashMap<Character, Integer> map=new HashMap<Character, Integer> ();
for (int i =0; i< s.length();i++){
cur++;
char a=s.charAt(i);
if (map.containsKey(a)){
cur=Math.min(cur,i-map.get(a));
}
map.put(a,i);
if (cur>max){
max=cur;
}
}
return max;
}
}