Length of the longest substring without repeating characters | This functionr eturns true if all characters in strr [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code
def areDistinct ( strr , i , j ) : NEW_LINE INDENT visited = [ 0 ] * ( 26 ) NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT if ( visited [ ord ( strr [ k ] ) - ord ( ' a ' ) ] == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT visited [ ord ( strr [ k ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT return True NEW_LINE DEDENT def longestUniqueSubsttr ( strr ) : NEW_LINE INDENT n = len ( strr ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT if ( areDistinct ( strr , i , j ) ) : NEW_LINE INDENT res = max ( res , j - i + 1 ) NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " geeksforgeeks " NEW_LINE print ( " The ▁ input ▁ is ▁ " , strr ) NEW_LINE len = longestUniqueSubsttr ( strr ) NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " " non - repeating ▁ character ▁ substring ▁ is ▁ " , len ) NEW_LINE DEDENT