Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vishnusureshperumbavoor/b205f756b3acf5c3ae4a9512e2abb9f2 to your computer and use it in GitHub Desktop.
Save vishnusureshperumbavoor/b205f756b3acf5c3ae4a9512e2abb9f2 to your computer and use it in GitHub Desktop.
Tff
#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