Watch 7 video solutions for Change Minimum Characters to Satisfy One of Three Conditions, a medium level problem involving Hash Table, String, Counting. This walkthrough by Happy Coding has 1,304 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
a is strictly less than every letter in b in the alphabet.b is strictly less than every letter in a in the alphabet.a and b consist 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: 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".
Constraints:
1 <= a.length, b.length <= 105a and b consist only of lowercase letters.Problem Overview: You get two lowercase strings a and b. Modify the minimum number of characters so one of three conditions holds: every character in a is strictly less than every character in b, every character in b is strictly less than every character in a, or both strings contain only one distinct character.
The constraints are small for the alphabet (26 letters) but large for string length. That means the right strategy focuses on frequency counting instead of comparing characters directly.
Approach 1: Direct Count and Compare (O(n + 26), space O(26))
Start by building two frequency arrays of size 26 for a and b. Each index represents a letter from 'a' to 'z'. With these counts, evaluate the three required conditions.
For the third condition (both strings use a single identical character), iterate through all 26 letters and compute how many characters must change so both strings become that letter. The minimum across all letters is the cost.
For the first two conditions, test every possible split character c where letters in one string must be <= c and the other > c. Count how many characters violate this rule using the frequency arrays. This approach relies on simple counting and works well because the alphabet size is constant.
Approach 2: Prefix Sums for Strictly Less Condition (O(n + 26), space O(26))
This approach improves the evaluation of lexicographic conditions using prefix sums over the frequency arrays. After counting characters, build prefix sums where prefix[i] stores the number of characters with index ≤ i.
For a split character i, you instantly know how many characters in a are ≤ i and how many in b are ≤ i. From this you derive how many characters violate the strictly less requirement. Iterate over all 25 possible split points (a–y) and compute the required modifications.
Prefix sums remove repeated counting work and make the condition checks constant time per letter. The total complexity stays linear in the string length.
Both strategies rely heavily on character frequencies, making them classic applications of counting and prefix sum techniques on string data.
Recommended for interviews: The prefix-sum counting approach. Interviewers expect you to reduce the problem to frequency arrays over the 26-letter alphabet and evaluate split points efficiently. Starting with direct counting shows understanding, but using prefix sums demonstrates stronger algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Count and Compare | O(n + 26) | O(26) | Simple implementation when using frequency arrays to evaluate all conditions directly. |
| Prefix Sums for Strictly Less Condition | O(n + 26) | O(26) | Preferred approach when efficiently evaluating lexicographic split points using cumulative counts. |