Solution Code
C#
public class Solution {
public int LengthOfLongestSubstring(string s) {
int[] mark = new int[500];
int len = s.Length, en = 0, mx = 0, cnt = 0;
for (int st = 0; st < len; st++) {
if (mark[s[st]] > 0) {
for (; en < st; en++) {
if (s[en] == s[st]) {
en++;
break;
}
cnt--;
mark[s[en]]--;
}
} else {
cnt++;
mark[s[st]]++;
mx = Math.Max(mx, cnt);
}
}
return mx;
}
}
C++
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int mark[500] = {0}, len = s.size(), en = 0, mx = 0, cnt = 0;
for (int st = 0; st < len; st++) {
if (mark[s[st]] > 0) {
for (; en < st; en++) {
if (s[en] == s[st]) {
en++;
break;
}
cnt--;
mark[s[en]]--;
}
} else {
cnt++;
mark[s[st]]++;
mx = max(mx, cnt);
}
}
return mx;
}
};
Java
class Solution {
public int lengthOfLongestSubstring(String s) {
int[] mark = new int[500];
int len = s.length(), en = 0, mx = 0, cnt = 0;
for (int st = 0; st < len; st++) {
if (mark[s.charAt(st)] > 0) {
for (; en < st; en++) {
if (s.charAt(en) == s.charAt(st)) {
en++;
break;
}
cnt--;
mark[s.charAt(en)]--;
}
} else {
cnt++;
mark[s.charAt(st)]++;
mx = Math.max(mx, cnt);
}
}
return mx;
}
}