Palindrome Partitioning | DP | Driver code
function minCut ( a ) { var cut = new Array ( a . length ) ; var palindrome = new Array ( a . length ) ; for ( var i = 0 ; i < a . length ; i ++ ) { var minCut = i ; for ( var j = 0 ; j <= i ; j ++ ) { if ( a . charAt ( i ) == a . charAt ( j ) && ( i - j < 5 palindrome [ j + 1 ] [ i - 1 ] ) ) { palindrome [ j , i ] = true ; minCut = Math . min ( minCut , j == 0 ? 0 : ( cut [ j - 1 ] + 1 ) ) ; } } cut [ i ] = minCut ; } return cut [ a . length - 1 ] ; } document . write ( minCut ( " " ) + " " ) ; document . write ( minCut ( " " ) ) ;