Split a string into maximum number of unique substrings | C # 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
using System ; using System . Collections . Generic ; class GFG { public int maxUniqueSplit ( String S ) { return maxUnique ( S , new HashSet < String > ( ) ) ; } public int maxUnique ( String S , HashSet < 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 ; } } public class GFG { public static void Main ( String [ ] args ) { Solution st = new Solution ( ) ; String S = " ababccc " ; int res = st . maxUniqueSplit ( S ) ; Console . WriteLine ( res ) ; } }