public class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int first = 0;
String org = s;
HashSet<Character> hashSet=new HashSet<Character>();
for(int i = 0; i<org.length(); i++){
if(hashSet.contains(org.charAt(i))){
int j;
for(j = first; org.charAt(j) != org.charAt(i); j++){
hashSet.remove(org.charAt(j));
}
max = Math.max(max,i-first);
first = j+1;
}else{
hashSet.add(org.charAt(i));
max = Math.max(max, i-first+1);
}
}
return max;
}
}
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashSet<Character> set = new HashSet<Character>();
int leftBound = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (set.contains(s.charAt(i))) {
while (leftBound < i && s.charAt(leftBound) != s.charAt(i)) {
set.remove(s.charAt(leftBound));
leftBound ++;
}
leftBound ++;
} else {
set.add(s.charAt(i));
max = Math.max(max, i - leftBound + 1);
}
}
return max;
}
}
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int chars[256];
for (int i = 0; i < 256; ++i) chars[i] = -1;
int first = 0, size = 0;
for (decltype(s.size()) i = 0; i < s.size(); ++i){
if (chars[s[i]] >= first) first = chars[s[i]] + 1;
chars[s[i]] = i;
size = (i - first + 1) > size? (i - first + 1):size;
}
return size;
}
};