|
1 | 1 | #include <iostream> |
2 | | -#include <unordered_map> |
3 | 2 | #include <string> |
| 3 | +#include <unordered_map> |
4 | 4 | using namespace std; |
5 | 5 |
|
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 |
7 | 8 | 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; |
25 | 20 | } |
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; |
27 | 29 | } |
28 | 30 |
|
29 | 31 | int main() { |
30 | | - string input; |
31 | | - cout << "Enter a string: "; |
32 | | - cin >> input; |
| 32 | + string input; |
| 33 | + cout << "Enter a string: "; |
| 34 | + cin >> input; |
33 | 35 |
|
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; |
36 | 40 |
|
37 | | - return 0; |
| 41 | + return 0; |
38 | 42 | } |
39 | 43 |
|
40 | 44 | /* |
41 | 45 | Example: |
42 | 46 | Input: "abcabcbb" |
43 | 47 | 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. |
45 | 50 | - As we iterate through the string: |
46 | 51 | - Start with "a" -> length 1 |
47 | 52 | - Add "b" -> "ab" -> length 2 |
48 | 53 | - 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) |
50 | 56 | - Continue until the end, with the longest substring remaining "abc". |
51 | 57 | Output: 3 |
52 | 58 |
|
53 | 59 | Input: "bbbbb" |
54 | 60 | 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 |
57 | 63 | */ |
0 commit comments