#1737
Change Minimum Characters to Satisfy One of Three Conditions
expert · 1095 · lc medium +32 · verified · 37.8% accepted · 337 likes · top 16%
Description
You are given two lowercase strings a and b. In one operation change any character in a or b. Reach one of three states using the fewest operations:
- Every character in a is strictly earlier in the alphabet than every character in b.
- Every character in b is strictly earlier in the alphabet than every character in a.
- Both strings consist entirely of a single repeated letter.
Return the minimum operations needed.
Example 1:
Input: a = "aba", b = "caa"
Output: 2
Explanation: Consider the best way to make each condition true:
1) Change b to "ccc" in 2 operations, then every letter in a is less than every letter in b.
2) Change a to "bbb" and b to "aaa" in 3 operations, then every letter in b is less than every letter in a.
3) 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".
Code
1
2
3