A text-repair tool transforms a source string s into a target string t using three kinds of single-character operations, each with its own fixed cost: inserting one character anywhere, deleting one character, or substituting one character for a different character. Given the three costs and the two strings, find the minimum total cost of a sequence of operations that turns s into t. Substituting a character for itself is never necessary and is not considered a valid use of the substitute operation.
Line 1: three integers insCost, delCost, subCost.
Line 2: the source string s (may be empty, producing a blank line).
Line 3: the target string t (may be empty, producing a blank line).
Both s and t contain only lowercase English letters.
A single integer: the minimum total cost to transform s into t.
Example 1
Input
1 1 10 cat cot
Expected
2
Explanation
Substituting is expensive (cost 10), but deleting 'a' (cost 1) and inserting 'o' (cost 1) achieves the same result for a total of 2, which is cheaper than one substitution.
Example 2
Input
5 5 1 abc abd
Expected
1
Explanation
Substituting the last character 'c' for 'd' costs only 1, far cheaper than deleting and inserting at cost 5 each.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →