Partition string into two substrings having maximum number of common non | Function to count maximum common non - repeating characters that can be obtained by partitioning the string into two non - empty substrings ; Stores count of non - repeating characters present in both the substrings ; Stores distinct characters in left substring ; Stores distinct characters in right substring ; Traverse left substring ; Insert S [ i ] into ls ; Traverse right substring ; Insert S [ i ] into rs ; Traverse distinct characters of left substring ; If current character is present in right substring ; Update cnt ; Return count ; Function to partition the string into two non - empty substrings in all possible ways ; Stores maximum common distinct characters present in both the substring partitions ; Traverse the string ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code
function countCommonChar ( ind , S ) { var cnt = 0 ; var ls = new Set ( ) ; var rs = new Set ( ) ; for ( var i = 0 ; i < ind ; ++ i ) { ls . add ( S [ i ] ) ; } for ( var i = ind ; i < S . length ; ++ i ) { rs . add ( S [ i ] ) ; } ls . forEach ( v => { if ( rs . has ( v ) ) { ++ cnt ; } } ) ; return cnt ; } function partitionStringWithMaxCom ( S ) { var ans = 0 ; for ( var i = 1 ; i < S . length ; ++ i ) { ans = Math . max ( ans , countCommonChar ( i , S ) ) ; } document . write ( ans ) ; } var str = " " ; partitionStringWithMaxCom ( str ) ;