Description Link to heading
3.longest-substring-withou-repeating-characters
Solution Link to heading
We use arr[96]
to record the number of occurence of each character. If the number is larger than 1
, than there is repeating character between l
and r
; else, update res
, if current length of substr is larger than res
.
Code Link to heading
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int arr[96] = {0};
int res = 0;
int l = 0;
for (int r = 0; r < s.size(); r++) {
arr[s[r] - ' ']++;
if (arr[s[r] - ' '] > 1) {
while (l < r && arr[s[r] - ' '] > 1)
arr[s[l++] - ' ']--;
} else {
res = max(r - l + 1, res);
}
}
return res;
}
};