Minimum Merge Conflicts
Problem statement
Developers want to merge two source-control branches into one unified branch while preserving the relative order of commits from each branch.
Each branch is represented by a lowercase string. Each character represents a commit priority, where a lower alphabetical character has higher priority.
A conflict occurs when, in the merged branch, a lower-priority commit appears before a higher-priority commit. In other words, for positions i < j in the merged string, there is a conflict when merged[i] > merged[j].
Return the minimum possible number of conflicts over all valid merges of primary and secondary.
A valid merge must contain every character from both branches and preserve the original order within each input branch.
Function
getMinimumConflicts(primary: String, secondary: String) → int The function getMinimumConflicts takes the following input:
string primary: the commit sequence of the primary branchstring secondary: the commit sequence of the secondary branch
Returns int: the minimum number of conflicts after a valid merge
Examples
Example 1
primary = "zc"secondary = "d"return = 2Valid merges include:
zcdwith2merge conflicts (zbeing lower priority is placed before higher priority commitscandd).- Similarly,
zdcwith3merge conflicts. - Similarly,
dzcwith2merge conflicts.
The minimum number of merge conflicts possible is 2.
Example 2
primary = "dae"secondary = "add"return = 1adadde has 1 merge conflict.
Example 3
primary = "aaa"secondary = "abb"return = 0aaaabb has no merge conflicts.
Added from a newer source found on June 24, 2026 🦒
Constraints
1 <= |primary|, |secondary| <= 1000primaryandsecondaryconsist of lowercase English letters only.