Lengths of maximized partitions of a string such that each character of the string appears in one substring | Function to find the length of all partitions oof a string such that each characters occurs in a single substring ; Stores last index of string s ; Find the last position of each letter in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Given string str ; Function call
def partitionString ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = [ ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_pos = [ - 1 ] * 26 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] = i NEW_LINE DEDENT DEDENT minp = - 1 NEW_LINE plen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lp = last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] NEW_LINE minp = max ( minp , lp ) NEW_LINE plen += 1 NEW_LINE if ( i == minp ) : NEW_LINE INDENT ans . append ( plen ) NEW_LINE minp = - 1 NEW_LINE plen = 0 NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT str = " acbbcc " NEW_LINE partitionString ( str ) NEW_LINE