ShwStone/humaneval-leetcode huggingface dataset:
def uniqueLetterString(s: str) -> int:
    """
    Let's define a function countUniqueChars(s) that returns the number of unique characters in s.
    
    For example, calling countUniqueChars(s) if s = "LEETCODE" then "L", "T", "C", "O", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
    
    Given a string s, return the sum of countUniqueChars(t) where t is a substring of s. The test cases are generated such that the answer fits in a 32-bit integer.
    Notice that some substrings can be repeated so in this case you have to count the repeated ones too.
    
    Example 1:
    
    >>> uniqueLetterString(s = "ABC")
    >>> 10
    Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
    Every substring is composed with only unique letters.
    Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
    
    Example 2:
    
    >>> uniqueLetterString(s = "ABA")
    >>> 8
    Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
    
    Example 3:
    
    >>> uniqueLetterString(s = "LEETCODE")
    >>> 92
    """
def consecutiveNumbersSum(n: int) -> int:
    """
    Given an integer n, return the number of ways you can write n as the sum of consecutive positive integers.
    
    Example 1:
    
    >>> consecutiveNumbersSum(n = 5)
    >>> 2
    Explanation: 5 = 2 + 3
    
    Example 2:
    
    >>> consecutiveNumbersSum(n = 9)
    >>> 3
    Explanation: 9 = 4 + 5 = 2 + 3 + 4
    
    Example 3:
    
    >>> consecutiveNumbersSum(n = 15)
    >>> 4
    Explanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
    """
def largeGroupPositions(s: str) -> List[List[int]]:
    """
    In a string s of lowercase letters, these letters form consecutive groups of the same character.
    For example, a string like s = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z", and "yy".
    A group is identified by an interval [start, end], where start and end denote the start and end indices (inclusive) of the group. In the above example, "xxxx" has the interval [3,6].
    A group is considered large if it has 3 or more characters.
    Return the intervals of every large group sorted in increasing order by start index.
    
    Example 1:
    
    >>> largeGroupPositions(s = "abbxxxxzzy")
    >>> [[3,6]]
    Explanation: "xxxx" is the only large group with start index 3 and end index 6.
    
    Example 2:
    
    >>> largeGroupPositions(s = "abc")
    >>> []
    Explanation: We have groups "a", "b", and "c", none of which are large groups.
    
    Example 3:
    
    >>> largeGroupPositions(s = "abcdddeeeeaabbbcd")
    >>> [[3,5],[6,9],[12,14]]
    Explanation: The large groups are "ddd", "eeee", and "bbb".
    """
def maskPII(s: str) -> str:
    """
    You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules.
    Email address:
    An email address is:
    
    A name consisting of uppercase and lowercase English letters, followed by
    The '@' symbol, followed by
    The domain consisting of uppercase and lowercase English letters with a dot '.' somewhere in the middle (not the first or last character).
    
    To mask an email:
    
    The uppercase letters in the name and domain must be converted to lowercase letters.
    The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks "*****".
    
    Phone number:
    A phone number is formatted as follows:
    
    The phone number contains 10-13 digits.
    The last 10 digits make up the local number.
    The remaining 0-3 digits, in the beginning, make up the country code.
    Separation characters from the set {'+', '-', '(', ')', ' '} separate the above digits in some way.
    
    To mask a phone number:
    
    Remove all separation characters.
    The masked phone number should have the form:
    
    "***-***-XXXX" if the country code has 0 digits.
    "+*-***-***-XXXX" if the country code has 1 digit.
    "+**-***-***-XXXX" if the country code has 2 digits.
    "+***-***-***-XXXX" if the country code has 3 digits.
    
    
    "XXXX" is the last 4 digits of the local number.
    
    
    Example 1:
    
    >>> maskPII(s = "LeetCode@LeetCode.com")
    >>> "l*****e@leetcode.com"
    Explanation: s is an email address.
    The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
    
    Example 2:
    
    >>> maskPII(s = "AB@qq.com")
    >>> "a*****b@qq.com"
    Explanation: s is an email address.
    The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
    Note that even though "ab" is 2 characters, it still must have 5 asterisks in the middle.
    
    Example 3:
    
    >>> maskPII(s = "1(234)567-890")
    >>> "***-***-7890"
    Explanation: s is a phone number.
    There are 10 digits, so the local number is 10 digits and the country code is 0 digits.
    Thus, the resulting masked number is "***-***-7890".
    """
def flipAndInvertImage(image: List[List[int]]) -> List[List[int]]:
    """
    Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.
    To flip an image horizontally means that each row of the image is reversed.
    
    For example, flipping [1,1,0] horizontally results in [0,1,1].
    
    To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.
    
    For example, inverting [0,1,1] results in [1,0,0].
    
    
    Example 1:
    
    >>> flipAndInvertImage(image = [[1,1,0],[1,0,1],[0,0,0]])
    >>> [[1,0,0],[0,1,0],[1,1,1]]
    Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
    Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
    
    Example 2:
    
    >>> flipAndInvertImage(image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]])
    >>> [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
    Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
    Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
    """
def findReplaceString(s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
    """
    You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
    To complete the ith replacement operation:
    
    Check if the substring sources[i] occurs at index indices[i] in the original string s.
    If it does not occur, do nothing.
    Otherwise if it does occur, replace that substring with targets[i].
    
    For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "eeecd".
    All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
    
    For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.
    
    Return the resulting string after performing all replacement operations on s.
    A substring is a contiguous sequence of characters in a string.
    
    Example 1:
    
    
    >>> findReplaceString(s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"])
    >>> "eeebffff"
    Explanation:
    "a" occurs at index 0 in s, so we replace it with "eee".
    "cd" occurs at index 2 in s, so we replace it with "ffff".
    
    Example 2:
    
    
    >>> findReplaceString(s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"])
    >>> "eeecd"
    Explanation:
    "ab" occurs at index 0 in s, so we replace it with "eee".
    "ec" does not occur at index 2 in s, so we do nothing.
    """
def sumOfDistancesInTree(n: int, edges: List[List[int]]) -> List[int]:
    """
    There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
    You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
    Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree and all other nodes.
    
    Example 1:
    
    
    >>> sumOfDistancesInTree(n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]])
    >>> [8,12,6,10,10,10]
    Explanation: The tree is shown above.
    We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
    equals 1 + 1 + 2 + 2 + 2 = 8.
    Hence, answer[0] = 8, and so on.
    
    Example 2:
    
    
    >>> sumOfDistancesInTree(n = 1, edges = [])
    >>> [0]
    
    Example 3:
    
    
    >>> sumOfDistancesInTree(n = 2, edges = [[1,0]])
    >>> [1,1]
    """
def largestOverlap(img1: List[List[int]], img2: List[List[int]]) -> int:
    """
    You are given two images, img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values.
    We translate one image however we choose by sliding all the 1 bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images.
    Note also that a translation does not include any kind of rotation. Any 1 bits that are translated outside of the matrix borders are erased.
    Return the largest possible overlap.
    
    Example 1:
    
    
    >>> largestOverlap(img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]])
    >>> 3
    Explanation: We translate img1 to right by 1 unit and down by 1 unit.
    
    The number of positions that have a 1 in both images is 3 (shown in red).
    
    
    Example 2:
    
    >>> largestOverlap(img1 = [[1]], img2 = [[1]])
    >>> 1
    
    Example 3:
    
    >>> largestOverlap(img1 = [[0]], img2 = [[0]])
    >>> 0
    """
def isRectangleOverlap(rec1: List[int], rec2: List[int]) -> bool:
    """
    An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.
    Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.
    Given two axis-aligned rectangles rec1 and rec2, return true if they overlap, otherwise return false.
    
    Example 1:
    >>> isRectangleOverlap(rec1 = [0,0,2,2], rec2 = [1,1,3,3])
    >>> true
    Example 2:
    >>> isRectangleOverlap(rec1 = [0,0,1,1], rec2 = [1,0,2,1])
    >>> false
    Example 3:
    >>> isRectangleOverlap(rec1 = [0,0,1,1], rec2 = [2,2,3,3])
    >>> false
    """
def new21Game(n: int, k: int, maxPts: int) -> float:
    """
    Alice plays the following game, loosely based on the card game "21".
    Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where maxPts is an integer. Each draw is independent and the outcomes have equal probabilities.
    Alice stops drawing numbers when she gets k or more points.
    Return the probability that Alice has n or fewer points.
    Answers within 10-5 of the actual answer are considered accepted.
    
    Example 1:
    
    >>> new21Game(n = 10, k = 1, maxPts = 10)
    >>> 1.00000
    Explanation: Alice gets a single card, then stops.
    
    Example 2:
    
    >>> new21Game(n = 6, k = 1, maxPts = 10)
    >>> 0.60000
    Explanation: Alice gets a single card, then stops.
    In 6 out of 10 possibilities, she is at or below 6 points.
    
    Example 3:
    
    >>> new21Game(n = 21, k = 17, maxPts = 10)
    >>> 0.73278
    """
def pushDominoes(dominoes: str) -> str:
    """
    There are n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
    After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
    When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
    For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
    You are given a string dominoes representing the initial state where:
    
    dominoes[i] = 'L', if the ith domino has been pushed to the left,
    dominoes[i] = 'R', if the ith domino has been pushed to the right, and
    dominoes[i] = '.', if the ith domino has not been pushed.
    
    Return a string representing the final state.
    
    Example 1:
    
    >>> pushDominoes(dominoes = "RR.L")
    >>> "RR.L"
    Explanation: The first domino expends no additional force on the second domino.
    
    Example 2:
    
    
    >>> pushDominoes(dominoes = ".L.R...LR..L..")
    >>> "LL.RR.LLRRLL.."
    """
def numSimilarGroups(strs: List[str]) -> int:
    """
    Two strings, X and Y, are considered similar if either they are identical or we can make them equivalent by swapping at most two letters (in distinct positions) within the string X.
    For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".
    Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}.  Notice that "tars" and "arts" are in the same group even though they are not similar.  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
    We are given a list strs of strings where every string in strs is an anagram of every other string in strs. How many groups are there?
    
    Example 1:
    
    >>> numSimilarGroups(strs = ["tars","rats","arts","star"])
    >>> 2
    
    Example 2:
    
    >>> numSimilarGroups(strs = ["omv","ovm"])
    >>> 1
    """
def numMagicSquaresInside(grid: List[List[int]]) -> int:
    """
    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.
    Given a row x col grid of integers, how many 3 x 3 magic square subgrids are there?
    Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15.
    
    Example 1:
    
    
    >>> numMagicSquaresInside(grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]])
    >>> 1
    Explanation:
    The following subgrid is a 3 x 3 magic square:
    
    while this one is not:
    
    In total, there is only one magic square inside the given grid.
    
    Example 2:
    
    >>> numMagicSquaresInside(grid = [[8]])
    >>> 0
    """
def canVisitAllRooms(rooms: List[List[int]]) -> bool:
    """
    There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.
    When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
    Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.
    
    Example 1:
    
    >>> canVisitAllRooms(rooms = [[1],[2],[3],[]])
    >>> true
    Explanation:
    We visit room 0 and pick up key 1.
    We then visit room 1 and pick up key 2.
    We then visit room 2 and pick up key 3.
    We then visit room 3.
    Since we were able to visit every room, we return true.
    
    Example 2:
    
    >>> canVisitAllRooms(rooms = [[1,3],[3,0,1],[2],[0]])
    >>> false
    Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.
    """
def splitIntoFibonacci(num: str) -> List[int]:
    """
    You are given a string of digits num, such as "123456579". We can split it into a Fibonacci-like sequence [123, 456, 579].
    Formally, a Fibonacci-like sequence is a list f of non-negative integers such that:
    
    0 <= f[i] < 231, (that is, each integer fits in a 32-bit signed integer type),
    f.length >= 3, and
    f[i] + f[i + 1] == f[i + 2] for all 0 <= i < f.length - 2.
    
    Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself.
    Return any Fibonacci-like sequence split from num, or return [] if it cannot be done.
    
    Example 1:
    
    >>> splitIntoFibonacci(num = "1101111")
    >>> [11,0,11,11]
    Explanation: The output [110, 1, 111] would also be accepted.
    
    Example 2:
    
    >>> splitIntoFibonacci(num = "112358130")
    >>> []
    Explanation: The task is impossible.
    
    Example 3:
    
    >>> splitIntoFibonacci(num = "0123")
    >>> []
    Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.
    """
def backspaceCompare(s: str, t: str) -> bool:
    """
    Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
    Note that after backspacing an empty text, the text will continue empty.
    
    Example 1:
    
    >>> backspaceCompare(s = "ab#c", t = "ad#c")
    >>> true
    Explanation: Both s and t become "ac".
    
    Example 2:
    
    >>> backspaceCompare(s = "ab##", t = "c#d#")
    >>> true
    Explanation: Both s and t become "".
    
    Example 3:
    
    >>> backspaceCompare(s = "a#c", t = "b")
    >>> false
    Explanation: s becomes "c" while t becomes "b".
    """
def longestMountain(arr: List[int]) -> int:
    """
    You may recall that an array arr is a mountain array if and only if:
    
    arr.length >= 3
    There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
    
    arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    
    
    
    Given an integer array arr, return the length of the longest subarray, which is a mountain. Return 0 if there is no mountain subarray.
    
    Example 1:
    
    >>> longestMountain(arr = [2,1,4,7,3,2,5])
    >>> 5
    Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
    
    Example 2:
    
    >>> longestMountain(arr = [2,2,2])
    >>> 0
    Explanation: There is no mountain.
    """
def isNStraightHand(hand: List[int], groupSize: int) -> bool:
    """
    Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
    Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
    
    Example 1:
    
    >>> isNStraightHand(hand = [1,2,3,6,2,3,4,7,8], groupSize = 3)
    >>> true
    Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
    
    Example 2:
    
    >>> isNStraightHand(hand = [1,2,3,4,5], groupSize = 4)
    >>> false
    Explanation: Alice's hand can not be rearranged into groups of 4.
    """
def shortestPathLength(graph: List[List[int]]) -> int:
    """
    You have an undirected, connected graph of n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge.
    Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
    
    Example 1:
    
    
    >>> shortestPathLength(graph = [[1,2,3],[0],[0],[0]])
    >>> 4
    Explanation: One possible path is [1,0,2,0,3]
    
    Example 2:
    
    
    >>> shortestPathLength(graph = [[1],[0,2,4],[1,3,4],[2],[1,2]])
    >>> 4
    Explanation: One possible path is [0,1,4,2,3]
    """
def shiftingLetters(s: str, shifts: List[int]) -> str:
    """
    You are given a string s of lowercase English letters and an integer array shifts of the same length.
    Call the shift() of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').
    
    For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.
    
    Now for each shifts[i] = x, we want to shift the first i + 1 letters of s, x times.
    Return the final string after all such shifts to s are applied.
    
    Example 1:
    
    >>> shiftingLetters(s = "abc", shifts = [3,5,9])
    >>> "rpl"
    Explanation: We start with "abc".
    After shifting the first 1 letters of s by 3, we have "dbc".
    After shifting the first 2 letters of s by 5, we have "igc".
    After shifting the first 3 letters of s by 9, we have "rpl", the answer.
    
    Example 2:
    
    >>> shiftingLetters(s = "aaa", shifts = [1,2,3])
    >>> "gfd"
    """
def maxDistToClosest(seats: List[int]) -> int:
    """
    You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed).
    There is at least one empty seat, and at least one person sitting.
    Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
    Return that maximum distance to the closest person.
    
    Example 1:
    
    
    >>> maxDistToClosest(seats = [1,0,0,0,1,0,1])
    >>> 2
    Explanation:
    If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
    If Alex sits in any other open seat, the closest person has distance 1.
    Thus, the maximum distance to the closest person is 2.
    
    Example 2:
    
    >>> maxDistToClosest(seats = [1,0,0,0])
    >>> 3
    Explanation:
    If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
    This is the maximum distance possible, so the answer is 3.
    
    Example 3:
    
    >>> maxDistToClosest(seats = [0,1])
    >>> 1
    """
def rectangleArea(rectangles: List[List[int]]) -> int:
    """
    You are given a 2D array of axis-aligned rectangles. Each rectangle[i] = [xi1, yi1, xi2, yi2] denotes the ith rectangle where (xi1, yi1) are the coordinates of the bottom-left corner, and (xi2, yi2) are the coordinates of the top-right corner.
    Calculate the total area covered by all rectangles in the plane. Any area covered by two or more rectangles should only be counted once.
    Return the total area. Since the answer may be too large, return it modulo 109 + 7.
    
    Example 1:
    
    
    >>> rectangleArea(rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]])
    >>> 6
    Explanation: A total area of 6 is covered by all three rectangles, as illustrated in the picture.
    From (1,1) to (2,2), the green and red rectangles overlap.
    From (1,0) to (2,3), all three rectangles overlap.
    
    Example 2:
    
    >>> rectangleArea(rectangles = [[0,0,1000000000,1000000000]])
    >>> 49
    Explanation: The answer is 1018 modulo (109 + 7), which is 49.
    """