-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD53_Longest substring with distinct characters.cpp
More file actions
60 lines (49 loc) · 2 KB
/
D53_Longest substring with distinct characters.cpp
File metadata and controls
60 lines (49 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Given a string s, find the length of the longest substring with all distinct characters.
// Examples:
// Input: s = "geeksforgeeks"
// Output: 7
// Explanation: "eksforg" is the longest substring with all distinct characters.
// Input: s = "aaa"
// Output: 1
// Explanation: "a" is the longest substring with all distinct characters.
// Input: s = "abcdefabcbb"
// Output: 6
// Explanation: The longest substring with all distinct characters is "abcdef", which has a length of 6.
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class Solution
{
public:
int longestUniqueSubstr(string &s)
{
unordered_map<char, int> char_map; // To store the last index of each character
int left = 0; // Left pointer of the window
int max_length = 0; // To track the maximum length of substring with distinct characters
for (int right = 0; right < s.length(); ++right)
{
// If the character is already in the current window, move the left pointer
if (char_map.find(s[right]) != char_map.end() && char_map[s[right]] >= left)
{
left = char_map[s[right]] + 1;
}
// Update the last index of the current character
char_map[s[right]] = right;
// Calculate the length of the current window and update the max length
max_length = max(max_length, right - left + 1);
}
return max_length;
}
};
int main()
{
Solution solution;
string s1 = "geeksforgeeks";
cout << "Longest substring length in '" << s1 << "' is: " << solution.longestUniqueSubstr(s1) << endl;
string s2 = "aaa";
cout << "Longest substring length in '" << s2 << "' is: " << solution.longestUniqueSubstr(s2) << endl;
string s3 = "abcdefabcbb";
cout << "Longest substring length in '" << s3 << "' is: " << solution.longestUniqueSubstr(s3) << endl;
return 0;
}