bowang0911/leetcode huggingface dataset:
Encrypt and Decrypt Strings

You are given a character array `keys` containing **unique** characters and a string array `values` containing strings of length 2. You are also given another string array `dictionary` that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a **0-indexed** string.

A string is **encrypted** with the following process:

1.  For each character `c` in the string, we find the index `i` satisfying `keys[i] == c` in `keys`.
2.  Replace `c` with `values[i]` in the string.

Note that in case a character of the string is **not present** in `keys`, the encryption process cannot be carried out, and an empty string `" "` is returned.

A string is **decrypted** with the following process:

1.  For each substring `s` of length 2 occurring at an even index in the string, we find an `i` such that `values[i] == s`. If there are multiple valid `i`, we choose **any** one of them. This means a string could have multiple possible strings it can decrypt to.
2.  Replace `s` with `keys[i]` in the string.

Implement the `Encrypter` class:

*   `Encrypter(char[] keys, String[] values, String[] dictionary)` Initializes the `Encrypter` class with `keys, values`, and `dictionary`.
*   `String encrypt(String word1)` Encrypts `word1` with the encryption process described above and returns the encrypted string.
*   `int decrypt(String word2)` Returns the number of possible strings `word2` could decrypt to that also appear in `dictionary`.

**Example 1:**

**Input**
\[ "Encrypter ",  "encrypt ",  "decrypt "\]
\[\[\['a', 'b', 'c', 'd'\], \[ "ei ",  "zf ",  "ei ",  "am "\], \[ "abcd ",  "acbd ",  "adbc ",  "badc ",  "dacb ",  "cadb ",  "cbda ",  "abad "\]\], \[ "abcd "\], \[ "eizfeiam "\]\]
**Output**
\[null,  "eizfeiam ", 2\]

**Explanation**
Encrypter encrypter = new Encrypter(\[\['a', 'b', 'c', 'd'\], \[ "ei ",  "zf ",  "ei ",  "am "\], \[ "abcd ",  "acbd ",  "adbc ",  "badc ",  "dacb ",  "cadb ",  "cbda ",  "abad "\]);
encrypter.encrypt( "abcd "); // return  "eizfeiam ". 
                           // 'a' maps to  "ei ", 'b' maps to  "zf ", 'c' maps to  "ei ", and 'd' maps to  "am ".
encrypter.decrypt( "eizfeiam "); // return 2. 
                              //  "ei " can map to 'a' or 'c',  "zf " maps to 'b', and  "am " maps to 'd'. 
                              // Thus, the possible strings after decryption are  "abad ",  "cbad ",  "abcd ", and  "cbcd ". 
                              // 2 of those strings,  "abad " and  "abcd ", appear in dictionary, so the answer is 2.

**Constraints:**

*   `1 <= keys.length == values.length <= 26`
*   `values[i].length == 2`
*   `1 <= dictionary.length <= 100`
*   `1 <= dictionary[i].length <= 100`
*   All `keys[i]` and `dictionary[i]` are **unique**.
*   `1 <= word1.length <= 2000`
*   `1 <= word2.length <= 200`
*   All `word1[i]` appear in `keys`.
*   `word2.length` is even.
*   `keys`, `values[i]`, `dictionary[i]`, `word1`, and `word2` only contain lowercase English letters.
*   At most `200` calls will be made to `encrypt` and `decrypt` **in total**.
Check if an Array Is Consecutive

Given an integer array `nums`, return `true` _if_ `nums` _is **consecutive**, otherwise return_ `false`_._

An array is **consecutive** if it contains every number in the range `[x, x + n - 1]` (**inclusive**), where `x` is the minimum number in the array and `n` is the length of the array.

**Example 1:**

**Input:** nums = \[1,3,4,2\]
**Output:** true
**Explanation:**
The minimum value is 1 and the length of nums is 4.
All of the values in the range \[x, x + n - 1\] = \[1, 1 + 4 - 1\] = \[1, 4\] = (1, 2, 3, 4) occur in nums.
Therefore, nums is consecutive.

**Example 2:**

**Input:** nums = \[1,3\]
**Output:** false
**Explanation:**
The minimum value is 1 and the length of nums is 2.
The value 2 in the range \[x, x + n - 1\] = \[1, 1 + 2 - 1\], = \[1, 2\] = (1, 2) does not occur in nums.
Therefore, nums is not consecutive.

**Example 3:**

**Input:** nums = \[3,5,4\]
**Output:** true
**Explanation:**
The minimum value is 3 and the length of nums is 3.
All of the values in the range \[x, x + n - 1\] = \[3, 3 + 3 - 1\] = \[3, 5\] = (3, 4, 5) occur in nums.
Therefore, nums is consecutive.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 105`
Largest Number After Digit Swaps by Parity

You are given a positive integer `num`. You may swap any two digits of `num` that have the same **parity** (i.e. both odd digits or both even digits).

Return _the **largest** possible value of_ `num` _after **any** number of swaps._

**Example 1:**

**Input:** num = 1234
**Output:** 3412
**Explanation:** Swap the digit 3 with the digit 1, this results in the number 3214.
Swap the digit 2 with the digit 4, this results in the number 3412.
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.

**Example 2:**

**Input:** num = 65875
**Output:** 87655
**Explanation:** Swap the digit 8 with the digit 6, this results in the number 85675.
Swap the first digit 5 with the digit 7, this results in the number 87655.
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.

**Constraints:**

*   `1 <= num <= 109`
Minimize Result by Adding Parentheses to Expression

You are given a **0-indexed** string `expression` of the form `"+ "` where and represent positive integers.

Add a pair of parentheses to `expression` such that after the addition of parentheses, `expression` is a **valid** mathematical expression and evaluates to the **smallest** possible value. The left parenthesis **must** be added to the left of `'+'` and the right parenthesis **must** be added to the right of `'+'`.

Return `expression` _after adding a pair of parentheses such that_ `expression` _evaluates to the **smallest** possible value._ If there are multiple answers that yield the same result, return any of them.

The input has been generated such that the original value of `expression`, and the value of `expression` after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.

**Example 1:**

**Input:** expression =  "247+38 "
**Output:**  "2(47+38) "
**Explanation:** The `expression` evaluates to 2 \* (47 + 38) = 2 \* 85 = 170.
Note that  "2(4)7+38 " is invalid because the right parenthesis must be to the right of the `'+'`.
It can be shown that 170 is the smallest possible value.

**Example 2:**

**Input:** expression =  "12+34 "
**Output:**  "1(2+3)4 "
**Explanation:** The expression evaluates to 1 \* (2 + 3) \* 4 = 1 \* 5 \* 4 = 20.

**Example 3:**

**Input:** expression =  "999+999 "
**Output:**  "(999+999) "
**Explanation:** The `expression` evaluates to 999 + 999 = 1998.

**Constraints:**

*   `3 <= expression.length <= 10`
*   `expression` consists of digits from `'1'` to `'9'` and `'+'`.
*   `expression` starts and ends with digits.
*   `expression` contains exactly one `'+'`.
*   The original value of `expression`, and the value of `expression` after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
Maximum Product After K Increments

You are given an array of non-negative integers `nums` and an integer `k`. In one operation, you may choose **any** element from `nums` and **increment** it by `1`.

Return _the **maximum** **product** of_ `nums` _after **at most**_ `k` _operations._ Since the answer may be very large, return it **modulo** `109 + 7`. Note that you should maximize the product before taking the modulo.

**Example 1:**

**Input:** nums = \[0,4\], k = 5
**Output:** 20
**Explanation:** Increment the first number 5 times.
Now nums = \[5, 4\], with a product of 5 \* 4 = 20.
It can be shown that 20 is maximum product possible, so we return 20.
Note that there may be other ways to increment nums to have the maximum product.

**Example 2:**

**Input:** nums = \[6,3,3,2\], k = 2
**Output:** 216
**Explanation:** Increment the second number 1 time and increment the fourth number 1 time.
Now nums = \[6, 4, 3, 3\], with a product of 6 \* 4 \* 3 \* 3 = 216.
It can be shown that 216 is maximum product possible, so we return 216.
Note that there may be other ways to increment nums to have the maximum product.

**Constraints:**

*   `1 <= nums.length, k <= 105`
*   `0 <= nums[i] <= 106`
Maximum Total Beauty of the Gardens

Alice is a caretaker of `n` gardens and she wants to plant flowers to maximize the total beauty of all her gardens.

You are given a **0-indexed** integer array `flowers` of size `n`, where `flowers[i]` is the number of flowers already planted in the `ith` garden. Flowers that are already planted **cannot** be removed. You are then given another integer `newFlowers`, which is the **maximum** number of flowers that Alice can additionally plant. You are also given the integers `target`, `full`, and `partial`.

A garden is considered **complete** if it has **at least** `target` flowers. The **total beauty** of the gardens is then determined as the **sum** of the following:

*   The number of **complete** gardens multiplied by `full`.
*   The **minimum** number of flowers in any of the **incomplete** gardens multiplied by `partial`. If there are no incomplete gardens, then this value will be `0`.

Return _the **maximum** total beauty that Alice can obtain after planting at most_ `newFlowers` _flowers._

**Example 1:**

**Input:** flowers = \[1,3,1,1\], newFlowers = 7, target = 6, full = 12, partial = 1
**Output:** 14
**Explanation:** Alice can plant
- 2 flowers in the 0th garden
- 3 flowers in the 1st garden
- 1 flower in the 2nd garden
- 1 flower in the 3rd garden
The gardens will then be \[3,6,2,2\]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers.
There is 1 garden that is complete.
The minimum number of flowers in the incomplete gardens is 2.
Thus, the total beauty is 1 \* 12 + 2 \* 1 = 12 + 2 = 14.
No other way of planting flowers can obtain a total beauty higher than 14.

**Example 2:**

**Input:** flowers = \[2,4,5,3\], newFlowers = 10, target = 5, full = 2, partial = 6
**Output:** 30
**Explanation:** Alice can plant
- 3 flowers in the 0th garden
- 0 flowers in the 1st garden
- 0 flowers in the 2nd garden
- 2 flowers in the 3rd garden
The gardens will then be \[5,4,5,5\]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers.
There are 3 gardens that are complete.
The minimum number of flowers in the incomplete gardens is 4.
Thus, the total beauty is 3 \* 2 + 4 \* 6 = 6 + 24 = 30.
No other way of planting flowers can obtain a total beauty higher than 30.
Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.

**Constraints:**

*   `1 <= flowers.length <= 105`
*   `1 <= flowers[i], target <= 105`
*   `1 <= newFlowers <= 1010`
*   `1 <= full, partial <= 105`
Add Two Integers

Given two integers `num1` and `num2`, return _the **sum** of the two integers_.

**Example 1:**

**Input:** num1 = 12, num2 = 5
**Output:** 17
**Explanation:** num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.

**Example 2:**

**Input:** num1 = -10, num2 = 4
**Output:** -6
**Explanation:** num1 + num2 = -6, so -6 is returned.

**Constraints:**

*   `-100 <= num1, num2 <= 100`
Root Equals Sum of Children

You are given the `root` of a **binary tree** that consists of exactly `3` nodes: the root, its left child, and its right child.

Return `true` _if the value of the root is equal to the **sum** of the values of its two children, or_ `false` _otherwise_.

**Example 1:**

**Input:** root = \[10,4,6\]
**Output:** true
**Explanation:** The values of the root, its left child, and its right child are 10, 4, and 6, respectively.
10 is equal to 4 + 6, so we return true.

**Example 2:**

**Input:** root = \[5,3,1\]
**Output:** false
**Explanation:** The values of the root, its left child, and its right child are 5, 3, and 1, respectively.
5 is not equal to 3 + 1, so we return false.

**Constraints:**

*   The tree consists only of the root, its left child, and its right child.
*   `-100 <= Node.val <= 100`
Count Positions on Street With Required Brightness

You are given an integer `n`. A perfectly straight street is represented by a number line ranging from `0` to `n - 1`. You are given a 2D integer array `lights` representing the street lamp(s) on the street. Each `lights[i] = [positioni, rangei]` indicates that there is a street lamp at position `positioni` that lights up the area from `[max(0, positioni - rangei), min(n - 1, positioni + rangei)]` (**inclusive**).

The **brightness** of a position `p` is defined as the number of street lamps that light up the position `p`. You are given a **0-indexed** integer array `requirement` of size `n` where `requirement[i]` is the minimum **brightness** of the `ith` position on the street.

Return _the number of positions_ `i` _on the street between_ `0` _and_ `n - 1` _that have a **brightness**_ _of **at least**_ `requirement[i]`_._

**Example 1:**

**Input:** n = 5, lights = \[\[0,1\],\[2,1\],\[3,2\]\], requirement = \[0,2,1,4,1\]
**Output:** 4
**Explanation:**
- The first street lamp lights up the area from \[max(0, 0 - 1), min(n - 1, 0 + 1)\] = \[0, 1\] (inclusive).
- The second street lamp lights up the area from \[max(0, 2 - 1), min(n - 1, 2 + 1)\] = \[1, 3\] (inclusive).
- The third street lamp lights up the area from \[max(0, 3 - 2), min(n - 1, 3 + 2)\] = \[1, 4\] (inclusive).

- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is greater than requirement\[0\].
- Position 1 is covered by the first, second, and third street lamps. It is covered by 3 street lamps which is greater than requirement\[1\].
- Position 2 is covered by the second and third street lamps. It is covered by 2 street lamps which is greater than requirement\[2\].
- Position 3 is covered by the second and third street lamps. It is covered by 2 street lamps which is less than requirement\[3\].
- Position 4 is covered by the third street lamp. It is covered by 1 street lamp which is equal to requirement\[4\].

Positions 0, 1, 2, and 4 meet the requirement so we return 4.

**Example 2:**

**Input:** n = 1, lights = \[\[0,1\]\], requirement = \[2\]
**Output:** 0
**Explanation:**
- The first street lamp lights up the area from \[max(0, 0 - 1), min(n - 1, 0 + 1)\] = \[0, 0\] (inclusive).
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is less than requirement\[0\].
- We return 0 because no position meets their brightness requirement.

**Constraints:**

*   `1 <= n <= 105`
*   `1 <= lights.length <= 105`
*   `0 <= positioni < n`
*   `0 <= rangei <= 105`
*   `requirement.length == n`
*   `0 <= requirement[i] <= 105`
Find Closest Number to Zero

Given an integer array `nums` of size `n`, return _the number with the value **closest** to_ `0` _in_ `nums`. If there are multiple answers, return _the number with the **largest** value_.

**Example 1:**

**Input:** nums = \[-4,-2,1,4,8\]
**Output:** 1
**Explanation:**
The distance from -4 to 0 is |-4| = 4.
The distance from -2 to 0 is |-2| = 2.
The distance from 1 to 0 is |1| = 1.
The distance from 4 to 0 is |4| = 4.
The distance from 8 to 0 is |8| = 8.
Thus, the closest number to 0 in the array is 1.

**Example 2:**

**Input:** nums = \[2,-1,1\]
**Output:** 1
**Explanation:** 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.

**Constraints:**

*   `1 <= n <= 1000`
*   `-105 <= nums[i] <= 105`
Number of Ways to Buy Pens and Pencils

You are given an integer `total` indicating the amount of money you have. You are also given two integers `cost1` and `cost2` indicating the price of a pen and pencil respectively. You can spend **part or all** of your money to buy multiple quantities (or none) of each kind of writing utensil.

Return _the **number of distinct ways** you can buy some number of pens and pencils._

**Example 1:**

**Input:** total = 20, cost1 = 10, cost2 = 5
**Output:** 9
**Explanation:** The price of a pen is 10 and the price of a pencil is 5.
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
- If you buy 2 pens, you cannot buy any pencils.
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.

**Example 2:**

**Input:** total = 5, cost1 = 10, cost2 = 10
**Output:** 1
**Explanation:** The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.

**Constraints:**

*   `1 <= total, cost1, cost2 <= 106`
Design an ATM Machine

There is an ATM machine that stores banknotes of `5` denominations: `20`, `50`, `100`, `200`, and `500` dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money.

When withdrawing, the machine prioritizes using banknotes of **larger** values.

*   For example, if you want to withdraw `$300` and there are `2` `$50` banknotes, `1` `$100` banknote, and `1` `$200` banknote, then the machine will use the `$100` and `$200` banknotes.
*   However, if you try to withdraw `$600` and there are `3` `$200` banknotes and `1` `$500` banknote, then the withdraw request will be rejected because the machine will first try to use the `$500` banknote and then be unable to use banknotes to complete the remaining `$100`. Note that the machine is **not** allowed to use the `$200` banknotes instead of the `$500` banknote.

Implement the ATM class:

*   `ATM()` Initializes the ATM object.
*   `void deposit(int[] banknotesCount)` Deposits new banknotes in the order `$20`, `$50`, `$100`, `$200`, and `$500`.
*   `int[] withdraw(int amount)` Returns an array of length `5` of the number of banknotes that will be handed to the user in the order `$20`, `$50`, `$100`, `$200`, and `$500`, and update the number of banknotes in the ATM after withdrawing. Returns `[-1]` if it is not possible (do **not** withdraw any banknotes in this case).

**Example 1:**

**Input**
\[ "ATM ",  "deposit ",  "withdraw ",  "deposit ",  "withdraw ",  "withdraw "\]
\[\[\], \[\[0,0,1,2,1\]\], \[600\], \[\[0,1,0,1,1\]\], \[600\], \[550\]\]
**Output**
\[null, null, \[0,0,1,0,1\], null, \[-1\], \[0,1,0,0,1\]\]

**Explanation**
ATM atm = new ATM();
atm.deposit(\[0,0,1,2,1\]); // Deposits 1 $100 banknote, 2 $200 banknotes,
                          // and 1 $500 banknote.
atm.withdraw(600);        // Returns \[0,0,1,0,1\]. The machine uses 1 $100 banknote
                          // and 1 $500 banknote. The banknotes left over in the
                          // machine are \[0,0,0,2,0\].
atm.deposit(\[0,1,0,1,1\]); // Deposits 1 $50, $200, and $500 banknote.
                          // The banknotes in the machine are now \[0,1,0,3,1\].
atm.withdraw(600);        // Returns \[-1\]. The machine will try to use a $500 banknote
                          // and then be unable to complete the remaining $100,
                          // so the withdraw request will be rejected.
                          // Since the request is rejected, the number of banknotes
                          // in the machine is not modified.
atm.withdraw(550);        // Returns \[0,1,0,0,1\]. The machine uses 1 $50 banknote
                          // and 1 $500 banknote.

**Constraints:**

*   `banknotesCount.length == 5`
*   `0 <= banknotesCount[i] <= 109`
*   `1 <= amount <= 109`
*   At most `5000` calls **in total** will be made to `withdraw` and `deposit`.
*   At least **one** call will be made to each function `withdraw` and `deposit`.
Maximum Score of a Node Sequence

There is an **undirected** graph with `n` nodes, numbered from `0` to `n - 1`.

You are given a **0-indexed** integer array `scores` of length `n` where `scores[i]` denotes the score of node `i`. You are also given a 2D integer array `edges` where `edges[i] = [ai, bi]` denotes that there exists an **undirected** edge connecting nodes `ai` and `bi`.

A node sequence is **valid** if it meets the following conditions:

*   There is an edge connecting every pair of **adjacent** nodes in the sequence.
*   No node appears more than once in the sequence.

The score of a node sequence is defined as the **sum** of the scores of the nodes in the sequence.

Return _the **maximum score** of a valid node sequence with a length of_ `4`_._ If no such sequence exists, return `-1`.

**Example 1:**

**Input:** scores = \[5,2,9,8,4\], edges = \[\[0,1\],\[1,2\],\[2,3\],\[0,2\],\[1,3\],\[2,4\]\]
**Output:** 24
**Explanation:** The figure above shows the graph and the chosen node sequence \[0,1,2,3\].
The score of the node sequence is 5 + 2 + 9 + 8 = 24.
It can be shown that no other node sequence has a score of more than 24.
Note that the sequences \[3,1,2,0\] and \[1,0,2,3\] are also valid and have a score of 24.
The sequence \[0,3,2,4\] is not valid since no edge connects nodes 0 and 3.

**Example 2:**

**Input:** scores = \[9,20,6,4,11,12\], edges = \[\[0,3\],\[5,3\],\[2,4\],\[1,3\]\]
**Output:** -1
**Explanation:** The figure above shows the graph.
There are no valid node sequences of length 4, so we return -1.

**Constraints:**

*   `n == scores.length`
*   `4 <= n <= 5 * 104`
*   `1 <= scores[i] <= 108`
*   `0 <= edges.length <= 5 * 104`
*   `edges[i].length == 2`
*   `0 <= ai, bi <= n - 1`
*   `ai != bi`
*   There are no duplicate edges.
Calculate Digit Sum of a String

You are given a string `s` consisting of digits and an integer `k`.

A **round** can be completed if the length of `s` is greater than `k`. In one round, do the following:

1.  **Divide** `s` into **consecutive groups** of size `k` such that the first `k` characters are in the first group, the next `k` characters are in the second group, and so on. **Note** that the size of the last group can be smaller than `k`.
2.  **Replace** each group of `s` with a string representing the sum of all its digits. For example, `"346 "` is replaced with `"13 "` because `3 + 4 + 6 = 13`.
3.  **Merge** consecutive groups together to form a new string. If the length of the string is greater than `k`, repeat from step `1`.

Return `s` _after all rounds have been completed_.

**Example 1:**

**Input:** s =  "11111222223 ", k = 3
**Output:**  "135 "
**Explanation:** 
- For the first round, we divide s into groups of size 3:  "111 ",  "112 ",  "222 ", and  "23 ".
  ​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5. 
  So, s becomes  "3 " +  "4 " +  "6 " +  "5 " =  "3465 " after the first round.
- For the second round, we divide s into  "346 " and  "5 ".
  Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. 
  So, s becomes  "13 " +  "5 " =  "135 " after second round. 
Now, s.length <= k, so we return  "135 " as the answer.

**Example 2:**

**Input:** s =  "00000000 ", k = 3
**Output:**  "000 "
**Explanation:** 
We divide s into  "000 ",  "000 ", and  "00 ".
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. 
s becomes  "0 " +  "0 " +  "0 " =  "000 ", whose length is equal to k, so we return  "000 ".

**Constraints:**

*   `1 <= s.length <= 100`
*   `2 <= k <= 100`
*   `s` consists of digits only.