Maximized partitions of a string such that each character of the string appears in one substring | Java program for the above approach ; 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 ; Driver Code ; Input string ; Function call
import java . util . * ; class GFG { public static void print_substring ( String s ) { int n = s . length ( ) ; String str = " " ; Vector < Integer > ans = new Vector < Integer > ( ) ; if ( n == 0 ) { System . out . print ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; Arrays . fill ( last_pos , - 1 ) ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s . charAt ( i ) - ' a ' ] == - 1 ) { last_pos [ s . charAt ( i ) - ' a ' ] = i ; } } int minp = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s . charAt ( i ) - ' a ' ] ; minp = Math . max ( minp , lp ) ; if ( i == minp ) { str += s . charAt ( i ) ; System . out . print ( str + ' ▁ ' ) ; minp = - 1 ; str = " " ; } else { str += s . charAt ( i ) ; } } } public static void main ( String [ ] args ) { String S = " ababcbacadefegdehijhklij " ; print_substring ( S ) ; } }