Split a string into maximum number of unique substrings | Java program for the above approach ; Function to find the maximum count of unique substrings by splitting a string into maximum number of unique substrings ; Utility function to find maximum count of unique substrings by splitting the string ; Stores maximum count of unique substring by splitting the string into substrings ; Iterate over the characters of the string ; Stores prefix substring ; Check if the current substring already exists ; Insert tmp into set ; Recursively call for remaining characters of string ; Remove from the set ; Return answer ; Driver Code
import java . io . * ; import java . util . * ; class Solution { public int maxUniqueSplit ( String S ) { return maxUnique ( S , new HashSet < String > ( ) ) ; } public int maxUnique ( String S , Set < String > set ) { int max = 0 ; for ( int i = 1 ; i <= S . length ( ) ; i ++ ) { String tmp = S . substring ( 0 , i ) ; if ( ! set . contains ( tmp ) ) { set . add ( tmp ) ; max = Math . max ( max , maxUnique ( S . substring ( i ) , set ) + 1 ) ; set . remove ( tmp ) ; } } return max ; } } class GFG { public static void main ( String [ ] args ) { Solution st = new Solution ( ) ; String S = " ababccc " ; int res = st . maxUniqueSplit ( S ) ; System . out . println ( res ) ; } }