Maximized partitions of a string such that each character of the string appears in one substring | C # program for the above approach ; Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s List < int > ans = new List < int > ( ) ; ; 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 respective character to the string ; Store the partition 's  len and reset variables ; Update the minp and str ; Driver Code ; Input string ; Function call
using System ; using System . Collections . Generic ; class GFG { public static void print_substring ( String s ) { int n = s . Length ; String str = " " ; if ( n == 0 ) { Console . Write ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) last_pos [ i ] = - 1 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] - ' a ' ] == - 1 ) { last_pos [ s [ i ] - ' a ' ] = i ; } } int minp = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s [ i ] - ' a ' ] ; minp = Math . Max ( minp , lp ) ; if ( i == minp ) { str += s [ i ] ; Console . Write ( str + ' ▁ ' ) ; minp = - 1 ; str = " " ; } else { str += s [ i ] ; } } } public static void Main ( String [ ] args ) { String S = " ababcbacadefegdehijhklij " ; print_substring ( S ) ; } }