Length of the longest substring without repeating characters | C ++ program to find the length of the longest substring without repeating characters ; result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestUniqueSubsttr ( string str ) { int n = str . size ( ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { vector < bool > visited ( 256 ) ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str [ j ] ] == true ) break ; else { res = max ( res , j - i + 1 ) ; visited [ str [ j ] ] = true ; } } visited [ str [ i ] ] = false ; } return res ; } int main ( ) { string str = " geeksforgeeks " ; cout << " The ▁ input ▁ string ▁ is ▁ " << str << endl ; int len = longestUniqueSubsttr ( str ) ; cout << " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ " " character ▁ substring ▁ is ▁ " << len ; return 0 ; }