Suppose we have two lowercase strings s and t, now consider an operation where we can delete any character in any of these two strings. We have to find the minimum number of operations needed to make s and t equal.
So, if the input is like s = "pipe" t = "ripe", then the output will be 2, because we can delete "p" from s and "r" from t to make these strings same "ipe"
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(s, t): m = len(s) n = len(t) def dp(i, j): if i == m: return n - j elif j == n: return m - i else: if s[i] == t[j]: return dp(i + 1, j + 1) else: return 1 + min(dp(i + 1, j), dp(i, j + 1)) return dp(0, 0) s = "pipe" t = "ripe" print(solve(s, t))
"pipe", "ripe"
2