1737. Change Minimum Characters to Satisfy One of Three Conditions class Solution: def minCharacters(self, a: str, b: str) -> int: from collections import Counter ac = Counter(ord(i) - ord('a') for i in a) bc = Counter(ord(i) - ord('a') for i in b) com = ac.keys() & bc.keys() same = len(a) + len(b) - max((ac + bc).values()) def work(s1, s2): # a < b res = float("inf") ## s2 for i in range(1, 26): cnt = 0 for j in range(i, 26): cnt += s1.get(j, 0) for j in range(0, i): cnt += s2.get(j, 0) res = min(res,cnt) return res return min(same, work(ac,bc), work(bc,ac))