Change Minimum Characters to Satisfy One of Three Conditions

You are given two strings a and b that consist of lowercase letters. In one operation, you can change any character in a or b to any lowercase letter.

Your goal is to satisfy one of the following three conditions:

  • Every letter in a is strictly less than every letter in b in the alphabet.
  • Every letter in b is strictly less than every letter in a in the alphabet.
  • Both a and b consist of only one distinct letter.

Return the minimum number of operations needed to achieve your goal.

Example 1
Inputa = "aba", b = "caa"
Output2
The best way takes 2 operations, either by making every letter in a strictly less than every letter in b or by making both strings consist of one distinct letter.
Example 2
Inputa = "dabadd", b = "cda"
Output3
The best way is to make condition 1 true by changing b to "eee".

Constraints

  • 1 <= a.length, b.length <= 10^5
  • a and b consist only of lowercase letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate