Partition the string in two parts such that both parts have at least k different characters | Java implementation of the approach ; Function to find the partition of the string such that both parts have at least k different characters ; Length of the string ; To check if the current character is already found ; Count number of different characters in the left part ; If current character is not already found , increase cnt by 1 ; If count becomes equal to k , we 've  got the first part, therefore,  store current index and break the loop ; Increment i by 1 ; Clear the map ; Assign cnt as 0 ; If the current character is not already found , increase cnt by 1 ; If cnt becomes equal to k , the second part also have k different characters so break it ; If the second part has less than k different characters , then print " Not ▁ Possible " ; Otherwise print both parts ; Driver code ; Function call
import java . util . * ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . length ; Map < Character , Boolean > has = new HashMap < > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } i ++ ; has . clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { System . out . println ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; while ( i < n ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . toCharArray ( ) , k ) ; } }