bowang0911/leetcode huggingface dataset:
Adding Spaces to a String

You are given a **0-indexed** string `s` and a **0-indexed** integer array `spaces` that describes the indices in the original string where spaces will be added. Each space should be inserted **before** the character at the given index.

*   For example, given `s = "EnjoyYourCoffee "` and `spaces = [5, 9]`, we place spaces before `'Y'` and `'C'`, which are at indices `5` and `9` respectively. Thus, we obtain `"Enjoy **Y**our **C**offee "`.

Return _the modified string **after** the spaces have been added._

**Example 1:**

**Input:** s =  "LeetcodeHelpsMeLearn ", spaces = \[8,13,15\]
**Output:**  "Leetcode Helps Me Learn "
**Explanation:** 
The indices 8, 13, and 15 correspond to the underlined characters in  "Leetcode**H**elps**M**e**L**earn ".
We then place spaces before those characters.

**Example 2:**

**Input:** s =  "icodeinpython ", spaces = \[1,5,7,9\]
**Output:**  "i code in py thon "
**Explanation:**
The indices 1, 5, 7, and 9 correspond to the underlined characters in  "i**c**ode**i**n**p**y**t**hon ".
We then place spaces before those characters.

**Example 3:**

**Input:** s =  "spacing ", spaces = \[0,1,2,3,4,5,6\]
**Output:**  " s p a c i n g "
**Explanation:**
We are also able to place spaces before the first character of the string.

**Constraints:**

*   `1 <= s.length <= 3 * 105`
*   `s` consists only of lowercase and uppercase English letters.
*   `1 <= spaces.length <= 3 * 105`
*   `0 <= spaces[i] <= s.length - 1`
*   All the values of `spaces` are **strictly increasing**.
Number of Smooth Descent Periods of a Stock

You are given an integer array `prices` representing the daily price history of a stock, where `prices[i]` is the stock price on the `ith` day.

A **smooth descent period** of a stock consists of **one or more contiguous** days such that the price on each day is **lower** than the price on the **preceding day** by **exactly** `1`. The first day of the period is exempted from this rule.

Return _the number of **smooth descent periods**_.

**Example 1:**

**Input:** prices = \[3,2,1,4\]
**Output:** 7
**Explanation:** There are 7 smooth descent periods:
\[3\], \[2\], \[1\], \[4\], \[3,2\], \[2,1\], and \[3,2,1\]
Note that a period with one day is a smooth descent period by the definition.

**Example 2:**

**Input:** prices = \[8,6,7,7\]
**Output:** 4
**Explanation:** There are 4 smooth descent periods: \[8\], \[6\], \[7\], and \[7\]
Note that \[8,6\] is not a smooth descent period as 8 - 6 ≠ 1.

**Example 3:**

**Input:** prices = \[1\]
**Output:** 1
**Explanation:** There is 1 smooth descent period: \[1\]

**Constraints:**

*   `1 <= prices.length <= 105`
*   `1 <= prices[i] <= 105`
Minimum Operations to Make the Array K-Increasing

You are given a **0-indexed** array `arr` consisting of `n` positive integers, and a positive integer `k`.

The array `arr` is called **K-increasing** if `arr[i-k] <= arr[i]` holds for every index `i`, where `k <= i <= n-1`.

*   For example, `arr = [4, 1, 5, 2, 6, 2]` is K-increasing for `k = 2` because:
    *   `arr[0] <= arr[2] (4 <= 5)`
    *   `arr[1] <= arr[3] (1 <= 2)`
    *   `arr[2] <= arr[4] (5 <= 6)`
    *   `arr[3] <= arr[5] (2 <= 2)`
*   However, the same `arr` is not K-increasing for `k = 1` (because `arr[0] > arr[1]`) or `k = 3` (because `arr[0] > arr[3]`).

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

Return _the **minimum number of operations** required to make the array K-increasing for the given_ `k`.

**Example 1:**

**Input:** arr = \[5,4,3,2,1\], k = 1
**Output:** 4
**Explanation:**
For k = 1, the resultant array has to be non-decreasing.
Some of the K-increasing arrays that can be formed are \[5,**6**,**7**,**8**,**9**\], \[**1**,**1**,**1**,**1**,1\], \[**2**,**2**,3,**4**,**4**\]. All of them require 4 operations.
It is suboptimal to change the array to, for example, \[**6**,**7**,**8**,**9**,**10**\] because it would take 5 operations.
It can be shown that we cannot make the array K-increasing in less than 4 operations.

**Example 2:**

**Input:** arr = \[4,1,5,2,6,2\], k = 2
**Output:** 0
**Explanation:**
This is the same example as the one in the problem description.
Here, for every index i where 2 <= i <= 5, arr\[i-2\] <= arr\[i\].
Since the given array is already K-increasing, we do not need to perform any operations.

**Example 3:**

**Input:** arr = \[4,1,5,2,6,2\], k = 3
**Output:** 2
**Explanation:**
Indices 3 and 5 are the only ones not satisfying arr\[i-3\] <= arr\[i\] for 3 <= i <= 5.
One of the ways we can make the array K-increasing is by changing arr\[3\] to 4 and arr\[5\] to 5.
The array will now be \[4,1,5,**4**,6,**5**\].
Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.

**Constraints:**

*   `1 <= arr.length <= 105`
*   `1 <= arr[i], k <= arr.length`
Elements in Array After Removing and Replacing Elements

You are given a **0-indexed** integer array `nums`. Initially on minute `0`, the array is unchanged. Every minute, the **leftmost** element in `nums` is removed until no elements remain. Then, every minute, one element is appended to the **end** of `nums`, in the order they were removed in, until the original array is restored. This process repeats indefinitely.

*   For example, the array `[0,1,2]` would change as follows: `[0,1,2] -> [1,2] -> [2] -> [] -> [0] -> [0,1] -> [0,1,2] -> [1,2] -> [2] -> [] -> [0] -> [0,1] -> [0,1,2] -> ...`

You are also given a 2D integer array `queries` of size `n` where `queries[j] = [timej, indexj]`. The answer to the `jth` query is:

*   `nums[indexj]` if `indexj < nums.length` at minute `timej`
*   `-1` if `indexj >= nums.length` at minute `timej`

Return _an integer array `ans` of size_ `n` _where_ `ans[j]` _is the answer to the_ `jth` _query_.

**Example 1:**

**Input:** nums = \[0,1,2\], queries = \[\[0,2\],\[2,0\],\[3,2\],\[5,0\]\]
**Output:** \[2,2,-1,0\]
**Explanation:**
Minute 0: \[0,1,2\] - All elements are in the nums.
Minute 1: \[1,2\]   - The leftmost element, 0, is removed.
Minute 2: \[2\]     - The leftmost element, 1, is removed.
Minute 3: \[\]      - The leftmost element, 2, is removed.
Minute 4: \[0\]     - 0 is added to the end of nums.
Minute 5: \[0,1\]   - 1 is added to the end of nums.

At minute 0, nums\[2\] is 2.
At minute 2, nums\[0\] is 2.
At minute 3, nums\[2\] does not exist.
At minute 5, nums\[0\] is 0.

**Example 2:**

**Input:** nums = \[2\], queries = \[\[0,0\],\[1,0\],\[2,0\],\[3,0\]\]
**Output:** \[2,-1,2,-1\]
Minute 0: \[2\] - All elements are in the nums.
Minute 1: \[\]  - The leftmost element, 2, is removed.
Minute 2: \[2\] - 2 is added to the end of nums.
Minute 3: \[\]  - The leftmost element, 2, is removed.

At minute 0, nums\[0\] is 2.
At minute 1, nums\[0\] does not exist.
At minute 2, nums\[0\] is 2.
At minute 3, nums\[0\] does not exist.

**Constraints:**

*   `1 <= nums.length <= 100`
*   `0 <= nums[i] <= 100`
*   `n == queries.length`
*   `1 <= n <= 105`
*   `queries[j].length == 2`
*   `0 <= timej <= 105`
*   `0 <= indexj < nums.length`
Maximum Number of Words Found in Sentences

A **sentence** is a list of **words** that are separated by a single space with no leading or trailing spaces.

You are given an array of strings `sentences`, where each `sentences[i]` represents a single **sentence**.

Return _the **maximum number of words** that appear in a single sentence_.

**Example 1:**

**Input:** sentences = \[ "alice and bob love leetcode ",  "i think so too ",  "this is great thanks very much "\]
**Output:** 6
**Explanation:** 
- The first sentence,  "alice and bob love leetcode ", has 5 words in total.
- The second sentence,  "i think so too ", has 4 words in total.
- The third sentence,  "this is great thanks very much ", has 6 words in total.
Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.

**Example 2:**

**Input:** sentences = \[ "please wait ",  "continue to fight ",  "continue to win "\]
**Output:** 3
**Explanation:** It is possible that multiple sentences contain the same number of words. 
In this example, the second and third sentences (underlined) have the same number of words.

**Constraints:**

*   `1 <= sentences.length <= 100`
*   `1 <= sentences[i].length <= 100`
*   `sentences[i]` consists only of lowercase English letters and `' '` only.
*   `sentences[i]` does not have leading or trailing spaces.
*   All the words in `sentences[i]` are separated by a single space.
Find All Possible Recipes from Given Supplies

You have information about `n` different recipes. You are given a string array `recipes` and a 2D string array `ingredients`. The `ith` recipe has the name `recipes[i]`, and you can **create** it if you have **all** the needed ingredients from `ingredients[i]`. Ingredients to a recipe may need to be created from **other** recipes, i.e., `ingredients[i]` may contain a string that is in `recipes`.

You are also given a string array `supplies` containing all the ingredients that you initially have, and you have an infinite supply of all of them.

Return _a list of all the recipes that you can create._ You may return the answer in **any order**.

Note that two recipes may contain each other in their ingredients.

**Example 1:**

**Input:** recipes = \[ "bread "\], ingredients = \[\[ "yeast ", "flour "\]\], supplies = \[ "yeast ", "flour ", "corn "\]
**Output:** \[ "bread "\]
**Explanation:**
We can create  "bread " since we have the ingredients  "yeast " and  "flour ".

**Example 2:**

**Input:** recipes = \[ "bread ", "sandwich "\], ingredients = \[\[ "yeast ", "flour "\],\[ "bread ", "meat "\]\], supplies = \[ "yeast ", "flour ", "meat "\]
**Output:** \[ "bread ", "sandwich "\]
**Explanation:**
We can create  "bread " since we have the ingredients  "yeast " and  "flour ".
We can create  "sandwich " since we have the ingredient  "meat " and can create the ingredient  "bread ".

**Example 3:**

**Input:** recipes = \[ "bread ", "sandwich ", "burger "\], ingredients = \[\[ "yeast ", "flour "\],\[ "bread ", "meat "\],\[ "sandwich ", "meat ", "bread "\]\], supplies = \[ "yeast ", "flour ", "meat "\]
**Output:** \[ "bread ", "sandwich ", "burger "\]
**Explanation:**
We can create  "bread " since we have the ingredients  "yeast " and  "flour ".
We can create  "sandwich " since we have the ingredient  "meat " and can create the ingredient  "bread ".
We can create  "burger " since we have the ingredient  "meat " and can create the ingredients  "bread " and  "sandwich ".

**Constraints:**

*   `n == recipes.length == ingredients.length`
*   `1 <= n <= 100`
*   `1 <= ingredients[i].length, supplies.length <= 100`
*   `1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10`
*   `recipes[i], ingredients[i][j]`, and `supplies[k]` consist only of lowercase English letters.
*   All the values of `recipes` and `supplies` combined are unique.
*   Each `ingredients[i]` does not contain any duplicate values.
Check if a Parentheses String Can Be Valid

A parentheses string is a **non-empty** string consisting only of `'('` and `')'`. It is valid if **any** of the following conditions is **true**:

*   It is `()`.
*   It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid parentheses strings.
*   It can be written as `(A)`, where `A` is a valid parentheses string.

You are given a parentheses string `s` and a string `locked`, both of length `n`. `locked` is a binary string consisting only of `'0'`s and `'1'`s. For **each** index `i` of `locked`,

*   If `locked[i]` is `'1'`, you **cannot** change `s[i]`.
*   But if `locked[i]` is `'0'`, you **can** change `s[i]` to either `'('` or `')'`.

Return `true` _if you can make `s` a valid parentheses string_. Otherwise, return `false`.

**Example 1:**

**Input:** s =  "))())) ", locked =  "010100 "
**Output:** true
**Explanation:** locked\[1\] == '1' and locked\[3\] == '1', so we cannot change s\[1\] or s\[3\].
We change s\[0\] and s\[4\] to '(' while leaving s\[2\] and s\[5\] unchanged to make s valid.

**Example 2:**

**Input:** s =  "()() ", locked =  "0000 "
**Output:** true
**Explanation:** We do not need to make any changes because s is already valid.

**Example 3:**

**Input:** s =  ") ", locked =  "0 "
**Output:** false
**Explanation:** locked permits us to change s\[0\]. 
Changing s\[0\] to either '(' or ')' will not make s valid.

**Constraints:**

*   `n == s.length == locked.length`
*   `1 <= n <= 105`
*   `s[i]` is either `'('` or `')'`.
*   `locked[i]` is either `'0'` or `'1'`.

x if x >= 0, or -x if x < 0.
Abbreviating the Product of a Range

You are given two positive integers `left` and `right` with `left <= right`. Calculate the **product** of all integers in the **inclusive** range `[left, right]`.

Since the product may be very large, you will **abbreviate** it following these steps:

1.  Count all **trailing** zeros in the product and **remove** them. Let us denote this count as `C`.
    *   For example, there are `3` trailing zeros in `1000`, and there are `0` trailing zeros in `546`.
2.  Denote the remaining number of digits in the product as `d`. If `d > 10`, then express the product as
    
        ...
    
3.  Finally, represent the product as a **string** `"`
    
        ...eC "
    

Return _a string denoting the **abbreviated product** of all integers in the **inclusive** range_ `[left, right]`.

**Example 1:**

**Input:** left = 1, right = 4
**Output:**  "24e0 "
**Explanation:** The product is 1 \* 2 \* 3 \* 4 = 24.
There are no trailing zeros, so 24 remains the same. The abbreviation will end with  "e0 ".
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is  "24e0 ".

**Example 2:**

**Input:** left = 2, right = 11
**Output:**  "399168e2 "
**Explanation:** The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with  "e2 ".
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is  "399168e2 ".

**Example 3:**

**Input:** left = 371, right = 375
**Output:**  "7219856259e3 "
**Explanation:** The product is 7219856259000.

**Constraints:**

*   `1 <= left <= right <= 104`
A Number After a Double Reversal

**Reversing** an integer means to reverse all its digits.

*   For example, reversing `2021` gives `1202`. Reversing `12300` gives `321` as the **leading zeros are not retained**.

Given an integer `num`, **reverse** `num` to get `reversed1`, **then reverse** `reversed1` to get `reversed2`. Return `true` _if_ `reversed2` _equals_ `num`. Otherwise return `false`.

**Example 1:**

**Input:** num = 526
**Output:** true
**Explanation:** Reverse num to get 625, then reverse 625 to get 526, which equals num.

**Example 2:**

**Input:** num = 1800
**Output:** false
**Explanation:** Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

**Example 3:**

**Input:** num = 0
**Output:** true
**Explanation:** Reverse num to get 0, then reverse 0 to get 0, which equals num.

**Constraints:**

*   `0 <= num <= 106`
Execution of All Suffix Instructions Staying in a Grid

There is an `n x n` grid, with the top-left cell at `(0, 0)` and the bottom-right cell at `(n - 1, n - 1)`. You are given the integer `n` and an integer array `startPos` where `startPos = [startrow, startcol]` indicates that a robot is initially at cell `(startrow, startcol)`.

You are also given a **0-indexed** string `s` of length `m` where `s[i]` is the `ith` instruction for the robot: `'L'` (move left), `'R'` (move right), `'U'` (move up), and `'D'` (move down).

The robot can begin executing from any `ith` instruction in `s`. It executes the instructions one by one towards the end of `s` but it stops if either of these conditions is met:

*   The next instruction will move the robot off the grid.
*   There are no more instructions left to execute.

Return _an array_ `answer` _of length_ `m` _where_ `answer[i]` _is **the number of instructions** the robot can execute if the robot **begins executing from** the_ `ith` _instruction in_ `s`.

**Example 1:**

**Input:** n = 3, startPos = \[0,1\], s =  "RRDDLU "
**Output:** \[1,5,4,3,1,0\]
**Explanation:** Starting from startPos and beginning execution from the ith instruction:
- 0th:  "**R**RDDLU ". Only one instruction  "R " can be executed before it moves off the grid.
- 1st:   "**RDDLU** ". All five instructions can be executed while it stays in the grid and ends at (1, 1).
- 2nd:    "**DDLU** ". All four instructions can be executed while it stays in the grid and ends at (1, 0).
- 3rd:     "**DLU** ". All three instructions can be executed while it stays in the grid and ends at (0, 0).
- 4th:      "**L**U ". Only one instruction  "L " can be executed before it moves off the grid.
- 5th:       "U ". If moving up, it would move off the grid.

**Example 2:**

**Input:** n = 2, startPos = \[1,1\], s =  "LURD "
**Output:** \[4,1,0,0\]
**Explanation:**
- 0th:  "**LURD** ".
- 1st:   "**U**RD ".
- 2nd:    "RD ".
- 3rd:     "D ".

**Example 3:**

**Input:** n = 1, startPos = \[0,0\], s =  "LRUD "
**Output:** \[0,0,0,0\]
**Explanation:** No matter which instruction the robot begins execution from, it would move off the grid.

**Constraints:**

*   `m == s.length`
*   `1 <= n, m <= 500`
*   `startPos.length == 2`
*   `0 <= startrow, startcol < n`
*   `s` consists of `'L'`, `'R'`, `'U'`, and `'D'`.
Intervals Between Identical Elements

You are given a **0-indexed** array of `n` integers `arr`.

The **interval** between two elements in `arr` is defined as the **absolute difference** between their indices. More formally, the **interval** between `arr[i]` and `arr[j]` is `|i - j|`.

Return _an array_ `intervals` _of length_ `n` _where_ `intervals[i]` _is **the sum of intervals** between_ `arr[i]` _and each element in_ `arr` _with the same value as_ `arr[i]`_._

**Note:** `|x|` is the absolute value of `x`.

**Example 1:**

**Input:** arr = \[2,1,3,1,2,3,3\]
**Output:** \[4,2,7,2,4,4,5\]
**Explanation:**
- Index 0: Another 2 is found at index 4. |0 - 4| = 4
- Index 1: Another 1 is found at index 3. |1 - 3| = 2
- Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7
- Index 3: Another 1 is found at index 1. |3 - 1| = 2
- Index 4: Another 2 is found at index 0. |4 - 0| = 4
- Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4
- Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5

**Example 2:**

**Input:** arr = \[10,5,10,10\]
**Output:** \[5,0,3,4\]
**Explanation:**
- Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5
- Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
- Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3
- Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4

**Constraints:**

*   `n == arr.length`
*   `1 <= n <= 105`
*   `1 <= arr[i] <= 105`
Minimum Operations to Remove Adjacent Ones in Matrix

You are given a **0-indexed** binary matrix `grid`. In one operation, you can flip any `1` in `grid` to be `0`.

A binary matrix is **well-isolated** if there is no `1` in the matrix that is **4-directionally connected** (i.e., horizontal and vertical) to another `1`.

Return _the minimum number of operations to make_ `grid` _**well-isolated**_.

**Example 1:**

**Input:** grid = \[\[1,1,0\],\[0,1,1\],\[1,1,1\]\]
**Output:** 3
**Explanation:** Use 3 operations to change grid\[0\]\[1\], grid\[1\]\[2\], and grid\[2\]\[1\] to 0.
After, no more 1's are 4-directionally connected and grid is well-isolated.

**Example 2:**

**Input:** grid = \[\[0,0,0\],\[0,0,0\],\[0,0,0\]\]
**Output:** 0
**Explanation:** There are no 1's in grid and it is well-isolated.
No operations were done so return 0.

**Example 3:**

**Input:** grid = \[\[0,1\],\[1,0\]\]
**Output:** 0
**Explanation:** None of the 1's are 4-directionally connected and grid is well-isolated.
No operations were done so return 0.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `1 <= m, n <= 300`
*   `grid[i][j]` is either `0` or `1`.
Check if All A's Appears Before All B's

Given a string `s` consisting of **only** the characters `'a'` and `'b'`, return `true` _if **every**_ `'a'` _appears before **every**_ `'b'` _in the string_. Otherwise, return `false`.

**Example 1:**

**Input:** s =  "aaabbb "
**Output:** true
**Explanation:**
The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
Hence, every 'a' appears before every 'b' and we return true.

**Example 2:**

**Input:** s =  "abab "
**Output:** false
**Explanation:**
There is an 'a' at index 2 and a 'b' at index 1.
Hence, not every 'a' appears before every 'b' and we return false.

**Example 3:**

**Input:** s =  "bbb "
**Output:** true
**Explanation:**
There are no 'a's, hence, every 'a' appears before every 'b' and we return true.

**Constraints:**

*   `1 <= s.length <= 100`
*   `s[i]` is either `'a'` or `'b'`.
Number of Laser Beams in a Bank

Anti-theft security devices are activated inside a bank. You are given a **0-indexed** binary string array `bank` representing the floor plan of the bank, which is an `m x n` 2D matrix. `bank[i]` represents the `ith` row, consisting of `'0'`s and `'1'`s. `'0'` means the cell is empty, while`'1'` means the cell has a security device.

There is **one** laser beam between any **two** security devices **if both** conditions are met:

*   The two devices are located on two **different rows**: `r1` and `r2`, where `r1 < r2`.
*   For **each** row `i` where `r1 < i < r2`, there are **no security devices** in the `ith` row.

Laser beams are independent, i.e., one beam does not interfere nor join with another.

Return _the total number of laser beams in the bank_.

**Example 1:**

**Input:** bank = \[ "011001 ", "000000 ", "010100 ", "001000 "\]
**Output:** 8
**Explanation:** Between each of the following device pairs, there is one beam. In total, there are 8 beams:
 \* bank\[0\]\[1\] -- bank\[2\]\[1\]
 \* bank\[0\]\[1\] -- bank\[2\]\[3\]
 \* bank\[0\]\[2\] -- bank\[2\]\[1\]
 \* bank\[0\]\[2\] -- bank\[2\]\[3\]
 \* bank\[0\]\[5\] -- bank\[2\]\[1\]
 \* bank\[0\]\[5\] -- bank\[2\]\[3\]
 \* bank\[2\]\[1\] -- bank\[3\]\[2\]
 \* bank\[2\]\[3\] -- bank\[3\]\[2\]
Note that there is no beam between any device on the 0th row with any on the 3rd row.
This is because the 2nd row contains security devices, which breaks the second condition.

**Example 2:**

**Input:** bank = \[ "000 ", "111 ", "000 "\]
**Output:** 0
**Explanation:** There does not exist two devices located on two different rows.

**Constraints:**

*   `m == bank.length`
*   `n == bank[i].length`
*   `1 <= m, n <= 500`
*   `bank[i][j]` is either `'0'` or `'1'`.