-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_substring_length.cpp
More file actions
24 lines (21 loc) · 931 Bytes
/
longest_substring_length.cpp
File metadata and controls
24 lines (21 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <map>
#include <cmath>
int LongestSubstringLength(string s) {
int len = s.length();
unordered_map<char, int> charMap; // unordered map to store character and its recent index
int left = 0;
int longestSubstringLength = 0; // compute max substring length encountered till now
for (int right = 0; right < len; right++) {
if (charMap.count(s[right]) == 0 || charMap[s[right]] < left) { //check if current character is already part of substring
charMap[s[right]] = right;
longestSubstringLength = max(longestSubstringLength, right - left + 1); // update max substring length
} else {
left = charMap[s[right]] + 1; // consider new substring by updating left
charMap[s[right]] = right;
}
}
return longestSubstringLength;
}