Minimum cuts required to convert a palindromic string to a different palindromic string | C # program to solve the above problem ; Function to check if string is palindrome or not ; Function to check if it is possible to get result by making just one cut ; Appending last element in front ; Removing last element ; Checking whether string s2 is palindrome and different from s . ; 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 ; Return 1 if it is possible to get palindromic string in just one cut . Else we can always reached in two cuttings . ; Driver Code
using System ; using System . Linq ; class GFG { static bool isPalindrome ( string s ) { for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] != s [ s . Length - i - 1 ] ) { return false ; } } return true ; } static bool ans ( string s ) { string s2 = s ; for ( int i = 0 ; i < s . Length ; ++ i ) { s2 = s2 [ s2 . Length - 1 ] + s2 ; s2 = s2 . Substring ( 0 , s2 . Length - 1 ) ; if ( ( s == null ? s2 != null : ! s . Equals ( s2 ) ) && isPalindrome ( s2 ) ) { return true ; } } return false ; } static int solve ( string s ) { if ( s . Length <= 3 ) { return - 1 ; } int [ ] cnt = new int [ 25 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } if ( cnt . Max ( ) >= ( s . Length - 1 ) ) { return - 1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } static void Main ( ) { string s = " nolon " ; Console . WriteLine ( solve ( s ) ) ; } }