bowang0911/leetcode huggingface dataset:
Groups of Strings

You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.

Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one** of the following operations:

*   Adding exactly one letter to the set of the letters of `s1`.
*   Deleting exactly one letter from the set of the letters of `s1`.
*   Replacing exactly one letter from the set of the letters of `s1` with any letter, **including** itself.

The array `words` can be divided into one or more non-intersecting **groups**. A string belongs to a group if any **one** of the following is true:

*   It is connected to **at least one** other string of the group.
*   It is the **only** string present in the group.

Note that the strings in `words` should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.

Return _an array_ `ans` _of size_ `2` _where:_

*   `ans[0]` _is the **maximum number** of groups_ `words` _can be divided into, and_
*   `ans[1]` _is the **size of the largest** group_.

**Example 1:**

**Input:** words = \[ "a ", "b ", "ab ", "cde "\]
**Output:** \[2,3\]
**Explanation:**
- words\[0\] can be used to obtain words\[1\] (by replacing 'a' with 'b'), and words\[2\] (by adding 'b'). So words\[0\] is connected to words\[1\] and words\[2\].
- words\[1\] can be used to obtain words\[0\] (by replacing 'b' with 'a'), and words\[2\] (by adding 'a'). So words\[1\] is connected to words\[0\] and words\[2\].
- words\[2\] can be used to obtain words\[0\] (by deleting 'b'), and words\[1\] (by deleting 'a'). So words\[2\] is connected to words\[0\] and words\[1\].
- words\[3\] is not connected to any string in words.
Thus, words can be divided into 2 groups \[ "a ", "b ", "ab "\] and \[ "cde "\]. The size of the largest group is 3.  

**Example 2:**

**Input:** words = \[ "a ", "ab ", "abc "\]
**Output:** \[1,3\]
**Explanation:**
- words\[0\] is connected to words\[1\].
- words\[1\] is connected to words\[0\] and words\[2\].
- words\[2\] is connected to words\[1\].
Since all strings are connected to each other, they should be grouped together.
Thus, the size of the largest group is 3.

**Constraints:**

*   `1 <= words.length <= 2 * 104`
*   `1 <= words[i].length <= 26`
*   `words[i]` consists of lowercase English letters only.
*   No letter occurs more than once in `words[i]`.
Amount of New Area Painted Each Day

There is a long and thin painting that can be represented by a number line. You are given a **0-indexed** 2D integer array `paint` of length `n`, where `paint[i] = [starti, endi]`. This means that on the `ith` day you need to paint the area **between** `starti` and `endi`.

Painting the same area multiple times will create an uneven painting so you only want to paint each area of the painting at most **once**.

Return _an integer array_ `worklog` _of length_ `n`_, where_ `worklog[i]` _is the amount of **new** area that you painted on the_ `ith` _day._

**Example 1:**

**Input:** paint = \[\[1,4\],\[4,7\],\[5,8\]\]
**Output:** \[3,3,1\]
**Explanation:**
On day 0, paint everything between 1 and 4.
The amount of new area painted on day 0 is 4 - 1 = 3.
On day 1, paint everything between 4 and 7.
The amount of new area painted on day 1 is 7 - 4 = 3.
On day 2, paint everything between 7 and 8.
Everything between 5 and 7 was already painted on day 1.
The amount of new area painted on day 2 is 8 - 7 = 1. 

**Example 2:**

**Input:** paint = \[\[1,4\],\[5,8\],\[4,7\]\]
**Output:** \[3,3,1\]
**Explanation:**
On day 0, paint everything between 1 and 4.
The amount of new area painted on day 0 is 4 - 1 = 3.
On day 1, paint everything between 5 and 8.
The amount of new area painted on day 1 is 8 - 5 = 3.
On day 2, paint everything between 4 and 5.
Everything between 5 and 7 was already painted on day 1.
The amount of new area painted on day 2 is 5 - 4 = 1. 

**Example 3:**

**Input:** paint = \[\[1,5\],\[2,4\]\]
**Output:** \[4,0\]
**Explanation:**
On day 0, paint everything between 1 and 5.
The amount of new area painted on day 0 is 5 - 1 = 4.
On day 1, paint nothing because everything between 2 and 4 was already painted on day 0.
The amount of new area painted on day 1 is 0.

**Constraints:**

*   `1 <= paint.length <= 105`
*   `paint[i].length == 2`
*   `0 <= starti < endi <= 5 * 104`
Minimum Sum of Four Digit Number After Splitting Digits

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`
Minimum Cost to Set Cooking Time

A generic microwave supports cooking times for:

*   at least `1` second.
*   at most `99` minutes and `99` seconds.

To set the cooking time, you push **at most four digits**. The microwave normalizes what you push as four digits by **prepending zeroes**. It interprets the **first** two digits as the minutes and the **last** two digits as the seconds. It then **adds** them up as the cooking time. For example,

*   You push `9` `5` `4` (three digits). It is normalized as `0954` and interpreted as `9` minutes and `54` seconds.
*   You push `0` `0` `0` `8` (four digits). It is interpreted as `0` minutes and `8` seconds.
*   You push `8` `0` `9` `0`. It is interpreted as `80` minutes and `90` seconds.
*   You push `8` `1` `3` `0`. It is interpreted as `81` minutes and `30` seconds.

You are given integers `startAt`, `moveCost`, `pushCost`, and `targetSeconds`. **Initially**, your finger is on the digit `startAt`. Moving the finger above **any specific digit** costs `moveCost` units of fatigue. Pushing the digit below the finger **once** costs `pushCost` units of fatigue.

There can be multiple ways to set the microwave to cook for `targetSeconds` seconds but you are interested in the way with the minimum cost.

Return _the **minimum cost** to set_ `targetSeconds` _seconds of cooking time_.

Remember that one minute consists of `60` seconds.

**Example 1:**

**Input:** startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
**Output:** 6
**Explanation:** The following are the possible ways to set the cooking time.
- 1 0 0 0, interpreted as 10 minutes and 0 seconds.
  The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1).
  The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost.
- 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds.
  The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
  The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12.
- 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds.
  The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
  The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.

**Example 2:**

**Input:** startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
**Output:** 6
**Explanation:** The optimal way is to push two digits: 7 6, interpreted as 76 seconds.
The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2). The total cost is: 1 + 2 + 1 + 2 = 6
Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.

**Constraints:**

*   `0 <= startAt <= 9`
*   `1 <= moveCost, pushCost <= 105`
*   `1 <= targetSeconds <= 6039`
Minimum Difference in Sums After Removal of Elements

You are given a **0-indexed** integer array `nums` consisting of `3 * n` elements.

You are allowed to remove any **subsequence** of elements of size **exactly** `n` from `nums`. The remaining `2 * n` elements will be divided into two **equal** parts:

*   The first `n` elements belonging to the first part and their sum is `sumfirst`.
*   The next `n` elements belonging to the second part and their sum is `sumsecond`.

The **difference in sums** of the two parts is denoted as `sumfirst - sumsecond`.

*   For example, if `sumfirst = 3` and `sumsecond = 2`, their difference is `1`.
*   Similarly, if `sumfirst = 2` and `sumsecond = 3`, their difference is `-1`.

Return _the **minimum difference** possible between the sums of the two parts after the removal of_ `n` _elements_.

**Example 1:**

**Input:** nums = \[3,1,2\]
**Output:** -1
**Explanation:** Here, nums has 3 elements, so n = 1. 
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums\[0\] = 3, the array will be \[1,2\]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums\[1\] = 1, the array will be \[3,2\]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums\[2\] = 2, the array will be \[3,1\]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1. 

**Example 2:**

**Input:** nums = \[7,9,5,8,1,3\]
**Output:** 1
**Explanation:** Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums\[2\] = 5 and nums\[3\] = 8, the resultant array will be \[7,9,1,3\]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums\[1\] = 9 and nums\[4\] = 1. The resultant array becomes \[7,5,8,3\]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.

**Constraints:**

*   `nums.length == 3 * n`
*   `1 <= n <= 105`
*   `1 <= nums[i] <= 105`
Sort Even and Odd Indices Independently

You are given a **0-indexed** integer array `nums`. Rearrange the values of `nums` according to the following rules:

1.  Sort the values at **odd indices** of `nums` in **non-increasing** order.
    *   For example, if `nums = [4,**1**,2,**3**]` before this step, it becomes `[4,**3**,2,**1**]` after. The values at odd indices `1` and `3` are sorted in non-increasing order.
2.  Sort the values at **even indices** of `nums` in **non-decreasing** order.
    *   For example, if `nums = [**4**,1,**2**,3]` before this step, it becomes `[**2**,1,**4**,3]` after. The values at even indices `0` and `2` are sorted in non-decreasing order.

Return _the array formed after rearranging the values of_ `nums`.

**Example 1:**

**Input:** nums = \[4,1,2,3\]
**Output:** \[2,3,4,1\]
**Explanation:** 
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
So, nums changes from \[4,**1**,2,**3**\] to \[4,**3**,2,**1**\].
Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
So, nums changes from \[**4**,1,**2**,3\] to \[**2**,3,**4**,1\].
Thus, the array formed after rearranging the values is \[2,3,4,1\].

**Example 2:**

**Input:** nums = \[2,1\]
**Output:** \[2,1\]
**Explanation:** 
Since there is exactly one odd index and one even index, no rearrangement of values takes place.
The resultant array formed is \[2,1\], which is the same as the initial array. 

**Constraints:**

*   `1 <= nums.length <= 100`
*   `1 <= nums[i] <= 100`
Smallest Value of the Rearranged Number

You are given an integer `num.` **Rearrange** the digits of `num` such that its value is **minimized** and it does not contain **any** leading zeros.

Return _the rearranged number with minimal value_.

Note that the sign of the number does not change after rearranging the digits.

**Example 1:**

**Input:** num = 310
**Output:** 103
**Explanation:** The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310. 
The arrangement with the smallest value that does not contain any leading zeros is 103.

**Example 2:**

**Input:** num = -7605
**Output:** -7650
**Explanation:** Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567.
The arrangement with the smallest value that does not contain any leading zeros is -7650.

**Constraints:**

*   `-1015 <= num <= 1015`
Design Bitset

A **Bitset** is a data structure that compactly stores bits.

Implement the `Bitset` class:

*   `Bitset(int size)` Initializes the Bitset with `size` bits, all of which are `0`.
*   `void fix(int idx)` Updates the value of the bit at the index `idx` to `1`. If the value was already `1`, no change occurs.
*   `void unfix(int idx)` Updates the value of the bit at the index `idx` to `0`. If the value was already `0`, no change occurs.
*   `void flip()` Flips the values of each bit in the Bitset. In other words, all bits with value `0` will now have value `1` and vice versa.
*   `boolean all()` Checks if the value of **each** bit in the Bitset is `1`. Returns `true` if it satisfies the condition, `false` otherwise.
*   `boolean one()` Checks if there is **at least one** bit in the Bitset with value `1`. Returns `true` if it satisfies the condition, `false` otherwise.
*   `int count()` Returns the **total number** of bits in the Bitset which have value `1`.
*   `String toString()` Returns the current composition of the Bitset. Note that in the resultant string, the character at the `ith` index should coincide with the value at the `ith` bit of the Bitset.

**Example 1:**

**Input**
\[ "Bitset ",  "fix ",  "fix ",  "flip ",  "all ",  "unfix ",  "flip ",  "one ",  "unfix ",  "count ",  "toString "\]
\[\[5\], \[3\], \[1\], \[\], \[\], \[0\], \[\], \[\], \[0\], \[\], \[\]\]
**Output**
\[null, null, null, null, false, null, null, true, null, 2,  "01010 "\]

**Explanation**
Bitset bs = new Bitset(5); // bitset =  "00000 ".
bs.fix(3);     // the value at idx = 3 is updated to 1, so bitset =  "00010 ".
bs.fix(1);     // the value at idx = 1 is updated to 1, so bitset =  "01010 ". 
bs.flip();     // the value of each bit is flipped, so bitset =  "10101 ". 
bs.all();      // return False, as not all values of the bitset are 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset =  "00101 ".
bs.flip();     // the value of each bit is flipped, so bitset =  "11010 ". 
bs.one();      // return True, as there is at least 1 index with value 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset =  "01010 ".
bs.count();    // return 2, as there are 2 bits with value 1.
bs.toString(); // return  "01010 ", which is the composition of bitset.

**Constraints:**

*   `1 <= size <= 105`
*   `0 <= idx <= size - 1`
*   At most `105` calls will be made **in total** to `fix`, `unfix`, `flip`, `all`, `one`, `count`, and `toString`.
*   At least one call will be made to `all`, `one`, `count`, or `toString`.
*   At most `5` calls will be made to `toString`.
Minimum Time to Remove All Cars Containing Illegal Goods

You are given a **0-indexed** binary string `s` which represents a sequence of train cars. `s[i] = '0'` denotes that the `ith` car does **not** contain illegal goods and `s[i] = '1'` denotes that the `ith` car does contain illegal goods.

As the train conductor, you would like to get rid of all the cars containing illegal goods. You can do any of the following three operations **any** number of times:

1.  Remove a train car from the **left** end (i.e., remove `s[0]`) which takes 1 unit of time.
2.  Remove a train car from the **right** end (i.e., remove `s[s.length - 1]`) which takes 1 unit of time.
3.  Remove a train car from **anywhere** in the sequence which takes 2 units of time.

Return _the **minimum** time to remove all the cars containing illegal goods_.

Note that an empty sequence of cars is considered to have no cars containing illegal goods.

**Example 1:**

**Input:** s =  "**11**00**1**0**1** "
**Output:** 5
**Explanation:** 
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 2 times. Time taken is 2 \* 1 = 2.
- remove a car from the right end. Time taken is 1.
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2 + 1 + 2 = 5. 

An alternative way is to
- remove a car from the left end 2 times. Time taken is 2 \* 1 = 2.
- remove a car from the right end 3 times. Time taken is 3 \* 1 = 3.
This also obtains a total time of 2 + 3 = 5.

5 is the minimum time taken to remove all the cars containing illegal goods. 
There are no other ways to remove them with less time.

**Example 2:**

**Input:** s =  "00**1**0 "
**Output:** 2
**Explanation:**
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 3 times. Time taken is 3 \* 1 = 3.
This obtains a total time of 3.

Another way to remove all the cars containing illegal goods from the sequence is to
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2.

Another way to remove all the cars containing illegal goods from the sequence is to 
- remove a car from the right end 2 times. Time taken is 2 \* 1 = 2. 
This obtains a total time of 2.

2 is the minimum time taken to remove all the cars containing illegal goods. 
There are no other ways to remove them with less time.

**Constraints:**

*   `1 <= s.length <= 2 * 105`
*   `s[i]` is either `'0'` or `'1'`.
Unique Substrings With Equal Digit Frequency

Given a digit string `s`, return _the number of **unique substrings** of_ `s` _where every digit appears the same number of times._

**Example 1:**

**Input:** s =  "1212 "
**Output:** 5
**Explanation:** The substrings that meet the requirements are  "1 ",  "2 ",  "12 ",  "21 ",  "1212 ".
Note that although the substring  "12 " appears twice, it is only counted once.

**Example 2:**

**Input:** s =  "12321 "
**Output:** 9
**Explanation:** The substrings that meet the requirements are  "1 ",  "2 ",  "3 ",  "12 ",  "23 ",  "32 ",  "21 ",  "123 ",  "321 ".

**Constraints:**

*   `1 <= s.length <= 1000`
*   `s` consists of digits.
Minimum Operations to Make the Array Alternating

You are given a **0-indexed** array `nums` consisting of `n` positive integers.

The array `nums` is called **alternating** if:

*   `nums[i - 2] == nums[i]`, where `2 <= i <= n - 1`.
*   `nums[i - 1] != nums[i]`, where `1 <= i <= n - 1`.

In one **operation**, you can choose an index `i` and **change** `nums[i]` into **any** positive integer.

Return _the **minimum number of operations** required to make the array alternating_.

**Example 1:**

**Input:** nums = \[3,1,3,2,4,3\]
**Output:** 3
**Explanation:**
One way to make the array alternating is by converting it to \[3,1,3,**1**,**3**,**1**\].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations. 

**Example 2:**

**Input:** nums = \[1,2,2,2,2\]
**Output:** 2
**Explanation:**
One way to make the array alternating is by converting it to \[1,2,**1**,2,**1**\].
The number of operations required in this case is 2.
Note that the array cannot be converted to \[**2**,2,2,2,2\] because in this case nums\[0\] == nums\[1\] which violates the conditions of an alternating array.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 105`
Removing Minimum Number of Magic Beans

You are given an array of **positive** integers `beans`, where each integer represents the number of magic beans found in a particular magic bag.

**Remove** any number of beans (**possibly none**) from each bag such that the number of beans in each remaining **non-empty** bag (still containing **at least one** bean) is **equal**. Once a bean has been removed from a bag, you are **not** allowed to return it to any of the bags.

Return _the **minimum** number of magic beans that you have to remove_.

**Example 1:**

**Input:** beans = \[4,**1**,6,5\]
**Output:** 4
**Explanation:** 
- We remove 1 bean from the bag with only 1 bean.
  This results in the remaining bags: \[4,**0**,6,5\]
- Then we remove 2 beans from the bag with 6 beans.
  This results in the remaining bags: \[4,0,**4**,5\]
- Then we remove 1 bean from the bag with 5 beans.
  This results in the remaining bags: \[4,0,4,**4**\]
We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that remove 4 beans or fewer.

**Example 2:**

**Input:** beans = \[**2**,10,**3**,**2**\]
**Output:** 7
**Explanation:**
- We remove 2 beans from one of the bags with 2 beans.
  This results in the remaining bags: \[**0**,10,3,2\]
- Then we remove 2 beans from the other bag with 2 beans.
  This results in the remaining bags: \[0,10,3,**0**\]
- Then we remove 3 beans from the bag with 3 beans. 
  This results in the remaining bags: \[0,10,**0**,0\]
We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that removes 7 beans or fewer.

**Constraints:**

*   `1 <= beans.length <= 105`
*   `1 <= beans[i] <= 105`
Maximum AND Sum of Array

You are given an integer array `nums` of length `n` and an integer `numSlots` such that `2 * numSlots >= n`. There are `numSlots` slots numbered from `1` to `numSlots`.

You have to place all `n` integers into the slots such that each slot contains at **most** two numbers. The **AND sum** of a given placement is the sum of the **bitwise** `AND` of every number with its respective slot number.

*   For example, the **AND sum** of placing the numbers `[1, 3]` into slot `1` and `[4, 6]` into slot `2` is equal to `(1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4`.

Return _the maximum possible **AND sum** of_ `nums` _given_ `numSlots` _slots._

**Example 1:**

**Input:** nums = \[1,2,3,4,5,6\], numSlots = 3
**Output:** 9
**Explanation:** One possible placement is \[1, 4\] into slot 1, \[2, 6\] into slot 2, and \[3, 5\] into slot 3. 
This gives the maximum AND sum of (1 AND 1) + (4 AND 1) + (2 AND 2) + (6 AND 2) + (3 AND 3) + (5 AND 3) = 1 + 0 + 2 + 2 + 3 + 1 = 9.

**Example 2:**

**Input:** nums = \[1,3,10,4,7,1\], numSlots = 9
**Output:** 24
**Explanation:** One possible placement is \[1, 1\] into slot 1, \[3\] into slot 3, \[4\] into slot 4, \[7\] into slot 7, and \[10\] into slot 9.
This gives the maximum AND sum of (1 AND 1) + (1 AND 1) + (3 AND 3) + (4 AND 4) + (7 AND 7) + (10 AND 9) = 1 + 1 + 3 + 4 + 7 + 8 = 24.
Note that slots 2, 5, 6, and 8 are empty which is permitted.

**Constraints:**

*   `n == nums.length`
*   `1 <= numSlots <= 9`
*   `1 <= n <= 2 * numSlots`
*   `1 <= nums[i] <= 15`