// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum of
// the three numbers
static int getMin(int x, int y, int z)
{
return Math.Min(Math.Min(x, y), z);
}
// Function to find the minimum number
// operations required to convert String
// str1 to str2 using the operations
static int editDistance(string str1, string str2,
int m, int n)
{
// Stores the results of subproblems
int [,]dp = new int[m + 1,n + 1];
// Fill dp[,] in bottom up manner
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
// If str1 is empty, then
// insert all characters
// of String str2
if (i == 0)
// Minimum operations
// is j
dp[i,j] = j;
// If str2 is empty, then
// remove all characters
// of String str2
else if (j == 0)
// Minimum operations
// is i
dp[i,j] = i;
// If the last characters
// are same, then ignore
// last character
else if (str1[i - 1] == str2[j - 1])
dp[i,j] = dp[i - 1,j - 1];
// If the last character
// is different, then
// find the minimum
else {
// Perform one of the
// insert, remove and
// the replace
dp[i,j] = 1
+ getMin(
dp[i,j - 1],
dp[i - 1,j],
dp[i - 1,j - 1]);
}
}
}
// Return the minimum number of
// steps required
return dp[m,n];
}
// Function to find the minimum number
// of steps to modify the String such
// that first half and second half
// becomes the same
static void minimumSteps(string S, int N)
{
// Stores the minimum number of
// operations required
int ans = int.MaxValue;
// Traverse the given String S
for (int i = 1; i < N; i++) {
string S1 = S.Substring(0, i);
string S2 = S.Substring(i);
// Find the minimum operations
int count = editDistance(
S1, S2, S1.Length,
S2.Length);
// Update the ans
ans = Math.Min(ans, count);
}
// Print the result
Console.Write(ans);
}
// Driver Code
public static void Main(string[] args)
{
string S = "aabb";
int N = S.Length;
minimumSteps(S, N);
}
}
// This code is contributed by AnkThon