ShwStone/humaneval-leetcode huggingface dataset:
def isScramble(s1: str, s2: str) -> bool:
    """
    We can scramble a string s to get a string t using the following algorithm:
    
    If the length of the string is 1, stop.
    If the length of the string is > 1, do the following:
    
    Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
    Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
    Apply step 1 recursively on each of the two substrings x and y.
    
    
    
    Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.
    
    Example 1:
    
    >>> isScramble(s1 = "great", s2 = "rgeat")
    >>> true
    Explanation: One possible scenario applied on s1 is:
    "great" --> "gr/eat" // divide at random index.
    "gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order.
    "gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at random index each of them.
    "g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order.
    "r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t".
    "r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order.
    The algorithm stops now, and the result string is "rgeat" which is s2.
    As one possible scenario led s1 to be scrambled to s2, we return true.
    
    Example 2:
    
    >>> isScramble(s1 = "abcde", s2 = "caebd")
    >>> false
    
    Example 3:
    
    >>> isScramble(s1 = "a", s2 = "a")
    >>> true
    """
def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
    """
    You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
    Merge nums1 and nums2 into a single array sorted in non-decreasing order.
    The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
    
    Example 1:
    
    >>> merge(nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3)
    >>> [1,2,2,3,5,6]
    Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
    The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
    
    Example 2:
    
    >>> merge(nums1 = [1], m = 1, nums2 = [], n = 0)
    >>> [1]
    Explanation: The arrays we are merging are [1] and [].
    The result of the merge is [1].
    
    Example 3:
    
    >>> merge(nums1 = [0], m = 0, nums2 = [1], n = 1)
    >>> [1]
    Explanation: The arrays we are merging are [] and [1].
    The result of the merge is [1].
    Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
    """
def grayCode(n: int) -> List[int]:
    """
    An n-bit gray code sequence is a sequence of 2n integers where:
    
    Every integer is in the inclusive range [0, 2n - 1],
    The first integer is 0,
    An integer appears no more than once in the sequence,
    The binary representation of every pair of adjacent integers differs by exactly one bit, and
    The binary representation of the first and last integers differs by exactly one bit.
    
    Given an integer n, return any valid n-bit gray code sequence.
    
    Example 1:
    
    >>> grayCode(n = 2)
    >>> [0,1,3,2]
    Explanation:
    The binary representation of [0,1,3,2] is [00,01,11,10].
    - 00 and 01 differ by one bit
    - 01 and 11 differ by one bit
    - 11 and 10 differ by one bit
    - 10 and 00 differ by one bit
    [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
    - 00 and 10 differ by one bit
    - 10 and 11 differ by one bit
    - 11 and 01 differ by one bit
    - 01 and 00 differ by one bit
    
    Example 2:
    
    >>> grayCode(n = 1)
    >>> [0,1]
    """
def subsetsWithDup(nums: List[int]) -> List[List[int]]:
    """
    Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
    The solution set must not contain duplicate subsets. Return the solution in any order.
    
    Example 1:
    >>> subsetsWithDup(nums = [1,2,2])
    >>> [[],[1],[1,2],[1,2,2],[2],[2,2]]
    Example 2:
    >>> subsetsWithDup(nums = [0])
    >>> [[],[0]]
    """
def numDecodings(s: str) -> int:
    """
    You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:
    "1" -> 'A'
    "2" -> 'B'
    ...
    "25" -> 'Y'
    "26" -> 'Z'
    However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25").
    For example, "11106" can be decoded into:
    
    "AAJF" with the grouping (1, 1, 10, 6)
    "KJF" with the grouping (11, 10, 6)
    The grouping (1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid).
    
    Note: there may be strings that are impossible to decode.
    
    Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0.
    The test cases are generated so that the answer fits in a 32-bit integer.
    
    Example 1:
    
    >>> numDecodings(s = "12")
    >>> 2
    Explanation:
    "12" could be decoded as "AB" (1 2) or "L" (12).
    
    Example 2:
    
    >>> numDecodings(s = "226")
    >>> 3
    Explanation:
    "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
    
    Example 3:
    
    >>> numDecodings(s = "06")
    >>> 0
    Explanation:
    "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). In this case, the string is not a valid encoding, so return 0.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
    """
    Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5], left = 2, right = 4)
    >>> [1,4,3,2,5]
    
    Example 2:
    
    >>> __init__(head = [5], left = 1, right = 1)
    >>> [5]
    """
def restoreIpAddresses(s: str) -> List[str]:
    """
    A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
    
    For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
    
    Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.
    
    Example 1:
    
    >>> restoreIpAddresses(s = "25525511135")
    >>> ["255.255.11.135","255.255.111.35"]
    
    Example 2:
    
    >>> restoreIpAddresses(s = "0000")
    >>> ["0.0.0.0"]
    
    Example 3:
    
    >>> restoreIpAddresses(s = "101023")
    >>> ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(root: Optional[TreeNode]) -> List[int]:
    """
    Given the root of a binary tree, return the inorder traversal of its nodes' values.
    
    Example 1:
    
    >>> __init__(root = [1,null,2,3])
    >>> [1,3,2]
    Explanation:
    
    
    Example 2:
    
    >>> __init__(root = [1,2,3,4,5,null,8,null,null,6,7,9])
    >>> [4,2,6,5,7,1,3,9,8]
    Explanation:
    
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    
    Example 4:
    
    >>> __init__(root = [1])
    >>> [1]
    """
def numTrees(n: int) -> int:
    """
    Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.
    
    Example 1:
    
    
    >>> numTrees(n = 3)
    >>> 5
    
    Example 2:
    
    >>> numTrees(n = 1)
    >>> 1
    """
def isInterleave(s1: str, s2: str, s3: str) -> bool:
    """
    Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
    An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that:
    
    s = s1 + s2 + ... + sn
    t = t1 + t2 + ... + tm
    |n - m| <= 1
    The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
    
    Note: a + b is the concatenation of strings a and b.
    
    Example 1:
    
    
    >>> isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac")
    >>> true
    Explanation: One way to obtain s3 is:
    Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
    Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
    Since s3 can be obtained by interleaving s1 and s2, we return true.
    
    Example 2:
    
    >>> isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc")
    >>> false
    Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
    
    Example 3:
    
    >>> isInterleave(s1 = "", s2 = "", s3 = "")
    >>> true
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidBST(root: Optional[TreeNode]) -> bool:
    """
    Given the root of a binary tree, determine if it is a valid binary search tree (BST).
    A valid BST is defined as follows:
    
    The left subtree of a node contains only nodes with keys less than the node's key.
    The right subtree of a node contains only nodes with keys greater than the node's key.
    Both the left and right subtrees must also be binary search trees.
    
    
    Example 1:
    
    
    >>> __init__(root = [2,1,3])
    >>> true
    
    Example 2:
    
    
    >>> __init__(root = [5,1,4,null,null,3,6])
    >>> false
    Explanation: The root node's value is 5 but its right child's value is 4.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverTree(root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
    """
    You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.
    
    Example 1:
    
    
    >>> __init__(root = [1,3,null,null,2])
    >>> [3,1,null,null,2]
    Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid.
    
    Example 2:
    
    
    >>> __init__(root = [3,1,4,null,null,2])
    >>> [2,1,4,null,null,3]
    Explanation: 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST valid.
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
    """
    Given the roots of two binary trees p and q, write a function to check if they are the same or not.
    Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
    
    Example 1:
    
    
    >>> __init__(p = [1,2,3], q = [1,2,3])
    >>> true
    
    Example 2:
    
    
    >>> __init__(p = [1,2], q = [1,null,2])
    >>> false
    
    Example 3:
    
    
    >>> __init__(p = [1,2,1], q = [1,1,2])
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(root: Optional[TreeNode]) -> bool:
    """
    Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
    
    Example 1:
    
    
    >>> __init__(root = [1,2,2,3,4,4,3])
    >>> true
    
    Example 2:
    
    
    >>> __init__(root = [1,2,2,null,3,null,3])
    >>> false
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [[3],[9,20],[15,7]]
    
    Example 2:
    
    >>> __init__(root = [1])
    >>> [[1]]
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def zigzagLevelOrder(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [[3],[20,9],[15,7]]
    
    Example 2:
    
    >>> __init__(root = [1])
    >>> [[1]]
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(root: Optional[TreeNode]) -> int:
    """
    Given the root of a binary tree, return its maximum depth.
    A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> 3
    
    Example 2:
    
    >>> __init__(root = [1,null,2])
    >>> 2
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
    """
    Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
    
    Example 1:
    
    
    >>> __init__(preorder = [3,9,20,15,7], inorder = [9,3,15,20,7])
    >>> [3,9,20,null,null,15,7]
    
    Example 2:
    
    >>> __init__(preorder = [-1], inorder = [-1])
    >>> [-1]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
    """
    Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
    
    Example 1:
    
    
    >>> __init__(inorder = [9,3,15,20,7], postorder = [9,15,7,20,3])
    >>> [3,9,20,null,null,15,7]
    
    Example 2:
    
    >>> __init__(inorder = [-1], postorder = [-1])
    >>> [-1]
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrderBottom(root: Optional[TreeNode]) -> List[List[int]]:
    """
    Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> [[15,7],[9,20],[3]]
    
    Example 2:
    
    >>> __init__(root = [1])
    >>> [[1]]
    
    Example 3:
    
    >>> __init__(root = [])
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sortedArrayToBST(nums: List[int]) -> Optional[TreeNode]:
    """
    Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
    
    Example 1:
    
    
    >>> __init__(nums = [-10,-3,0,5,9])
    >>> [0,-3,9,-10,null,5]
    Explanation: [0,-10,5,null,-3,null,9] is also accepted:
    
    
    Example 2:
    
    
    >>> __init__(nums = [1,3])
    >>> [3,1]
    Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sortedListToBST(head: Optional[ListNode]) -> Optional[TreeNode]:
    """
    Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
    
    Example 1:
    
    
    >>> __init__(head = [-10,-3,0,5,9])
    >>> [0,-3,9,-10,null,5]
    Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
    
    Example 2:
    
    >>> __init__(head = [])
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(root: Optional[TreeNode]) -> bool:
    """
    Given a binary tree, determine if it is height-balanced.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> true
    
    Example 2:
    
    
    >>> __init__(root = [1,2,2,3,3,null,null,4,4])
    >>> false
    
    Example 3:
    
    >>> __init__(root = [])
    >>> true
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDepth(root: Optional[TreeNode]) -> int:
    """
    Given a binary tree, find its minimum depth.
    The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
    Note: A leaf is a node with no children.
    
    Example 1:
    
    
    >>> __init__(root = [3,9,20,null,null,15,7])
    >>> 2
    
    Example 2:
    
    >>> __init__(root = [2,null,3,null,4,null,5,null,6])
    >>> 5
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(root: Optional[TreeNode], targetSum: int) -> bool:
    """
    Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
    A leaf is a node with no children.
    
    Example 1:
    
    
    >>> __init__(root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22)
    >>> true
    Explanation: The root-to-leaf path with the target sum is shown.
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3], targetSum = 5)
    >>> false
    Explanation: There are two root-to-leaf paths in the tree:
    (1 --> 2): The sum is 3.
    (1 --> 3): The sum is 4.
    There is no root-to-leaf path with sum = 5.
    
    Example 3:
    
    >>> __init__(root = [], targetSum = 0)
    >>> false
    Explanation: Since the tree is empty, there are no root-to-leaf paths.
    """
# 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) -> List[List[int]]:
    """
    Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.
    A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.
    
    Example 1:
    
    
    >>> __init__(root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22)
    >>> [[5,4,11,2],[5,8,4,5]]
    Explanation: There are two paths whose sum equals targetSum:
    5 + 4 + 11 + 2 = 22
    5 + 8 + 4 + 5 = 22
    
    Example 2:
    
    
    >>> __init__(root = [1,2,3], targetSum = 5)
    >>> []
    
    Example 3:
    
    >>> __init__(root = [1,2], targetSum = 0)
    >>> []
    """
# class TreeNode:
#     def __init__(val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flatten(root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
    """
    Given the root of a binary tree, flatten the tree into a "linked list":
    
    The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
    The "linked list" should be in the same order as a pre-order traversal of the binary tree.
    
    
    Example 1:
    
    
    >>> __init__(root = [1,2,5,3,4,null,6])
    >>> [1,null,2,null,3,null,4,null,5,null,6]
    
    Example 2:
    
    >>> __init__(root = [])
    >>> []
    
    Example 3:
    
    >>> __init__(root = [0])
    >>> [0]
    """
def numDistinct(s: str, t: str) -> int:
    """
    Given two strings s and t, return the number of distinct subsequences of s which equals t.
    The test cases are generated so that the answer fits on a 32-bit signed integer.
    
    Example 1:
    
    >>> numDistinct(s = "rabbbit", t = "rabbit")
    >>> 3
    Explanation:
    As shown below, there are 3 ways you can generate "rabbit" from s.
    rabbbit
    rabbbit
    rabbbit
    
    Example 2:
    
    >>> numDistinct(s = "babgbag", t = "bag")
    >>> 5
    Explanation:
    As shown below, there are 5 ways you can generate "bag" from s.
    babgbag
    babgbag
    babgbag
    babgbag
    babgbag
    """
def generate(numRows: int) -> List[List[int]]:
    """
    Given an integer numRows, return the first numRows of Pascal's triangle.
    In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
    
    
    Example 1:
    >>> generate(numRows = 5)
    >>> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
    Example 2:
    >>> generate(numRows = 1)
    >>> [[1]]
    """
def getRow(rowIndex: int) -> List[int]:
    """
    Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
    In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
    
    
    Example 1:
    >>> getRow(rowIndex = 3)
    >>> [1,3,3,1]
    Example 2:
    >>> getRow(rowIndex = 0)
    >>> [1]
    Example 3:
    >>> getRow(rowIndex = 1)
    >>> [1,1]
    """