Change Minimum Characters to Satisfy One of Three Conditions - Problem

You are given two strings a and b consisting only of lowercase English letters. Your mission is to transform these strings with the minimum number of character changes to satisfy one of three winning conditions:

  1. Alphabetical Dominance (a < b): Every character in string a must be strictly less than every character in string b alphabetically
  2. Alphabetical Dominance (b < a): Every character in string b must be strictly less than every character in string a alphabetically
  3. Uniform Strings: Both strings must consist of only one distinct character (they can be different characters)

In one operation, you can change any character in either string to any lowercase letter. Your goal is to find the minimum number of operations needed to achieve any one of these three conditions.

Example: If a = "aba" and b = "caa", you could change one character to make a = "aaa" and b = "aaa" (condition 3), requiring just 1 operation.

Input & Output

example_1.py โ€” Basic Case
$ Input: a = "aba", b = "caa"
โ€บ Output: 1
๐Ÿ’ก Note: We can change one character in string a to make both strings uniform: change 'b' to 'a' to get a="aaa", b="caa" (condition 3). Only 1 operation needed.
example_2.py โ€” Alphabetical Ordering
$ Input: a = "dabadd", b = "cda"
โ€บ Output: 3
๐Ÿ’ก Note: Best strategy is to make all characters in a โ‰ค 'c' and all characters in b > 'c'. We need to change 'd','d','d' in string a to characters โ‰ค 'c' (3 operations).
example_3.py โ€” Edge Case
$ Input: a = "abc", b = "def"
โ€บ Output: 0
๐Ÿ’ก Note: The strings already satisfy condition 1: every character in a ('a','b','c') is strictly less than every character in b ('d','e','f'). No operations needed.

Visualization

Tap to expand
String Transformation VisualizationString A: "ace"aceString B: "bdf"bdfSplit at 'c'All โ‰ค 'c'All > 'c'โœ“โœ“โœ—โœ—โœ“โœ“Strategy Cost Analysisโ€ข Split at 'c': 2 operations (change e,b)โ€ข Make uniform 'a': 4 operationsโ€ข Optimal: Split strategy (2 ops)
Understanding the Visualization
1
Count Character Types
Count how many of each letter type we have in both strings
2
Try Alphabetical Split
For each possible split point, calculate cost of moving characters
3
Try Uniform Strings
Calculate cost of making both strings consist of single character
4
Choose Minimum
Select the strategy that requires the fewest operations
Key Takeaway
๐ŸŽฏ Key Insight: Use frequency counting with prefix sums to efficiently evaluate all possible transformation strategies in optimal O(n+m) time

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Single pass to count frequencies O(n + m), then O(26) to try all splits and characters

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using fixed-size arrays of length 26 for character frequencies

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค a.length, b.length โ‰ค 105
  • a and b consist only of lowercase English letters
  • The strings are guaranteed to be non-empty
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen