Skip to content

Commit 7c02489

Browse files
Update Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp
Co-authored-by: Shamith <[email protected]>
1 parent 8529473 commit 7c02489

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,63 @@
11
#include <iostream>
2-
#include <unordered_map>
32
#include <string>
3+
#include <unordered_map>
44
using namespace std;
55

6-
// Function to find the length of the longest substring without repeating characters
6+
// Function to find the length of the longest substring without repeating
7+
// characters
78
int lengthOfLongestSubstring(const string &s) {
8-
unordered_map<char, int> charIndexMap;
9-
int maxLength = 0;
10-
int start = 0;
11-
12-
for (int end = 0; end < s.length(); end++) {
13-
char currentChar = s[end];
14-
15-
// If the character is already in the map and is within the current window
16-
if (charIndexMap.find(currentChar) != charIndexMap.end() && charIndexMap[currentChar] >= start) {
17-
start = charIndexMap[currentChar] + 1;
18-
}
19-
20-
// Update the last seen index of the current character
21-
charIndexMap[currentChar] = end;
22-
23-
// Calculate the max length of substring
24-
maxLength = max(maxLength, end - start + 1);
9+
unordered_map<char, int> charIndexMap;
10+
int maxLength = 0;
11+
int start = 0;
12+
13+
for (int end = 0; end < s.length(); end++) {
14+
char currentChar = s[end];
15+
16+
// If the character is already in the map and is within the current window
17+
if (charIndexMap.find(currentChar) != charIndexMap.end() &&
18+
charIndexMap[currentChar] >= start) {
19+
start = charIndexMap[currentChar] + 1;
2520
}
26-
return maxLength;
21+
22+
// Update the last seen index of the current character
23+
charIndexMap[currentChar] = end;
24+
25+
// Calculate the max length of substring
26+
maxLength = max(maxLength, end - start + 1);
27+
}
28+
return maxLength;
2729
}
2830

2931
int main() {
30-
string input;
31-
cout << "Enter a string: ";
32-
cin >> input;
32+
string input;
33+
cout << "Enter a string: ";
34+
cin >> input;
3335

34-
int result = lengthOfLongestSubstring(input);
35-
cout << "The length of the longest substring without repeating characters is: " << result << endl;
36+
int result = lengthOfLongestSubstring(input);
37+
cout
38+
<< "The length of the longest substring without repeating characters is: "
39+
<< result << endl;
3640

37-
return 0;
41+
return 0;
3842
}
3943

4044
/*
4145
Example:
4246
Input: "abcabcbb"
4347
Explanation:
44-
- The longest substring without repeating characters is "abc", which has a length of 3.
48+
- The longest substring without repeating characters is "abc", which has a
49+
length of 3.
4550
- As we iterate through the string:
4651
- Start with "a" -> length 1
4752
- Add "b" -> "ab" -> length 2
4853
- Add "c" -> "abc" -> length 3
49-
- "a" repeats, so move the start to the next character -> "bca" (length still 3)
54+
- "a" repeats, so move the start to the next character -> "bca" (length
55+
still 3)
5056
- Continue until the end, with the longest substring remaining "abc".
5157
Output: 3
5258
5359
Input: "bbbbb"
5460
Explanation:
55-
- The longest substring without repeating characters is "b", which has a length of 1.
56-
Output: 1
61+
- The longest substring without repeating characters is "b", which has a length
62+
of 1. Output: 1
5763
*/

0 commit comments

Comments
 (0)