ShwStone/humaneval-leetcode huggingface dataset:
def equalDigitFrequency(s: str) -> int:
    """
    Given a digit string s, return the number of unique substrings of s where every digit appears the same number of times.
    
    Example 1:
    
    >>> equalDigitFrequency(s = "1212")
    >>> 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:
    
    >>> equalDigitFrequency(s = "12321")
    >>> 9
    Explanation: The substrings that meet the requirements are "1", "2", "3", "12", "23", "32", "21", "123", "321".
    """
def countOperations(num1: int, num2: int) -> int:
    """
    You are given two non-negative integers num1 and num2.
    In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.
    
    For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1.
    
    Return the number of operations required to make either num1 = 0 or num2 = 0.
    
    Example 1:
    
    >>> countOperations(num1 = 2, num2 = 3)
    >>> 3
    Explanation:
    - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
    - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
    - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
    Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
    So the total number of operations required is 3.
    
    Example 2:
    
    >>> countOperations(num1 = 10, num2 = 10)
    >>> 1
    Explanation:
    - Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
    Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
    So the total number of operations required is 1.
    """
def minimumOperations(nums: List[int]) -> int:
    """
    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:
    
    >>> minimumOperations(nums = [3,1,3,2,4,3])
    >>> 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:
    
    >>> minimumOperations(nums = [1,2,2,2,2])
    >>> 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.
    """
def minimumRemoval(beans: List[int]) -> int:
    """
    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:
    
    >>> minimumRemoval(beans = [4,1,6,5])
    >>> 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:
    
    >>> minimumRemoval(beans = [2,10,3,2])
    >>> 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.
    """
def maximumANDSum(nums: List[int], numSlots: int) -> int:
    """
    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:
    
    >>> maximumANDSum(nums = [1,2,3,4,5,6], numSlots = 3)
    >>> 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:
    
    >>> maximumANDSum(nums = [1,3,10,4,7,1], numSlots = 9)
    >>> 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.
    """
def removeOnes(grid: List[List[int]]) -> int:
    """
    You are given a 0-indexed m x n binary matrix grid.
    In one operation, you can choose any i and j that meet the following conditions:
    
    0 <= i < m
    0 <= j < n
    grid[i][j] == 1
    
    and change the values of all cells in row i and column j to zero.
    Return the minimum number of operations needed to remove all 1's from grid.
    
    Example 1:
    
    
    >>> removeOnes(grid = [[1,1,1],[1,1,1],[0,1,0]])
    >>> 2
    Explanation:
    In the first operation, change all cell values of row 1 and column 1 to zero.
    In the second operation, change all cell values of row 0 and column 0 to zero.
    
    Example 2:
    
    
    >>> removeOnes(grid = [[0,1,0],[1,0,1],[0,1,0]])
    >>> 2
    Explanation:
    In the first operation, change all cell values of row 1 and column 0 to zero.
    In the second operation, change all cell values of row 2 and column 1 to zero.
    Note that we cannot perform an operation using row 1 and column 1 because grid[1][1] != 1.
    
    Example 3:
    
    
    >>> removeOnes(grid = [[0,0],[0,0]])
    >>> 0
    Explanation:
    There are no 1's to remove so return 0.
    """
def countPairs(nums: List[int], k: int) -> int:
    """
    Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.
    
    Example 1:
    
    >>> countPairs(nums = [3,1,2,2,2,1,3], k = 2)
    >>> 4
    Explanation:
    There are 4 pairs that meet all the requirements:
    - nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
    - nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
    - nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
    - nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
    
    Example 2:
    
    >>> countPairs(nums = [1,2,3,4], k = 1)
    >>> 0
    Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
    """
def sumOfThree(num: int) -> List[int]:
    """
    Given an integer num, return three consecutive integers (as a sorted array) that sum to num. If num cannot be expressed as the sum of three consecutive integers, return an empty array.
    
    Example 1:
    
    >>> sumOfThree(num = 33)
    >>> [10,11,12]
    Explanation: 33 can be expressed as 10 + 11 + 12 = 33.
    10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
    
    Example 2:
    
    >>> sumOfThree(num = 4)
    >>> []
    Explanation: There is no way to express 4 as the sum of 3 consecutive integers.
    """
def maximumEvenSplit(finalSum: int) -> List[int]:
    """
    You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.
    
    For example, given finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (12), (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the maximum number of integers. Note that finalSum cannot be split into (2 + 2 + 4 + 4) as all the numbers should be unique.
    
    Return a list of integers that represent a valid split containing a maximum number of integers. If no valid split exists for finalSum, return an empty list. You may return the integers in any order.
    
    Example 1:
    
    >>> maximumEvenSplit(finalSum = 12)
    >>> [2,4,6]
    Explanation: The following are valid splits: (12), (2 + 10), (2 + 4 + 6), and (4 + 8).
    (2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].
    Note that [2,6,4], [6,2,4], etc. are also accepted.
    
    Example 2:
    
    >>> maximumEvenSplit(finalSum = 7)
    >>> []
    Explanation: There are no valid splits for the given finalSum.
    Thus, we return an empty array.
    
    Example 3:
    
    >>> maximumEvenSplit(finalSum = 28)
    >>> [6,8,2,12]
    Explanation: The following are valid splits: (2 + 26), (6 + 8 + 2 + 12), and (4 + 24).
    (6 + 8 + 2 + 12) has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
    Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
    """
def goodTriplets(nums1: List[int], nums2: List[int]) -> int:
    """
    You are given two 0-indexed arrays nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1].
    A good triplet is a set of 3 distinct values which are present in increasing order by position both in nums1 and nums2. In other words, if we consider pos1v as the index of the value v in nums1 and pos2v as the index of the value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that pos1x < pos1y < pos1z and pos2x < pos2y < pos2z.
    Return the total number of good triplets.
    
    Example 1:
    
    >>> goodTriplets(nums1 = [2,0,1,3], nums2 = [0,1,2,3])
    >>> 1
    Explanation:
    There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
    Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.
    
    Example 2:
    
    >>> goodTriplets(nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3])
    >>> 4
    Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
    """
def countEven(num: int) -> int:
    """
    Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.
    The digit sum of a positive integer is the sum of all its digits.
    
    Example 1:
    
    >>> countEven(num = 4)
    >>> 2
    Explanation:
    The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
    
    Example 2:
    
    >>> countEven(num = 30)
    >>> 14
    Explanation:
    The 14 integers less than or equal to 30 whose digit sums are even are
    2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeNodes(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0.
    For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's.
    Return the head of the modified linked list.
    
    Example 1:
    
    
    >>> __init__(head = [0,3,1,0,4,5,2,0])
    >>> [4,11]
    Explanation:
    The above figure represents the given linked list. The modified list contains
    - The sum of the nodes marked in green: 3 + 1 = 4.
    - The sum of the nodes marked in red: 4 + 5 + 2 = 11.
    
    Example 2:
    
    
    >>> __init__(head = [0,1,0,3,0,2,2,0])
    >>> [1,3,4]
    Explanation:
    The above figure represents the given linked list. The modified list contains
    - The sum of the nodes marked in green: 1 = 1.
    - The sum of the nodes marked in red: 3 = 3.
    - The sum of the nodes marked in yellow: 2 + 2 = 4.
    """
def repeatLimitedString(s: str, repeatLimit: int) -> str:
    """
    You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s.
    Return the lexicographically largest repeatLimitedString possible.
    A string a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one.
    
    Example 1:
    
    >>> repeatLimitedString(s = "cczazcc", repeatLimit = 3)
    >>> "zzcccac"
    Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac".
    The letter 'a' appears at most 1 time in a row.
    The letter 'c' appears at most 3 times in a row.
    The letter 'z' appears at most 2 times in a row.
    Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
    The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
    Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
    
    Example 2:
    
    >>> repeatLimitedString(s = "aababab", repeatLimit = 2)
    >>> "bbabaa"
    Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
    The letter 'a' appears at most 2 times in a row.
    The letter 'b' appears at most 2 times in a row.
    Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
    The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
    Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
    """
def countPairs(nums: List[int], k: int) -> int:
    """
    Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that:
    
    0 <= i < j <= n - 1 and
    nums[i] * nums[j] is divisible by k.
    
    
    Example 1:
    
    >>> countPairs(nums = [1,2,3,4,5], k = 2)
    >>> 7
    Explanation:
    The 7 pairs of indices whose corresponding products are divisible by 2 are
    (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
    Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
    Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
    
    Example 2:
    
    >>> countPairs(nums = [1,2,3,4], k = 5)
    >>> 0
    Explanation: There does not exist any pair of indices whose corresponding product is divisible by 5.
    """
def buildWall(height: int, width: int, bricks: List[int]) -> int:
    """
    You are given integers height and width which specify the dimensions of a brick wall you are building. You are also given a 0-indexed array of unique integers bricks, where the ith brick has a height of 1 and a width of bricks[i]. You have an infinite supply of each type of brick and bricks may not be rotated.
    Each row in the wall must be exactly width units long. For the wall to be sturdy, adjacent rows in the wall should not join bricks at the same location, except at the ends of the wall.
    Return the number of ways to build a sturdy wall. Since the answer may be very large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> buildWall(height = 2, width = 3, bricks = [1,2])
    >>> 2
    Explanation:
    The first two walls in the diagram show the only two ways to build a sturdy brick wall.
    Note that the third wall in the diagram is not sturdy because adjacent rows join bricks 2 units from the left.
    
    Example 2:
    
    >>> buildWall(height = 1, width = 1, bricks = [5])
    >>> 0
    Explanation:
    There are no ways to build a sturdy wall because the only type of brick we have is longer than the width of the wall.
    """
def prefixCount(words: List[str], pref: str) -> int:
    """
    You are given an array of strings words and a string pref.
    Return the number of strings in words that contain pref as a prefix.
    A prefix of a string s is any leading contiguous substring of s.
    
    Example 1:
    
    >>> prefixCount(words = ["pay","attention","practice","attend"], pref = "at")
    >>> 2
    Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
    
    Example 2:
    
    >>> prefixCount(words = ["leetcode","win","loops","success"], pref = "code")
    >>> 0
    Explanation: There are no strings that contain "code" as a prefix.
    """
def minSteps(s: str, t: str) -> int:
    """
    You are given two strings s and t. In one step, you can append any character to either s or t.
    Return the minimum number of steps to make s and t anagrams of each other.
    An anagram of a string is a string that contains the same characters with a different (or the same) ordering.
    
    Example 1:
    
    >>> minSteps(s = "leetcode", t = "coats")
    >>> 7
    Explanation:
    - In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcodeas".
    - In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coatsleede".
    "leetcodeas" and "coatsleede" are now anagrams of each other.
    We used a total of 2 + 5 = 7 steps.
    It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
    
    Example 2:
    
    >>> minSteps(s = "night", t = "thing")
    >>> 0
    Explanation: The given strings are already anagrams of each other. Thus, we do not need any further steps.
    """
def minimumTime(time: List[int], totalTrips: int) -> int:
    """
    You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip.
    Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip. Also, each bus operates independently; that is, the trips of one bus do not influence the trips of any other bus.
    You are also given an integer totalTrips, which denotes the number of trips all buses should make in total. Return the minimum time required for all buses to complete at least totalTrips trips.
    
    Example 1:
    
    >>> minimumTime(time = [1,2,3], totalTrips = 5)
    >>> 3
    Explanation:
    - At time t = 1, the number of trips completed by each bus are [1,0,0].
      The total number of trips completed is 1 + 0 + 0 = 1.
    - At time t = 2, the number of trips completed by each bus are [2,1,0].
      The total number of trips completed is 2 + 1 + 0 = 3.
    - At time t = 3, the number of trips completed by each bus are [3,1,1].
      The total number of trips completed is 3 + 1 + 1 = 5.
    So the minimum time needed for all buses to complete at least 5 trips is 3.
    
    Example 2:
    
    >>> minimumTime(time = [2], totalTrips = 1)
    >>> 2
    Explanation:
    There is only one bus, and it will complete its first trip at t = 2.
    So the minimum time needed to complete 1 trip is 2.
    """
def minimumFinishTime(tires: List[List[int]], changeTime: int, numLaps: int) -> int:
    """
    You are given a 0-indexed 2D integer array tires where tires[i] = [fi, ri] indicates that the ith tire can finish its xth successive lap in fi * ri(x-1) seconds.
    
    For example, if fi = 3 and ri = 2, then the tire would finish its 1st lap in 3 seconds, its 2nd lap in 3 * 2 = 6 seconds, its 3rd lap in 3 * 22 = 12 seconds, etc.
    
    You are also given an integer changeTime and an integer numLaps.
    The race consists of numLaps laps and you may start the race with any tire. You have an unlimited supply of each tire and after every lap, you may change to any given tire (including the current tire type) if you wait changeTime seconds.
    Return the minimum time to finish the race.
    
    Example 1:
    
    >>> minimumFinishTime(tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4)
    >>> 21
    Explanation:
    Lap 1: Start with tire 0 and finish the lap in 2 seconds.
    Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
    Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
    Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
    Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
    The minimum time to complete the race is 21 seconds.
    
    Example 2:
    
    >>> minimumFinishTime(tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5)
    >>> 25
    Explanation:
    Lap 1: Start with tire 1 and finish the lap in 2 seconds.
    Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
    Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
    Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
    Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
    Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
    The minimum time to complete the race is 25 seconds.
    """