Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Convert String III

Try to solve the Minimum Cost to Convert String III problem.

Statement

You are given two strings source and target of equal length, consisting of lowercase English letters.

You are also given a 22D string array rules, where rules[i] = [pattern_i, replacement_i], and an integer array costs of equal length, where costs[i] denotes the base cost of applying rules[i].

In a single operation, you can choose a substring of source that matches pattern_i and replace it with replacement_i. A substring matches pattern_i if they have the same length and for every index, the character in pattern_i is either identical to the corresponding character in the substring or is the wildcard character '*'. Each wildcard character '*' matches any single lowercase English letter.

The total cost of applying rules[i] is costs[i] plus the number of '*' characters in pattern_i.

You can apply operations to any number of disjoint substrings of source. Any character in source that is not modified must already be identical to the character at the corresponding index in target with a cost of 00.

Return the minimum total cost to transform source into target, or −1-1 if it is impossible.

Constraints:

  • 1≤1 \leq source.length == target.length ≤5000\leq 5000

  • source and target consist of lowercase English letters.

  • 1≤1 \leq rules.length == costs.length ≤200\leq 200

  • rules[i] = [pattern_i, replacement_i]

  • 1≤1 \leq pattern_i.length == replacement_i.length ≤20\leq 20

  • pattern_i contains at least one lowercase English letter and at most 55 '*' characters.

  • replacement_i contains only lowercase English letters.

  • 1≤1 \leq costs[i] ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Convert String III

Try to solve the Minimum Cost to Convert String III problem.

Statement

You are given two strings source and target of equal length, consisting of lowercase English letters.

You are also given a 22D string array rules, where rules[i] = [pattern_i, replacement_i], and an integer array costs of equal length, where costs[i] denotes the base cost of applying rules[i].

In a single operation, you can choose a substring of source that matches pattern_i and replace it with replacement_i. A substring matches pattern_i if they have the same length and for every index, the character in pattern_i is either identical to the corresponding character in the substring or is the wildcard character '*'. Each wildcard character '*' matches any single lowercase English letter.

The total cost of applying rules[i] is costs[i] plus the number of '*' characters in pattern_i.

You can apply operations to any number of disjoint substrings of source. Any character in source that is not modified must already be identical to the character at the corresponding index in target with a cost of 00.

Return the minimum total cost to transform source into target, or −1-1 if it is impossible.

Constraints:

  • 1≤1 \leq source.length == target.length ≤5000\leq 5000

  • source and target consist of lowercase English letters.

  • 1≤1 \leq rules.length == costs.length ≤200\leq 200

  • rules[i] = [pattern_i, replacement_i]

  • 1≤1 \leq pattern_i.length == replacement_i.length ≤20\leq 20

  • pattern_i contains at least one lowercase English letter and at most 55 '*' characters.

  • replacement_i contains only lowercase English letters.

  • 1≤1 \leq costs[i] ≤1000\leq 1000