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
def longestUniqueSubsttr ( string ) : NEW_LINE INDENT seen = { } NEW_LINE maximum_length = 0 NEW_LINE start = 0 NEW_LINE for end in range ( len ( string ) ) : NEW_LINE INDENT if string [ end ] in seen : NEW_LINE INDENT start = max ( start , seen [ string [ end ] ] + 1 ) NEW_LINE DEDENT seen [ string [ end ] ] = end NEW_LINE maximum_length = max ( maximum_length , end - start + 1 ) NEW_LINE DEDENT return maximum_length NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE print ( " The ▁ input ▁ string ▁ is " , string ) NEW_LINE length = longestUniqueSubsttr ( string ) NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is " , length ) NEW_LINE