Minimum cuts required to convert a palindromic string to a different palindromic string | Recursive function to find minimum number of cuts if length of String is even ; If length is odd then return 2 ; To check if half of palindromic String is itself a palindrome ; If not then return 1 ; Else call function with half palindromic String ; Function to find minimum number of cuts If length of String is odd ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; If length is even ; If length is odd ; Driver Code
function solveEven ( s ) { if ( s . length % 2 == 1 ) { return 2 ; } let ls = s . substring ( 0 , s . length / 2 ) ; let rs = s . substring ( s . length / 2 , s . length ) ; if ( ls != rs ) { return 1 ; } return solveEven ( ls ) ; } function solveOdd ( s ) { return 2 ; } function solve ( s ) { if ( s . length <= 3 ) { return - 1 ; } let cnt = new Array ( 25 ) ; for ( let i = 0 ; i < 25 ; i ++ ) cnt [ i ] = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { cnt [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( Math . max ( ... cnt ) >= s . length - 1 ) { return - 1 ; } if ( s . length % 2 == 0 ) { return solveEven ( s ) ; } if ( s . length % 2 == 1 ) { return solveOdd ( s ) ; } return Number . MIN_VALUE ; } let s = " " ; document . write ( solve ( s ) ) ;