Class Solution
-
- All Implemented Interfaces:
public final class Solution1737 - Change Minimum Characters to Satisfy One of Three Conditions.
Medium
You are given two strings
aandbthat consist of lowercase letters. In one operation, you can change any character inaorbto any lowercase letter.Your goal is to satisfy one of the following three conditions:
Every letter in
ais strictly less than every letter inbin the alphabet.Every letter in
bis strictly less than every letter inain the alphabet.Both
aandbconsist of only one distinct letter.
Return the minimum number of operations needed to achieve your goal.
Example 1:
Input: a = "aba", b = "caa"
Output: 2
Explanation: Consider the best way to make each condition true:
Change b to "ccc" in 2 operations, then every letter in a is less than every letter in b.
Change a to "bbb" and b to "aaa" in 3 operations, then every letter in b is less than every letter in a.
Change a to "aaa" and b to "aaa" in 2 operations, then a and b consist of one distinct letter. The best way was done in 2 operations (either condition 1 or condition 3).
Example 2:
Input: a = "dabadd", b = "cda"
Output: 3
Explanation: The best way is to make condition 1 true by changing b to "eee".
Constraints:
<code>1 <= a.length, b.length <= 10<sup>5</sup></code>
aandbconsist only of lowercase letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminCharacters(String a, String b)-
-
Method Detail
-
minCharacters
final Integer minCharacters(String a, String b)
-
-
-
-