Maximized partitions of a string such that each character of the string appears in one substring | Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s ; Find the last position of each character 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 ; If the current pos of character equals the min pos then the end of partition ; Add the respective character first ; Store the partition 's  len and reset variables ; Update the minp and str ; Input string ; Function call
function print_substring ( s ) { let n = s . length ; let str = " " ; let ans = [ ] ; if ( n == 0 ) { document . write ( " " ) ; return ; } let last_pos = Array ( 26 ) . fill ( - 1 ) ; for ( let i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] == - 1 ) { last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = i ; } } let minp = - 1 ; for ( let i = 0 ; i < n ; ++ i ) { let lp = last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; minp = Math . max ( minp , lp ) ; if ( i == minp ) { str += s [ i ] ; document . write ( str + ' ' ) ; minp = - 1 ; str = " " ; } else { str += s [ i ] ; } } } let S = " " ; print_substring ( S ) ;