Minimum Insertions or Deletions to Convert String A to B

Given nonempty strings str1 and str2, return the minimum number of insertions and deletions required to transform str1 into str2. Each operation inserts or…

Given nonempty strings str1 and str2, return the minimum number of insertions and deletions required to transform str1 into str2. Each operation inserts or deletes one Unicode scalar at any position. Replacement is not a permitted single operation. Return only the total count. Characters are Unicode scalar values, not UTF-8 bytes, UTF-16 code units or visual grapheme clusters. Combining marks count separately. Do not normalize, case-fold, trim or ignore any scalar. Unpaired surrogates are outside the input domain.

- 1 <= scalar length of each operand <= 1000. - All Unicode scalar values are allowed; no unpaired surrogates. - The visualizer accepts each operand up to 8 scalars; longer valid inputs are coding-only.

Example 1: input str1 = "kitten", str2 = "sitting"; output 5. Delete k, insert s, delete e, insert i before n, then insert g at the end. The intermediate strings are itten → sitten → sittn → sittin → sitting.

Example 2: input str1 = "flaw", str2 = "lawn"; output 2. Delete f and insert n at the end; the ordered subsequence law remains unchanged.

Example 3: input str1 = "a", str2 = "b"; output 2. Delete a and insert b. Counting a replacement as one would solve a different problem.

Category: Strings. Topic: dynamic-programming. Difficulty: hard. Patterns: dynamic-programming.