Change Minimum Characters to Satisfy One of Three Conditions
Medium · Hash Table, String, Counting, Prefix Sum
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:
Return the minimum number of operations needed to achieve your goal.
Example 1
Input: a = "aba", b = "caa" Output: 2
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
The best way is to make condition 1 true by changing b to "eee".
Constraints
- 1 <= a.length, b.length <= 105
- a and b consist only of lowercase letters.
Starter code (python)
class Solution(object):
def minCharacters(self, a, b):
"""
:type a: str
:type b: str
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
Previous: Minimum Length of String After Deleting Similar Ends · Next: Palindrome Partitioning IV · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.