Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; Starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestUniqueSubsttr ( string s ) { map < char , int > seen ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . length ( ) ; end ++ ) { if ( seen . find ( s [ end ] ) != seen . end ( ) ) { start = max ( start , seen [ s [ end ] ] + 1 ) ; } seen [ s [ end ] ] = end ; maximum_length = max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } int main ( ) { string s = " geeksforgeeks " ; cout << " The ▁ input ▁ String ▁ is ▁ " << s << endl ; int length = longestUniqueSubsttr ( s ) ; cout << " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ " << " substring ▁ is ▁ " << length ; }