Two strings, primary and secondary, describe commit streams. Each lowercase letter is a commit priority, with alphabetically smaller letters representing higher priority. Merge the strings into one sequence while preserving the original relative order of every character from each input.
A conflict is a pair of positions in which a lower-priority commit appears before a higher-priority commit. In letter terms, positions i and j conflict when i < j and the letter at i is greater than the letter at j. Return the minimum possible number of conflicts over all valid merges.
Function Signature
def getMinimumConflicts(primary: str, secondary: str) -> int:
Rules
-
Every character occurrence from both strings appears exactly once in the merge.
-
Characters from either one string cannot be reordered relative to each other.
-
Equal letters have equal priority and do not conflict.
-
Return the minimum count, not a particular merged string.
Constraints
-
1 <= len(primary), len(secondary) <= 1000
.
-
Both inputs contain only lowercase ASCII letters.
-
The answer is at most
1999000
, which fits a signed 32-bit integer.
Examples
Input: primary = "zc", secondary = "d"
Output: 2
The legal merges are zcd, zdc, and dzc. Their conflict counts are two, three, and two respectively.
Input: primary = "ab", secondary = "ab"
Output: 0
The merge aabb preserves both input orders and contains no conflicting pair.