Created
October 12, 2023 08:08
-
-
Save vishnusureshperumbavoor/b205f756b3acf5c3ae4a9512e2abb9f2 to your computer and use it in GitHub Desktop.
Tff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <unordered_map> | |
using namespace std; // Add this line | |
int lengthOfLongestSubstring(string s) { | |
int n = s.length(); | |
int maxLength = 0; | |
unordered_map<char, int> charIndexMap; | |
int left = 0; | |
for (int right = 0; right < n; right++) { | |
if (charIndexMap.find(s[right]) != charIndexMap.end()) { | |
// Update the left boundary of the window to skip the repeated character | |
left = max(left, charIndexMap[s[right]] + 1); | |
} | |
charIndexMap[s[right]] = right; // Store the index of the character | |
// Update the maximum length if needed | |
maxLength = max(maxLength, right - left + 1); | |
} | |
return maxLength; | |
} | |
int main() { | |
string input = "abcabcbb"; | |
int result = lengthOfLongestSubstring(input); | |
cout << "Longest substring without repeating characters: " << result << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment