#NEWLINE# def lengthOfLongestSubstringKDistinct( self , s , k ) : #NEWLINE# #INDENT# '\n        :type s: str\n        :type k: int\n        :rtype: int\n        ' #NEWLINE# ( longest , start , distinct_count , visited ) = ( 0 , 0 , 0 , [ 0 for _ in xrange ( 256 ) ] ) #NEWLINE# for ( i , char ) in enumerate ( s ) : #NEWLINE# #INDENT# if ( visited [ ord ( char ) ] == 0 ) : #NEWLINE# #INDENT# distinct_count += 1 #NEWLINE# #UNINDENT# visited [ ord ( longest ) ] += 1 #NEWLINE# while ( distinct_count > k ) : #NEWLINE# #INDENT# visited [ ord ( s [ start ] ) ] -= 1 #NEWLINE# if ( visited [ ord ( s [ start ] ) ] == 0 ) : #NEWLINE# #INDENT# distinct_count -= 1 #NEWLINE# #UNINDENT# start += 1 #NEWLINE# #UNINDENT# longest = max ( longest , ( ( i - start ) + 1 ) ) #NEWLINE# #UNINDENT# return longest