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)
return 0;
if(s.length()<=1)
return s.length();
int maxLen=0;
int nowLen=0;
HashSet<Character> exitSet=new HashSet<Character>();
int start=0;
while(start<s.length()){
for(int i=start;i<s.length();i++){
if(exitSet.contains(s.charAt(i))){
exitSet.clear();
start++;
if(nowLen<maxLen)
nowLen=maxLen;
maxLen=0;
break;
}else{
exitSet.add(s.charAt(i));
maxLen++;
// if(nowLen<maxLen)
// nowLen=maxLen;
}
}
}
return nowLen;
}
}
public static void main(String[] args) {
String str="abccfdee";
System.out.println(lengthOfLongestSubstring(str));
}