Appearance
question: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: * **Every** letter in `a` is **strictly less** than **every** letter in `b` in the alphabet. * **Every** letter in `b` is **strictly less** than **every** letter in `a` in the alphabet. * **Both** `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 <= 105` * `a` and `b` consist only of lowercase letters.
answer:def maxDepth(s: str) -> int: depth, max_depth = 0, 0 for ch in s: if ch == '(': depth += 1 elif ch == ')': depth -= 1 max_depth = max(max_depth, depth) return max_depth
question:You are given two strings `s1` and `s2` of equal length. A **string swap** is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return `true` _if it is possible to make both strings equal by performing **at most one string swap** on **exactly one** of the strings._ Otherwise, return `false`. **Example 1:** **Input:** s1 = bank , s2 = kanb **Output:** true **Explanation:** For example, swap the first character with the last character of s2 to make bank . **Example 2:** **Input:** s1 = attack , s2 = defend **Output:** false **Explanation:** It is impossible to make them equal with one string swap. **Example 3:** **Input:** s1 = kelb , s2 = kelb **Output:** true **Explanation:** The two strings are already equal, so no string swap operation is required. **Constraints:** * `1 <= s1.length, s2.length <= 100` * `s1.length == s2.length` * `s1` and `s2` consist of only lowercase English letters.
answer:def are_almost_equal(s1, s2): if s1 == s2: return True diff = [i for i in range(len(s1)) if s1[i] != s2[i]] return len(diff) == 2 and s1[diff[0]] == s2[diff[1]] and s1[diff[1]] == s2[diff[0]]
question:You are given an `m x n` grid `grid` where: * `'.'` is an empty cell. * `'#'` is a wall. * `'@'` is the starting point. * Lowercase letters represent keys. * Uppercase letters represent locks. You start at the starting point and one move consists of walking one space in one of the four cardinal directions. You cannot walk outside the grid, or walk into a wall. If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key. For some `1 <= k <= 6`, there is exactly one lowercase and one uppercase letter of the first `k` letters of the English alphabet in the grid. This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return _the lowest number of moves to acquire all keys_. If it is impossible, return `1`. **Example 1:** **Input:** grid = [ @.a.. , #.# , b.A.B ] **Output:** 8 **Explanation:** Note that the goal is to obtain all the keys not to open all the locks. **Example 2:** **Input:** grid = [ @..aA , ..B#. , ....b ] **Output:** 6 **Example 3:** **Input:** grid = [ @Aa ] **Output:** 1 **Constraints:** * `m == grid.length` * `n == grid[i].length` * `1 <= m, n <= 30` * `grid[i][j]` is either an English letter, `'.'`, `'#'`, or `'@'`. * The number of keys in the grid is in the range `[1, 6]`. * Each key in the grid is **unique**. * Each key in the grid has a matching lock.
answer:def largestOverlap(img1, img2): n = len(img1) onesImg1 = [(i, j) for i in range(n) for j in range(n) if img1[i][j]] onesImg2 = [(i, j) for i in range(n) for j in range(n) if img2[i][j]] overlaps = {} for pt1 in onesImg1: for pt2 in onesImg2: dx, dy = pt2[0] - pt1[0], pt2[1] - pt1[1] key = (dx, dy) if key not in overlaps: overlaps[key] = 0 overlaps[key] += 1 return max(overlaps.values() or [0])
question:You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used. * For example, given `num = 2932`, you have the following digits: two `2`'s, one `9` and one `3`. Some of the possible pairs `[new1, new2]` are `[22, 93]`, `[23, 92]`, `[223, 9]` and `[2, 329]`. Return _the **minimum** possible sum of_ `new1` _and_ `new2`. **Example 1:** **Input:** num = 2932 **Output:** 52 **Explanation:** Some possible pairs [new1, new2] are [29, 23], [223, 9], etc. The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52. **Example 2:** **Input:** num = 4009 **Output:** 13 **Explanation:** Some possible pairs [new1, new2] are [0, 49], [490, 0], etc. The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13. **Constraints:** * `1000 <= num <= 9999`
answer:def min_operations(grid, x): min_element = min(min(row) for row in grid) min_ops = float('inf') base = min_element while True: operations = 0 impossible = False for row in grid: if impossible: break for cell in row: diff = abs(cell - base) if diff % x != 0: impossible = True break operations += diff // x if impossible: break min_ops = min(min_ops, operations) base -= 1 return -1 if min_ops == float('inf') else min_ops