using
System;
class
GFG
{
static
int
findLongestSubstring(
int
posA,
int
posB, String A,
String B,
int
canSkip,
int
n,
int
[, , ] dp)
{
if
(posA >= n || posB >= n) {
return
0;
}
if
(dp[posA, posB, canSkip] != -1) {
return
dp[posA, posB, canSkip];
}
int
op1 = 0;
int
op2 = 0;
int
op3 = 0;
if
(A[posA] == B[posB]) {
op1 = 1
+ findLongestSubstring(posA + 1, posB + 1,
A, B, 0, n, dp);
}
if
(canSkip == 1) {
op2 = findLongestSubstring(posA + 1, posB, A, B,
canSkip, n, dp);
}
op3 = findLongestSubstring(posA, posB + 1, A, B,
canSkip, n, dp);
return
dp[posA, posB, canSkip]
= Math.Max(op1, Math.Max(op2, op3));
}
static
void
minOperations(String A, String B,
int
N)
{
int
[, , ] dp =
new
int
[N, N, 2];
for
(
int
i = 0; i < N; i++)
{
for
(
int
j = 0; j < N; j++)
{
for
(
int
k = 0; k < 2; k++)
{
dp[i, j, k] = -1;
}
}
}
Console.WriteLine(
N - findLongestSubstring(0, 0, A, B, 1, N, dp));
}
static
public
void
Main ()
{
String A =
"abab"
;
String B =
"baba"
;
int
N = A.Length;
minOperations(A, B, N);
}
}