ShwStone/humaneval-leetcode huggingface dataset:
def wordSquares(words: List[str]) -> List[List[str]]:
    """
    Given an array of unique strings words, return all the word squares you can build from words. The same word from words can be used multiple times. You can return the answer in any order.
    A sequence of strings forms a valid word square if the kth row and column read the same string, where 0 <= k < max(numRows, numColumns).
    
    For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically.
    
    
    Example 1:
    
    >>> wordSquares(words = ["area","lead","wall","lady","ball"])
    >>> [["ball","area","lead","lady"],["wall","area","lead","lady"]]
    Explanation:
    The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
    
    Example 2:
    
    >>> wordSquares(words = ["abat","baba","atan","atal"])
    >>> [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]
    Explanation:
    The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
    """
def minMutation(startGene: str, endGene: str, bank: List[str]) -> int:
    """
    A gene string can be represented by an 8-character long string, with choices from 'A', 'C', 'G', and 'T'.
    Suppose we need to investigate a mutation from a gene string startGene to a gene string endGene where one mutation is defined as one single character changed in the gene string.
    
    For example, "AACCGGTT" --> "AACCGGTA" is one mutation.
    
    There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string.
    Given the two gene strings startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such a mutation, return -1.
    Note that the starting point is assumed to be valid, so it might not be included in the bank.
    
    Example 1:
    
    >>> minMutation(startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"])
    >>> 1
    
    Example 2:
    
    >>> minMutation(startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"])
    >>> 2
    """
def countSegments(s: str) -> int:
    """
    Given a string s, return the number of segments in the string.
    A segment is defined to be a contiguous sequence of non-space characters.
    
    Example 1:
    
    >>> countSegments(s = "Hello, my name is John")
    >>> 5
    Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]
    
    Example 2:
    
    >>> countSegments(s = "Hello")
    >>> 1
    """
def eraseOverlapIntervals(intervals: List[List[int]]) -> int:
    """
    Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
    Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.
    
    Example 1:
    
    >>> eraseOverlapIntervals(intervals = [[1,2],[2,3],[3,4],[1,3]])
    >>> 1
    Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
    
    Example 2:
    
    >>> eraseOverlapIntervals(intervals = [[1,2],[1,2],[1,2]])
    >>> 2
    Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
    
    Example 3:
    
    >>> eraseOverlapIntervals(intervals = [[1,2],[2,3]])
    >>> 0
    Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
    """
def findRightInterval(intervals: List[List[int]]) -> List[int]:
    """
    You are given an array of intervals, where intervals[i] = [starti, endi] and each starti is unique.
    The right interval for an interval i is an interval j such that startj >= endi and startj is minimized. Note that i may equal j.
    Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
    
    Example 1:
    
    >>> findRightInterval(intervals = [[1,2]])
    >>> [-1]
    Explanation: There is only one interval in the collection, so it outputs -1.
    
    Example 2:
    
    >>> findRightInterval(intervals = [[3,4],[2,3],[1,2]])
    >>> [-1,0,1]
    Explanation: There is no right interval for [3,4].
    The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3.
    The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2.
    
    Example 3:
    
    >>> findRightInterval(intervals = [[1,4],[2,3],[3,4]])
    >>> [-1,2,-1]
    Explanation: There is no right interval for [1,4] and [3,4].
    The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(root: Optional[TreeNode], targetSum: int) -> int:
    """
    Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.
    The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
    
    Example 1:
    
    
    >>> __init__(root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8)
    >>> 3
    Explanation: The paths that sum to 8 are shown.
    
    Example 2:
    
    >>> __init__(root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22)
    >>> 3
    """
def findAnagrams(s: str, p: str) -> List[int]:
    """
    Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
    
    Example 1:
    
    >>> findAnagrams(s = "cbaebabacd", p = "abc")
    >>> [0,6]
    Explanation:
    The substring with start index = 0 is "cba", which is an anagram of "abc".
    The substring with start index = 6 is "bac", which is an anagram of "abc".
    
    Example 2:
    
    >>> findAnagrams(s = "abab", p = "ab")
    >>> [0,1,2]
    Explanation:
    The substring with start index = 0 is "ab", which is an anagram of "ab".
    The substring with start index = 1 is "ba", which is an anagram of "ab".
    The substring with start index = 2 is "ab", which is an anagram of "ab".
    """
def parseTernary(expression: str) -> str:
    """
    Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return the result of it.
    You can always assume that the given expression is valid and only contains digits, '?', ':', 'T', and 'F' where 'T' is true and 'F' is false. All the numbers in the expression are one-digit numbers (i.e., in the range [0, 9]).
    The conditional expressions group right-to-left (as usual in most languages), and the result of the expression will always evaluate to either a digit, 'T' or 'F'.
    
    Example 1:
    
    >>> parseTernary(expression = "T?2:3")
    >>> "2"
    Explanation: If true, then result is 2; otherwise result is 3.
    
    Example 2:
    
    >>> parseTernary(expression = "F?1:T?4:5")
    >>> "4"
    Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:
    "(F ? 1 : (T ? 4 : 5))" --> "(F ? 1 : 4)" --> "4"
    or "(F ? 1 : (T ? 4 : 5))" --> "(T ? 4 : 5)" --> "4"
    
    Example 3:
    
    >>> parseTernary(expression = "T?T?F:5:3")
    >>> "F"
    Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:
    "(T ? (T ? F : 5) : 3)" --> "(T ? F : 3)" --> "F"
    "(T ? (T ? F : 5) : 3)" --> "(T ? F : 5)" --> "F"
    """
def findKthNumber(n: int, k: int) -> int:
    """
    Given two integers n and k, return the kth lexicographically smallest integer in the range [1, n].
    
    Example 1:
    
    >>> findKthNumber(n = 13, k = 2)
    >>> 10
    Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
    
    Example 2:
    
    >>> findKthNumber(n = 1, k = 1)
    >>> 1
    """
def arrangeCoins(n: int) -> int:
    """
    You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
    Given the integer n, return the number of complete rows of the staircase you will build.
    
    Example 1:
    
    
    >>> arrangeCoins(n = 5)
    >>> 2
    Explanation: Because the 3rd row is incomplete, we return 2.
    
    Example 2:
    
    
    >>> arrangeCoins(n = 8)
    >>> 3
    Explanation: Because the 4th row is incomplete, we return 3.
    """
def findDuplicates(nums: List[int]) -> List[int]:
    """
    Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appears twice.
    You must write an algorithm that runs in O(n) time and uses only constant auxiliary space, excluding the space needed to store the output
    
    Example 1:
    >>> findDuplicates(nums = [4,3,2,7,8,2,3,1])
    >>> [2,3]
    Example 2:
    >>> findDuplicates(nums = [1,1,2])
    >>> [1]
    Example 3:
    >>> findDuplicates(nums = [1])
    >>> []
    """
def compress(chars: List[str]) -> int:
    """
    Given an array of characters chars, compress it using the following algorithm:
    Begin with an empty string s. For each group of consecutive repeating characters in chars:
    
    If the group's length is 1, append the character to s.
    Otherwise, append the character followed by the group's length.
    
    The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.
    After you are done modifying the input array, return the new length of the array.
    You must write an algorithm that uses only constant extra space.
    
    Example 1:
    
    >>> compress(chars = ["a","a","b","b","c","c","c"])
    >>> Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
    Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
    
    Example 2:
    
    >>> compress(chars = ["a"])
    >>> Return 1, and the first character of the input array should be: ["a"]
    Explanation: The only group is "a", which remains uncompressed since it's a single character.
    
    Example 3:
    
    >>> compress(chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"])
    >>> Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
    Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
    """
def sequenceReconstruction(nums: List[int], sequences: List[List[int]]) -> bool:
    """
    You are given an integer array nums of length n where nums is a permutation of the integers in the range [1, n]. You are also given a 2D integer array sequences where sequences[i] is a subsequence of nums.
    Check if nums is the shortest possible and the only supersequence. The shortest supersequence is a sequence with the shortest length and has all sequences[i] as subsequences. There could be multiple valid supersequences for the given array sequences.
    
    For example, for sequences = [[1,2],[1,3]], there are two shortest supersequences, [1,2,3] and [1,3,2].
    While for sequences = [[1,2],[1,3],[1,2,3]], the only shortest supersequence possible is [1,2,3]. [1,2,3,4] is a possible supersequence but not the shortest.
    
    Return true if nums is the only shortest supersequence for sequences, or false otherwise.
    A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
    
    Example 1:
    
    >>> sequenceReconstruction(nums = [1,2,3], sequences = [[1,2],[1,3]])
    >>> false
    Explanation: There are two possible supersequences: [1,2,3] and [1,3,2].
    The sequence [1,2] is a subsequence of both: [1,2,3] and [1,3,2].
    The sequence [1,3] is a subsequence of both: [1,2,3] and [1,3,2].
    Since nums is not the only shortest supersequence, we return false.
    
    Example 2:
    
    >>> sequenceReconstruction(nums = [1,2,3], sequences = [[1,2]])
    >>> false
    Explanation: The shortest possible supersequence is [1,2].
    The sequence [1,2] is a subsequence of it: [1,2].
    Since nums is not the shortest supersequence, we return false.
    
    Example 3:
    
    >>> sequenceReconstruction(nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]])
    >>> true
    Explanation: The shortest possible supersequence is [1,2,3].
    The sequence [1,2] is a subsequence of it: [1,2,3].
    The sequence [1,3] is a subsequence of it: [1,2,3].
    The sequence [2,3] is a subsequence of it: [1,2,3].
    Since nums is the only shortest supersequence, we return true.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
    You may assume the two numbers do not contain any leading zero, except the number 0 itself.
    
    Example 1:
    
    
    >>> __init__(l1 = [7,2,4,3], l2 = [5,6,4])
    >>> [7,8,0,7]
    
    Example 2:
    
    >>> __init__(l1 = [2,4,3], l2 = [5,6,4])
    >>> [8,0,7]
    
    Example 3:
    
    >>> __init__(l1 = [0], l2 = [0])
    >>> [0]
    """
def numberOfArithmeticSlices(nums: List[int]) -> int:
    """
    Given an integer array nums, return the number of all the arithmetic subsequences of nums.
    A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
    
    For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences.
    For example, [1, 1, 2, 5, 7] is not an arithmetic sequence.
    
    A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
    
    For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].
    
    The test cases are generated so that the answer fits in 32-bit integer.
    
    Example 1:
    
    >>> numberOfArithmeticSlices(nums = [2,4,6,8,10])
    >>> 7
    Explanation: All arithmetic subsequence slices are:
    [2,4,6]
    [4,6,8]
    [6,8,10]
    [2,4,6,8]
    [4,6,8,10]
    [2,4,6,8,10]
    [2,6,10]
    
    Example 2:
    
    >>> numberOfArithmeticSlices(nums = [7,7,7,7,7])
    >>> 16
    Explanation: Any subsequence of this array is arithmetic.
    """
def numberOfBoomerangs(points: List[List[int]]) -> int:
    """
    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
    Return the number of boomerangs.
    
    Example 1:
    
    >>> numberOfBoomerangs(points = [[0,0],[1,0],[2,0]])
    >>> 2
    Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
    
    Example 2:
    
    >>> numberOfBoomerangs(points = [[1,1],[2,2],[3,3]])
    >>> 2
    
    Example 3:
    
    >>> numberOfBoomerangs(points = [[1,1]])
    >>> 0
    """
def findDisappearedNumbers(nums: List[int]) -> List[int]:
    """
    Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
    
    Example 1:
    >>> findDisappearedNumbers(nums = [4,3,2,7,8,2,3,1])
    >>> [5,6]
    Example 2:
    >>> findDisappearedNumbers(nums = [1,1])
    >>> [2]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def deleteNode(root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
    """
    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
    Basically, the deletion can be divided into two stages:
    
    Search for a node to remove.
    If the node is found, delete the node.
    
    
    Example 1:
    
    
    >>> __init__(root = [5,3,6,2,4,null,7], key = 3)
    >>> [5,4,6,2,null,null,7]
    Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
    One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
    Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
    
    
    Example 2:
    
    >>> __init__(root = [5,3,6,2,4,null,7], key = 0)
    >>> [5,3,6,2,4,null,7]
    Explanation: The tree does not contain a node with value = 0.
    
    Example 3:
    
    >>> __init__(root = [], key = 0)
    >>> []
    """
def frequencySort(s: str) -> str:
    """
    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
    Return the sorted string. If there are multiple answers, return any of them.
    
    Example 1:
    
    >>> frequencySort(s = "tree")
    >>> "eert"
    Explanation: 'e' appears twice while 'r' and 't' both appear once.
    So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
    
    Example 2:
    
    >>> frequencySort(s = "cccaaa")
    >>> "aaaccc"
    Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
    Note that "cacaca" is incorrect, as the same characters must be together.
    
    Example 3:
    
    >>> frequencySort(s = "Aabb")
    >>> "bbAa"
    Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
    Note that 'A' and 'a' are treated as two different characters.
    """
def findMinArrowShots(points: List[List[int]]) -> int:
    """
    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.
    Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
    Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
    
    Example 1:
    
    >>> findMinArrowShots(points = [[10,16],[2,8],[1,6],[7,12]])
    >>> 2
    Explanation: The balloons can be burst by 2 arrows:
    - Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
    - Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
    
    Example 2:
    
    >>> findMinArrowShots(points = [[1,2],[3,4],[5,6],[7,8]])
    >>> 4
    Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
    
    Example 3:
    
    >>> findMinArrowShots(points = [[1,2],[2,3],[3,4],[4,5]])
    >>> 2
    Explanation: The balloons can be burst by 2 arrows:
    - Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
    - Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
    """
def minMoves(nums: List[int]) -> int:
    """
    Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
    In one move, you can increment n - 1 elements of the array by 1.
    
    Example 1:
    
    >>> minMoves(nums = [1,2,3])
    >>> 3
    Explanation: Only three moves are needed (remember each move increments two elements):
    [1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]
    
    Example 2:
    
    >>> minMoves(nums = [1,1,1])
    >>> 0
    """
def fourSumCount(nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
    """
    Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:
    
    0 <= i, j, k, l < n
    nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
    
    
    Example 1:
    
    >>> fourSumCount(nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2])
    >>> 2
    Explanation:
    The two tuples are:
    1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
    2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
    
    Example 2:
    
    >>> fourSumCount(nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0])
    >>> 1
    """
def findContentChildren(g: List[int], s: List[int]) -> int:
    """
    Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
    Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
    
    Example 1:
    
    >>> findContentChildren(g = [1,2,3], s = [1,1])
    >>> 1
    Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
    And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
    You need to output 1.
    
    Example 2:
    
    >>> findContentChildren(g = [1,2], s = [1,2,3])
    >>> 2
    Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
    You have 3 cookies and their sizes are big enough to gratify all of the children,
    You need to output 2.
    """
def find132pattern(nums: List[int]) -> bool:
    """
    Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].
    Return true if there is a 132 pattern in nums, otherwise, return false.
    
    Example 1:
    
    >>> find132pattern(nums = [1,2,3,4])
    >>> false
    Explanation: There is no 132 pattern in the sequence.
    
    Example 2:
    
    >>> find132pattern(nums = [3,1,4,2])
    >>> true
    Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
    
    Example 3:
    
    >>> find132pattern(nums = [-1,3,2,0])
    >>> true
    Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
    """