554. Equalize Strings by Adding or Removing Characters at Ends
Given two strings, initial and target. The task is to modify initial by performing a series of operations to make it equal to target.
In one operation:
- Add one character only at the beginning or the end of initial.
- Remove one character only from the beginning or the end of initial.
Return the minimum number of operations required to transform initial into target.
Example 1:
Input: initial = "abcdef", target = "defgh"
Output: 5
Explanation:
Remove 'a', 'b', 'c' from the beginning → "def"
Add 'g' and 'h' to the end → "defgh"
Example 2:
Input: initial = "pqrrt", target = "rtsp"
Output: 5
Explanation:
Remove from beginning ->Â "qrrt"
Remove from beginning ->Â "rrt"
Remove from beginning ->Â "rt"
Add 's' to end ->Â "rts"
Add 'p' to end ->Â "rtsp"
Now Your Turn!
Pick the correct output for the given inputInput: initial = "mnop", target = "mnopqr"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= initial.length, target.length <= 1000
- initial and target consist only of lowercase English letters.