Minimum operation require to make first and last character same | C # program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver Code ; Function call
using System ; class GFG { static int MAX = Int32 . MaxValue ; static int minOperation ( string s , int i , int j , int count ) { if ( ( i >= s . Length && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] ) return count ; if ( i >= s . Length ) return minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) return minOperation ( s , i + 1 , j , count + 1 ) ; else return Math . Min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } public static void Main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . Length - 1 , 0 ) ; if ( ans == MAX ) Console . Write ( - 1 ) ; else Console . Write ( ans ) ; } }