Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, A, B): n, m = len(A), len(B) dp = [[0] * (m) for i in range(n)] for i in range(n): for j in range(m): dp[i][j] = A[i] * B[j] if i > 0 and j > 0: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
from functools import lru_cache class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: l1, l2 = len(nums1), len(nums2) if all([x < 0 for x in nums1]) and all([x > 0 for x in nums2]): return max(nums1) * min(nums2) if all([x > 0 for x in nums1]) and all([x < 0 for x in nums2]): return min(nums1) * max(nums2) @lru_cache(None) def dfs(i1, i2): if i1 == l1 or i2 == l2: return 0 return max(dfs(i1 + 1, i2), dfs(i1, i2 + 1), nums1[i1] * nums2[i2] + dfs(i1 + 1, i2 + 1)) return dfs(0, 0)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: @lru_cache(None) def dp(i,j): if i<0 or j<0: return -float('inf') return max(nums1[i]*nums2[j], nums1[i]*nums2[j]+dp(i-1,j-1), dp(i, j-1), dp(i-1, j)) return dp(len(nums1)-1, len(nums2)-1)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [[0 for _ in range(len(nums2))] for x in range(len(nums1))] for i in range(len(nums1)): for j in range(len(nums2)): dp[i][j] = nums1[i]*nums2[j] if i and j: dp[i][j] += max(dp[i-1][j-1], 0) if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
def max_dot_product (A, B): mem = {} for i in range (len (A)): for j in range (len (B)): if i == j == 0: ans = A[i] * B[j] elif i == 0: ans = max (A[i] * B[j], mem[i, j-1]) elif j == 0: ans = max (A[i] * B[j], mem[i-1, j]) else: ans = max (A[i] * B[j], A[i] * B[j] + mem[i-1, j-1], mem[i, j-1], mem[i-1, j]) mem[i, j] = ans return mem[len (A) - 1, len (B) - 1] class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: return max_dot_product(nums1, nums2)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n,m = len(nums1),len(nums2) if (max(nums1) < 0 and min(nums2) > 0) or (max(nums2) < 0 and min(nums1) > 0): return max(max(nums1)*min(nums2),max(nums2)*min(nums1)) import functools @functools.lru_cache(None) def DP(i,j): if i == n or j == m: return 0 if nums1[i] * nums2[j] > 0: return max(nums1[i] * nums2[j]+DP(i+1,j+1),DP(i,j+1),DP(i+1,j)) else: return max(DP(i+1,j+1),DP(i,j+1),DP(i+1,j)) return DP(0,0)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, a: List[int], b: List[int]) -> int: nums1 = [0] + a nums2 = [0] + b m, n = len(nums1), len(nums2) dp = [[float('-inf')] * m for _ in range(n)] for i in range(1, n): for j in range(1, m): # if nums1[j] * nums2[i] > 0: dp[i][j] = max(dp[i-1][j-1] + nums1[j] * nums2[i], dp[i][j-1], dp[i-1][j], nums1[j] * nums2[i]) # else: # dp[i][j] = max(dp[i][j-1], dp[i-1][j], nums1[j] * nums2[i]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, A, B): n, m = len(A), len(B) dp = [[0] * (m) for i in range(n)] for i in range(n): for j in range(m): dp[i][j] = A[i] * B[j] if i and j: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, A: List[int], B: List[int]) -> int: n, m = len(A), len(B) dp = [[0] * (m) for i in range(n)] for i in range(n): for j in range(m): dp[i][j] = A[i] * B[j] if i and j: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # TC: O(MN), SC: O(MN), refr. longest common sequence, Q1035 m, n = len(nums1), len(nums2) dp = [[0] * n for _ in range(m)] for i in range(m): for j in range(n): dp[i][j] = nums1[i] * nums2[j] if i and j: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[m - 1][n - 1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [] for i in range(len(nums1)): dp.append([]) for j in range(len(nums2)): dp[i].append(0) for i in range(len(nums1)): for j in range(len(nums2)): dp[i][j] = nums1[i]*nums2[j] if i and j: dp[i][j] += max(dp[i-1][j-1], 0) if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [[0] * len(nums2) for _ in range(len(nums1))] for i in range(len(nums1)): for j in range(len(nums2)): dp[i][j] = nums1[i] * nums2[j] if i and j: dp[i][j] += max(0, dp[i-1][j-1]) if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # Target: We want to calculate the maximal dot product for nums1[0:i] and nums2[0:j] # Base case # When i == 0 or j == 0, we return -inf. (Because this is an empty case, which is intolerable) # State Transition, for any i > 0 and j > 0, there are 4 possibilities # nums1[i - 1] is not selected, dp[i][j] = dp[i - 1][j] # nums2[j - 1] is not selected, dp[i][j] = dp[i][j - 1] # Neither nums1[i - 1] or nums2[j - 1] is selected, dp[i][j] = dp[i - 1][j - 1] # Both nums1[i - 1] and nums2[j - 1] are selected, dp[i][j] = max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1] # Since we already selected one pair (nums1[i - 1], nums2[j - 1]), we can assume the minimal proceeding value is 0 mx, m, n = -math.inf, len(nums1), len(nums2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i, num1 in enumerate(nums1): for j, num2 in enumerate(nums2): p = num1 * num2 mx = max(p, mx) p = max(p, 0) dp[i + 1][j + 1] = max(dp[i][j] + p, dp[i + 1][j], dp[i][j + 1]) return mx if mx <= 0 else dp[m][n] # n, m = len(nums1), len(nums2) # dp = [-math.inf] * (m + 1) # for i in range(1, n + 1): # dp, old_dp = [-math.inf], dp # for j in range(1, m + 1): # dp += max( # old_dp[j], # not select i # dp[-1], # not select j # old_dp[j - 1], # not select either # max(old_dp[j - 1], 0) + nums1[i - 1] * nums2[j - 1], # select both # ), # return dp[-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: if not nums1: return 0 cache = [[0 for _ in nums2] for _ in nums1] cache[-1][-1] = nums1[-1] * nums2[-1] for i in range(len(nums1)-2, -1, -1): cache[i][-1] = max(nums1[i]*nums2[-1], cache[i+1][-1]) for i in range(len(nums2)-2, -1, -1): cache[-1][i] = max(nums1[-1]*nums2[i], cache[-1][i+1]) for i in range(len(nums1)-2, -1, -1): for j in range(len(nums2)-2, -1, -1): cache[i][j] = max( nums1[i] * nums2[j] + max(cache[i+1][j+1], 0), cache[i+1][j], cache[i][j+1] ) return cache[0][0]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n, m = len(nums1), len(nums2) dp = [[0] * (m) for i in range(n)] for i in range(n): for j in range(m): dp[i][j] = nums1[i] * nums2[j] if i and j: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = collections.defaultdict(lambda: -math.inf) for (i,j) in product(range(len(nums1)), range(len(nums2))): x, y = nums1[i]*nums2[j], dp[(i-1,j-1)] m = max(x,y) if x<0 and y<0 else max(x,0)+max(y,0) dp[(i,j)] = max(dp[(i-1,j)], dp[(i,j-1)], m) return dp[(len(nums1)-1, len(nums2)-1)]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
# class Solution: # def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # # max over three different situations # # use memoization to avoid repeating. subproblems from functools import lru_cache class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: @lru_cache(None) def helper(i, j): if i == 0 or j == 0: return -math.inf return max(helper(i - 1, j - 1), helper(i, j - 1), helper(i - 1, j), max(helper(i - 1, j - 1), 0) + nums1[i - 1] * nums2[j - 1]) return helper(len(nums1), len(nums2))
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
# DP import sys class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # DP[i][j] -- max-dot-prod(nums1[0:i], nums[0:j]) M = len(nums1) N = len(nums2) dp = [[-sys.maxsize] * N for _ in range(M)] max_p = -sys.maxsize for i, num in enumerate(nums2): max_p = max(max_p, nums1[0] * nums2[i]) dp[0][i] = max_p max_p = -sys.maxsize for i, num in enumerate(nums1): max_p = max(max_p, nums1[i] * nums2[0]) dp[i][0] = max_p for i in range(1, M): for j in range(1, N): x = nums1[i] * nums2[j] dp[i][j] = max(x + max(dp[i - 1][j - 1], 0), dp[i-1][j], dp[i][j-1]) return dp[M-1][N-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [] maxes = [] for i in range(len(nums1)): dp.append([]) maxes.append([]) for j in range(len(nums2)): dp[i].append(0) maxes[i].append(0) for i in range(len(nums1)): dp[i][0] = nums1[i] * nums2[0] if i > 0: dp[i][0] = max(dp[i][0], dp[i-1][0]) for j in range(len(nums2)): dp[0][j] = nums1[0] * nums2[j] if j > 0: dp[0][j] = max(dp[0][j], dp[0][j-1]) for i in range(1, len(nums1)): for j in range(1, len(nums2)): dp[i][j] = max(dp[i-1][j-1] + nums1[i] * nums2[j], nums1[i] * nums2[j]) dp[i][j] = max(dp[i][j], dp[i-1][j]) dp[i][j] = max(dp[i][j-1], dp[i][j]) return dp[-1][-1] # dp = [] # maxes = [] # for i in range(len(nums1)): # dp.append([]) # maxes.append([]) # for j in range(len(nums2)): # dp[i].append(0) # maxes[i].append(0) # for i in range(len(nums1)): # dp[i][0] = nums1[i] * nums2[0] # maxes[i][0] = nums1[i] * nums2[0] # if i > 0: # maxes[i][0] = max(maxes[i-1][0], nums1[i] * nums2[0]) # for j in range(len(nums2)): # dp[0][j] = nums1[0] * nums2[j] # maxes[0][j] = nums1[0] * nums2[j] # if j > 0: # maxes[0][j] = max(maxes[0][j-1], nums1[0] * nums2[j]) # for i in range(1, len(nums1)): # for j in range(1, len(nums2)): # dp[i][j] = max(maxes[i-1][j-1] + nums1[i] * nums2[j], # nums1[i] * nums2[j]) # maxes[i][j] = max(maxes[i-1][j-1], maxes[i-1][j]) # maxes[i][j] = max(maxes[i][j], maxes[i][j-1]) # maxes[i][j] = max(maxes[i][j], dp[i][j]) # return maxes[i][j]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2): if all(x >= 0 for x in nums1) and all(y <= 0 for y in nums2): return min(nums1) * max(nums2) if all(x <= 0 for x in nums1) and all(y >= 0 for y in nums2): return max(nums1) * min(nums2) a, b = len(nums1), len(nums2) dp = [[0 for _ in range(b)] for _ in range(a)] for i in range(a): for j in range(b): dp[i][j] = nums1[i] * nums2[j] if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) if i and j: dp[i][j] = max(dp[i][j], nums1[i] * nums2[j] + dp[i-1][j-1]) return dp[a-1][b-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) m = len(nums2) dp = [[0 for _ in range(m)] for _ in range(n)] dp[0][0] = nums1[0]*nums2[0] for i in range(1,m): dp[0][i] = max(dp[0][i-1],nums1[0]*nums2[i]) for i in range(1,n): dp[i][0] = max(dp[i-1][0],nums2[0]*nums1[i]) for i in range(1,n): for j in range(1,m): dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j],dp[i-1][j-1]+nums1[i]*nums2[j],nums1[i]*nums2[j]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [[0] * len(nums2) for _ in range(len(nums1))] for i in range(len(nums1)): for j in range(len(nums2)): dp[i][j] = nums1[i] * nums2[j] if i >= 1: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j >= 1: dp[i][j] = max(dp[i][j], dp[i][j - 1]) if i >= 1 and j >= 1: dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + nums1[i] * nums2[j]) return dp[len(nums1) - 1][len(nums2) - 1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: a, b = [None] + nums1, [None] + nums2 m, n = len(a), len(b) dp = [[-math.inf] * n for _ in range(m)] for i in range(1, m): for j in range(1, n): here = a[i] * b[j] dp[i][j] = max( here, dp[i - 1][j - 1] + max(here, 0), dp[i - 1][j], dp[i][j - 1], ) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: m, n = len(nums1), len(nums2) dp = [[-math.inf]*(n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): a = dp[i-1][j] b = dp[i][j-1] c = max(dp[i-1][j-1], nums1[i-1]*nums2[j-1], dp[i-1][j-1]+nums1[i-1]*nums2[j-1]) dp[i][j] = max(a, b, c) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: arr = [[-math.inf for i in range(len(nums1)+1)] for j in range(len(nums2)+1)] for i in range(len(nums2)+1): arr[i][0] = -math.inf for i in range(len(nums1)+1): arr[0][i] = -math.inf maxc = nums2[0] * nums1[0] for i in range(len(nums2)+1)[1:]: for j in range(len(nums1)+1)[1:]: arr[i][j] = max(arr[i][j], arr[i][j-1]) arr[i][j] = max(arr[i][j], arr[i-1][j]) arr[i][j] = max(arr[i][j], max(arr[i-1][j-1], 0) + nums2[i-1]*nums1[j-1]) if arr[i][j] > maxc: maxc = arr[i][j] return maxc
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
import sys from typing import List class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: result = -sys.maxsize len1 = len(nums1) len2 = len(nums2) dp = [[-sys.maxsize for j in range(len2)] for i in range(len1)] for i in range(0, len1): for j in range(0, len2): dp[i][j] = nums1[i] * nums2[j] if i > 0: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j > 0: dp[i][j] = max(dp[i][j], dp[i][j - 1]) if i > 0 and j > 0: dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + nums1[i] * nums2[j]) result = max(result, dp[i][j]) pass pass return result
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1 = len(nums1) n2 = len(nums2) dp = [[0] * (n2) for j in range(n1)] for i in range(n1): for j in range(n2): dp[i][j] = nums1[i]*nums2[j] if i and j: dp[i][j] += max(dp[i-1][j-1], 0) if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) return dp[-1][-1] # dp[i+1][j+1] = max(dp[i][j] + max(0, nums1[i] * nums2[j]), dp[i][j+1], dp[i+1][j]) # # print(dp) # if dp[-1][-1] != 0: # return dp[-1][-1] # nums1.sort() # nums2.sort() # a = nums1[0] if nums1[0] > 0 else nums1[-1] # b = nums2[0] if nums2[0] > 0 else nums2[-1] # return a * b
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: m,n=len(nums1),len(nums2) dp=[[0]*n for _ in range(m)] #(i,j)到当前位置的最大子序列乘积和 for i in range(m): for j in range(n): dp[i][j]=nums1[i]*nums2[j] if i>0 and j>0: #此时判断是否与前边连接 dp[i][j]+=max(0,dp[i-1][j-1]) #当前i,j只取一个 if i>0: dp[i][j]=max(dp[i][j],dp[i-1][j]) if j>0: dp[i][j]=max(dp[i][j],dp[i][j-1]) return dp[-1][-1] #类似于72:编辑距离 #dp[i][j]代表nums1到i位,nums2到j位的最大乘积和 #由于是子序列,要判断取i还是取j,以及前边的子序列要不要取
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2) -> int: n, m = len(nums1), len(nums2) dp1 = [[-float('inf')] * (m) for _ in range(n)] dp2 = [[-float('inf')] * (m) for _ in range(n)] for i in range(n): for j in range(m): dp1[i][j] = nums1[i] * nums2[j] if i != 0 and j != 0: dp1[i][j] = max(dp1[i][j], dp2[i-1][j-1] + nums1[i] * nums2[j]) dp2[i][j] = dp1[i][j] if i != 0: dp2[i][j] = max(dp2[i][j], dp2[i-1][j]) if j != 0: dp2[i][j] = max(dp2[i][j], dp2[i][j-1]) ans = -float('inf') for i in range(n): for j in range(m): ans = max(ans, dp2[i][j]) return ans
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
import numpy as np class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # DP(a=index of last, b=index of last) = max of: # DP(a-1, b) # DP(a-1, i) + nums1[a] * max_or_min(nums2[i+1:b+1]) # same for b INF = int(1e9) n, m = len(nums1), len(nums2) DP = np.ones((n + 1, m + 1), dtype=int) * -INF for a in range(n): for b in range(m): el = nums1[a] * nums2[b] diag = DP[a, b] DP[a + 1, b + 1] = max(el, DP[a, b + 1], DP[a + 1, b], diag, diag + el) return DP[-1, -1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2): m = len(nums1) n = len(nums2) mem = [[None]*n for i in range(m)] for i in range(m-1,-1,-1): for j in range(n-1,-1,-1): mem[i][j] = nums1[i] * nums2[j] #myPrint(mem) for i in range(m-1,-1,-1): #max_ = mem[i+1][n-1] if i<m-1 else -1001 for j in range(n-1,-1,-1): #print('i,j : ' ,i,j) #if mem[i][j] < 0 and max_ < 0: # mem[i][j] = max(mem[i][j], max_) #else: #print('here2 :', max_) #mem[i][j] = max(mem[i][j] + max_, max(mem[i][j], max_)) max_ = mem[i+1][j+1] if i+1<m and j+1<n else -1001 mem[i][j] = max(mem[i][j] + max_, max(mem[i][j], max_)) #print('here : ', mem[i][j]) mem[i][j] = max(mem[i][j], mem[i][j+1] if j<n-1 else -1001) mem[i][j] = max(mem[i][j], mem[i+1][j] if i<m-1 else -1001) #max_ = max(max_, mem[i+1][j] if i<m-1 else -1001) #myPrint(mem) return mem[0][0]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
MAX_VAL = -10 ** 9 - 7 class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: num_rows = len(nums1) + 1 num_cols = len(nums2) + 1 dp = [] for i in range(num_rows): dp.append([MAX_VAL] * num_cols) for i in range(1, num_rows): for j in range(1, num_cols): dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + nums1[i-1] * nums2[j-1], nums1[i-1] * nums2[j-1]) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, A: List[int], B: List[int]) -> int: n=len(A) m=len(B) dp=[[0]*(m) for _ in range(n)] for i in range(n): for j in range(m): dp[i][j]=A[i]*B[j] if i and j: dp[i][j]+=max(dp[i-1][j-1],0) if i: dp[i][j]=max(dp[i][j],dp[i-1][j]) if j: dp[i][j]=max(dp[i][j],dp[i][j-1]) return dp[-1][-1] ''' [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] 0 0 * 2 3 [[6, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] 0 1 * 2 0 [[6, 6, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] 0 2 * 2 -6 [[6, 6, 6], [0, 0, 0], [0, 0, 0], [0, 0, 0]] ---------------------- 1 0 * 1 3 [[6, 6, 6], [6, 0, 0], [0, 0, 0], [0, 0, 0]] 1 1 * 1 0 [[6, 6, 6], [6, 6, 0], [0, 0, 0], [0, 0, 0]] 1 2 * 1 -6 [[6, 6, 6], [6, 6, 6], [0, 0, 0], [0, 0, 0]] ---------------------- 2 0 * -2 3 [[6, 6, 6], [6, 6, 6], [6, 0, 0], [0, 0, 0]] 2 1 * -2 0 [[6, 6, 6], [6, 6, 6], [6, 6, 0], [0, 0, 0]] 2 2 * -2 -6 [[6, 6, 6], [6, 6, 6], [6, 6, 18], [0, 0, 0]] ---------------------- 3 0 * 5 3 [[6, 6, 6], [6, 6, 6], [6, 6, 18], [15, 0, 0]] 3 1 * 5 0 [[6, 6, 6], [6, 6, 6], [6, 6, 18], [15, 15, 0]] 3 2 * 5 -6 [[6, 6, 6], [6, 6, 6], [6, 6, 18], [15, 15, 18]] ---------------------- [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0] '''
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: L1=len(nums1) L2=len(nums2) temp=[[u*v for v in nums2] for u in nums1] dic={} dic[(0,0)]=temp[0][0] def search(i,j): if (i,j) in dic: return dic[(i,j)] if i==0: dic[(0,j)]=max(search(0,j-1),temp[0][j]) return dic[(i,j)] elif j==0: dic[(i,0)]=max(search(i-1,0),temp[i][0]) return dic[(i,0)] else: dic[(i,j)]=max(search(i-1,j), search(i,j-1), temp[i][j]+search(i-1,j-1), temp[i][j]) return dic[(i,j)] return search(L1-1,L2-1)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: L1=len(nums1) L2=len(nums2) temp=[[u*v for v in nums2] for u in nums1] dic={} dic[(0,0)]=temp[0][0] def search(i,j): a=temp[i][j] if (i,j) in dic: return dic[(i,j)] if i==0: dic[(0,j)]=max(search(0,j-1),a) return dic[(i,j)] elif j==0: dic[(i,0)]=max(search(i-1,0),a) return dic[(i,0)] else: dic[(i,j)]=max(search(i-1,j), search(i,j-1), a+search(i-1,j-1), a) return dic[(i,j)] return search(L1-1,L2-1)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # fuck!!! I finally did a DP problem!!! CRYING n1 = len(nums1) n2 = len(nums2) dp = [[0] * (n2) for j in range(n1)] for i in range(n1): for j in range(n2): dp[i][j] = nums1[i]*nums2[j] if i and j: dp[i][j] += max(dp[i-1][j-1], 0) if i: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j-1]) return dp[-1][-1] # dp[i+1][j+1] = max(dp[i][j] + max(0, nums1[i] * nums2[j]), dp[i][j+1], dp[i+1][j]) # # print(dp) # if dp[-1][-1] != 0: # return dp[-1][-1] # nums1.sort() # nums2.sort() # a = nums1[0] if nums1[0] > 0 else nums1[-1] # b = nums2[0] if nums2[0] > 0 else nums2[-1] # return a * b
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: N, M = len(nums1), len(nums2) dp = [[None] * M for _ in range(N)] def solve(i, j): if i >= N or j >= M: return 0 if dp[i][j] is None: dp[i][j] = max(nums1[i] * nums2[j] + solve(i+1, j+1), solve(i+1, j), solve(i, j+1), solve(i+1, j+1)) return dp[i][j] result = solve(0, 0) if result == 0 and 0 not in nums1 + nums2: if nums1[0] < 0: return max(nums1) * min(nums2) else: return min(nums1) * max(nums2) else: return result
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [[-sys.maxsize-1 for i in range(len(nums2)+1)] for j in range(len(nums1)+1)] for i in range(1,len(nums1)+1): for j in range(1,len(nums2)+1): t = nums1[i-1]*nums2[j-1] if dp[i-1][j-1] == -sys.maxsize-1: dp[i][j] = max(t,max(dp[i-1][j], dp[i][j-1])) else: dp[i][j] = max(dp[i-1][j-1]+t,max(dp[i-1][j], dp[i][j-1])) dp[i][j] = max(t,dp[i][j]) # print(dp) return dp[-1][-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dic={} dic[(0,0)]=nums1[0]*nums2[0] def search(i,j): a=nums1[i]*nums2[j] if (i,j) in dic: return dic[(i,j)] if i==0: dic[(0,j)]=max(search(0,j-1),a) return dic[(i,j)] elif j==0: dic[(i,0)]=max(search(i-1,0),a) return dic[(i,0)] else: dic[(i,j)]=max(search(i-1,j), search(i,j-1), a+search(i-1,j-1), a) return dic[(i,j)] return search(len(nums1)-1,len(nums2)-1)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [[0 for j in range(len(nums2))] for i in range(len(nums1))] dp[0][0] = nums1[0] * nums2[0] for j in range(1, len(nums2)): dp[0][j] = max(dp[0][j-1], nums1[0] * nums2[j]) for i in range(1, len(nums1)): dp[i][0] = max(dp[i - 1][0], nums1[i] * nums2[0]) for i in range(1, len(nums1)): for j in range(1, len(nums2)): dp[i][j] = max(max(dp[i-1][j - 1], 0) + (nums1[i] * nums2[j]), dp[i-1][j], dp[i][j-1]) return dp[len(nums1) - 1][len(nums2) - 1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
from functools import lru_cache class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: @lru_cache(None) def dot(i, j): if i == N1 or j == N2: return -sys.maxsize ans = nums1[i]*nums2[j] ans = max(ans, ans + dot(i + 1, j + 1), dot(i, j + 1), dot(i + 1, j), dot(i + 1, j + 1) ) return ans N1, N2 = len(nums1), len(nums2) return dot(0, 0)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: m = len(nums1) n = len(nums2) dp = [[-math.inf] * (n) for _ in range(m)] for i in range(0, m): for j in range(0, n): if i > 0: dp[i][j] = max(dp[i][j], dp[i-1][j]) if j > 0: dp[i][j] = max(dp[i][j], dp[i][j-1]) dp[i][j] = max(dp[i][j], nums1[i] * nums2[j]) if i > 0 and j>0 and dp[i-1][j-1] > 0: dp[i][j] = max(dp[i][j], dp[i-1][j-1] + nums1[i] * nums2[j]) return dp[m-1][n-1]
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
def maxsub(a,b): dp = [[0 for x in range(len(b))] for x in range(len(a))] for i in range(len(a)): for j in range(len(b)): dp[i][j] = a[i]*b[j] if i-1>=0: dp[i][j] = max(dp[i][j],dp[i-1][j] ) if j-1>=0: dp[i][j] = max(dp[i][j],dp[i][j-1] ) if i-1>=0 and j-1>=0: dp[i][j] = max(dp[i][j],dp[i-1][j-1]+a[i]*b[j] ) return dp[-1][-1] class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: return maxsub(nums1, nums2)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: return dp_with_recursion(nums1, nums2) def dp_with_recursion(a, b): @lru_cache(maxsize=None) def recursion(i,j, is_empty=True): if i < 0 or j < 0: return float('-inf') if is_empty else 0 # BUG, BUG! #return max(recursion(i, j-1), recursion(i-1,j), recursion(i-1,j-1) + max(0, a[i]*b[j])) #return max(recursion(i, j-1), recursion(i-1,j), recursion(i-1, j-1, False) + a[i]*b[j]) return max(recursion(i, j-1, is_empty), recursion(i-1,j, is_empty), recursion(i-1, j-1, False) + a[i]*b[j]) return recursion(len(a)-1, len(b)-1)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: a =[[-1 for _ in range(len(nums2))] for _ in range(len(nums1))] def dp(x, y): if len(nums1) ==x or len(nums2) ==y: return -float('inf') if a[x][y] != -1: return a[x][y] else: a[x][y] = max(dp(x+1, y), dp(x, y+1), dp(x+1, y+1) + nums1[x]*nums2[y], nums1[x]*nums2[y]) return a[x][y] return dp(0,0)
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
from functools import lru_cache class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: @lru_cache(None) def solve(i, j, c): if i == len(nums1) or j == len(nums2): if c: return 0 return float('-inf') return max(solve(i+1, j, c), solve(i, j+1, c), nums1[i]*nums2[j] + solve(i+1, j+1, True)) return solve(0, 0, False)
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: letters = {} for i, char in enumerate(text): if char in letters: letters[char].append(i) else: letters[char] = [i] if len(letters) == 1: return len(text) ans = 0 for letter in letters: cur = 0 prev = 0 discarded = False maxSoFar = 0 arr = letters[letter] for j, pos in enumerate(arr): if not j: cur = 1 elif pos - arr[j-1] == 1: cur += 1 else: if not discarded and prev: discarded = True elif not discarded and pos - arr[j-1] > 2: discarded = True if prev + cur > maxSoFar: maxSoFar = prev+cur if pos - arr[j-1] == 2: prev = cur cur = 1 else: prev = 0 cur = 1 print((prev+cur)) if prev + cur > maxSoFar: maxSoFar = prev+cur if discarded: maxSoFar+=1 if maxSoFar > ans: ans = maxSoFar return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, S: str) -> int: # We get the group's key and length first, e.g. 'aaabaaa' -> [[a , 3], [b, 1], [a, 3] A = [[c, len(list(g))] for c, g in itertools.groupby(S)] # We also generate a count dict for easy look up e.g. 'aaabaaa' -> {a: 6, b: 1} count = collections.Counter(S) # only extend 1 more, use min here to avoid the case that there's no extra char to extend res = max(min(k + 1, count[c]) for c, k in A) # merge 2 groups together for i in range(1, len(A) - 1): # if both sides have the same char and are separated by only 1 char if A[i - 1][0] == A[i + 1][0] and A[i][1] == 1: # min here serves the same purpose res = max(res, min(A[i - 1][1] + A[i + 1][1] + 1, count[A[i + 1][0]])) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: for k,g in itertools.groupby(text): print(list(g)) G = [[k,len(list(g))] for k,g in itertools.groupby(text)] c = collections.Counter(text) res = max(min(n+1, c[k]) for k,n in G) for i in range(1,len(G)-1): if G[i-1][0] == G[i+1][0] and G[i][1] == 1: res = max(min(G[i-1][1]+G[i+1][1]+1, c[G[i-1][0]]), res) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
import itertools import collections class Solution: def maxRepOpt1(self, text: str) -> int: import collections counter = collections.Counter(text) cnt = collections.defaultdict(int) start = res = max_cnt = 0 for end, char in enumerate(text): cnt[char] += 1 max_cnt = max(max_cnt, cnt[char]) while end - start + 1 > max_cnt +1: cnt[text[start]] -= 1 max_cnt = max(cnt.values()) # This line can be commented start += 1 res = max(res, min(end - start + 1, counter[char])) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: be = 1 n = collections.Counter(text) maxl = 0 i = 1 re = [] now = text[0] while(i<len(text)): if now == text[i]: be += 1 if now != text[i] or i == len(text)-1: res = be if len(re)>=2: print(re) com,lencom = re.pop(0) if re[-1][1] == 1 and com == now: res = lencom + be if res<n[now]: res += 1 maxl = max(maxl,res) #print(re) re.append((now,be)) now = text[i] be = 1 i += 1 return maxl
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
import itertools from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: ref = [ [letter,len(list(s))] for letter,s in itertools.groupby(text)] count = Counter(text) res = max( [ min(count.get(i), k + 1) for i , k in ref] ) for i in range(1,len(ref) - 1): if ref[i-1][0] == ref[i+1][0] and ref[i][1] == 1 : res = max(res, min(ref[i-1][1] + ref[i+1][1] + 1, count.get(ref[i-1][0])) ) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: # get group's key and length, e.g. 'aaabaa' -> [[a, 3], [b, 1], [a, 3]] # itertools.groupby function returns consecutive keys and groups from the iterable. # note itertools.groupby(iterable, key_func) supports lambda function # If the key function is not specified or is None, key defaults to an identity function and returns the element unchanged. A = [[c, len(list(g))] for c, g in itertools.groupby(text)] # use count dict as upper bound 'aaabaa' -> {a: 6, b: 1} count = collections.Counter(text) # case 1: extend 1 more, use min here to avoid the case that there's no extra char to extend res = max(min(k + 1, count[c]) for c, k in A) # case 2: merge 2 groups together with 1 char in between for i in range(1, len(A) - 1): if A[i][1] == 1 and A[i - 1][0] == A[i + 1][0]: res = max(res, min(A[i - 1][1] + 1 + A[i + 1][1], count[A[i - 1][0]])) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: text_cnt = collections.Counter(text) d = collections.Counter() l,r = 0, 0 maxcnt = 0 res = 0 N = len(text) for r, ch in enumerate(text): d[ch]+=1 maxcnt = max(maxcnt, d[ch]) while r-l+1>maxcnt+1: d[text[l]]-=1 maxcnt = max(d.values()) l+=1 res = max(res, min(r-l+1, text_cnt[ch])) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: if len(text) == 1: return 1 counts = Counter(text) # a b a b a. pre = [1] nex = [1] ans = 1 for i in range(1, len(text)): if text[i] == text[i - 1]: pre.append(pre[-1] + 1) else : pre.append(1) ans = max(pre[i], ans) for i in range(len(text) - 2, -1, -1): if text[i] == text[i + 1]: nex.append(nex[-1] + 1) else: nex.append(1) ans = max(nex[len(text) - i - 1], ans) nex.reverse() # Now try some swaps. for i in range(len(text)): # We will try to swap out i. if i != len(text) - 1 and text[i] != text[i + 1]: l = nex[i + 1] if counts[text[i + 1]] > l: ans = max(ans, 1 + l) if i != 0 and text[i] != text[i - 1]: l = pre[i - 1] if counts[text[i - 1]] > l: ans = max(ans, 1 + l) if i != 0 and i != len(text) - 1 and text[i - 1] == text[i + 1]: # Match both forwards and backwards. l = pre[i - 1] + nex[i + 1] if counts[text[i - 1]] > l: ans = max(ans, 1 + l) else: # Can we still make it better? ans = max(ans, l) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: inuse = collections.defaultdict(int) # chars used in the repeated substring left = collections.defaultdict(int) # not used chars MOVE_TO_THE_NEXT_CHAR = 1 REPLACE = 0 LEAVE_AS_IT_IS = -1 res, i, n = 1, 0, 0 # initially no chars are used yet for ch in text: left[ch] += 1 for j, ch in enumerate(text): inuse[ch] += 1 # since we use this char left[ch] -= 1 # subtract its count from left dict n = max(inuse[ch], n) # this defines action we are going to take action = j - i - n char_exists = False if action >= MOVE_TO_THE_NEXT_CHAR: inuse[text[i]] -= 1 left[text[i]] += 1 # add it to the left dict back, since it is no longer in use i += 1 # we can replace only if we have unused char in the left dict elif action == REPLACE: char_exists = left[ch] > 0 elif action == LEAVE_AS_IT_IS: char_exists = True if char_exists and j - i >= res: res = j - i + 1 return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: from collections import Counter counter = Counter(text) if not text: return 0 if len(text) == 1: return 1 def helper(text): start_idx, end_idx = 0, 0 first_found = None ret = 0 while end_idx < len(text): if text[end_idx] == text[start_idx]: end_idx += 1 if counter[text[start_idx]] >= end_idx-start_idx: ret = max(ret, end_idx-start_idx) else: ret = max(ret, end_idx-start_idx-1) else: if not first_found: first_found = end_idx end_idx += 1 if counter[text[start_idx]] >= end_idx-start_idx: ret = max(ret, end_idx-start_idx) else: ret = max(ret, end_idx-start_idx-1) else: start_idx = end_idx = first_found first_found = None return ret return max(helper(text), helper(text[::-1]))
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: c = Counter(text) window = Counter() i = 0 ans = 1 for j in range(len(text)): c[text[j]] -= 1 window[text[j]] += 1 if (len(window) == 1 or (len(window) == 2 and min(window.values()) == 1 and c[sorted(window, key=window.get)[1]] > 0)): ans = max(ans, j - i + 1) else: c[text[i]] += 1 window[text[i]] -= 1 if window[text[i]] == 0: del window[text[i]] i += 1 return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: n = len(text) win = collections.Counter() def valid_win(win): if len(win) > 2: return False if len(win) == 1: return True return not all(n >= 2 for n in win.values()) max_c_idx = {} min_c_idx = {} for i, c in enumerate(text): if c not in max_c_idx: max_c_idx[c] = i elif i > max_c_idx[c]: max_c_idx[c] = i if c not in min_c_idx: min_c_idx[c] = i left, right = 0, 0 ans = 0 while right < n: c = text[right] right += 1 win[c] += 1 while not valid_win(win) and left < right: d = text[left] left += 1 win[d] -= 1 if win[d] == 0: win.pop(d) if len(win) == 1: ans = max(ans, right - left) else: k1, k2 = win.keys() if win[k1] == 1 and (max_c_idx[k2] >= right or min_c_idx[k2] < left): ans = max(ans, right - left) elif win[k2] == 1 and (max_c_idx[k1] >= right or min_c_idx[k1] < left): ans = max(ans, right - left) else: ans = max(ans, right - left - 1) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: max_char='' clist=[1]*len(text) one_max=1 compress_list=[] for i in range(1,len(text)): if text[i]==text[i-1]: clist[i]=clist[i-1]+1 else: clist[i]=1 compress_list.append([text[i-1],clist[i-1]]) if clist[i]>one_max: one_max=clist[i] max_char=text[i] compress_list.append([text[-1],clist[-1]]) print(compress_list) two_count=[] two_max=0 for i in range(2,len(compress_list)): if compress_list[i-1][1]>1: continue if compress_list[i][0]!=compress_list[i-2][0]: continue if compress_list[i][1]+compress_list[i-2][1]+1<two_max: continue c=compress_list[i][0] swaped=False for j in range(len(compress_list)): if j>=i-2 and j<=i: continue if compress_list[j][0]==c: len2=compress_list[i][1]+compress_list[i-2][1]+1 two_count.append(len2) if len2>two_max: two_max=len2 swaped=True if swaped: continue len2 = compress_list[i][1] + compress_list[i - 2][1] two_count.append(len2) if len2 > two_max: two_max = len2 if one_max>=two_max: char_count=0 for i in range(len(compress_list)): if compress_list[i][0]==max_char: char_count+=1 if char_count>1: one_max+=1 return max(one_max,two_max)
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: if len(text) == 0: return 0 seen = set() max_count = 0 i = j = 0 window = Counter() maxes = Counter() while j < len(text): if len(window) > 2 or (len(window) == 2 and min(window.values()) != 1): maxes[text[i]] = max(maxes[text[i]], window[text[i]]) window[text[i]] -= 1 if window[text[i]] == 0: del window[text[i]] i += 1 else: inc = 0 cur_char = text[j] window[cur_char] += 1 if cur_char in maxes: max_count = max(max_count, maxes[cur_char] + 1) inc = 1 max_count = max(max_count, window[cur_char] + inc) j += 1 return max_count
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: n = len(text) ans = 1 mp = {} for i in text: if i in mp: mp[i]+=1 else: mp[i]=1 for i in mp: first = 0 second = 0 if i in mp: for j in range(0,n): if text[j]==i: first+=1 if first+second<mp[i]: ans = max(ans,first+second+1) else: ans = max(ans,first+second) else: if j!=0 and j!=n-1 and text[j-1]==i and text[j+1]==i: second = first else: second = 0 first = 0 return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: if not text: return 0 compact = [ [text[0], 1] ] max_ = 1 for i in range(1, len(text)): if text[i] == compact[-1][0]: compact[-1][1] += 1 else: compact.append([text[i], 1]) max_ = max(max_, compact[-1][1]) last_idx = defaultdict(int) for i in range(len(compact)): last_idx[compact[i][0]] += compact[i][1] #print(compact) for i in range( len(compact) - 2 ): if compact[i][0] == compact[i+2][0] and compact[i+1][1] == 1 and compact[i][1] + compact[i+2][1] < last_idx[compact[i][0]]: max_ = max(max_, compact[i][1] + compact[i+2][1] + 1) if compact[i][0] == compact[i+2][0] and compact[i+1][1] == 1: max_ = max(max_, compact[i][1] + compact[i+2][1]) for i in range( len(compact)): if last_idx[compact[i][0]] > compact[i][1]: max_ = max(max_, compact[i][1] + 1) return max_
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: # Group letters by occurence # Ex: \"aaabaaa\" -> [['a', 3], ['b', 1], ['a', 3]] groups = [] for key, group in itertools.groupby(text): groups.append([key,len(list(group))]) # We need a counter to check if we can extend by one letter # Ex: \"aaa\" in \"aaabba\" can be extended to \"aaaa\" because we have another \"a\" at the end. count = collections.Counter(text) for index in range(len(groups)): # Check if we can merge two groups. # Ex \"aaa\" and \"aa\" in aaabaa\" can be merged to \"aaaaa\" if index <= len(groups) - 3: # Make sure to dont go out of bounds # The next groups's length must be 1 to be able to merge if groups[index+1][1] == 1 and groups[index+2][0] == groups[index][0]: groups[index][1] += groups[index+2][1] # Length will be extend by 1 if there are more letters in available, as explained above. if count[groups[index][0]] > groups[index][1]: groups[index][1] += 1 # Return the maximum length that exists maxlength = 0 for group in groups: if group[1] > maxlength: maxlength = group[1] return maxlength
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: def get_possible(idx, cur, step, swap): if idx >= len(positions): return step + swap if positions[idx] - 1 == cur: return get_possible(idx+1, positions[idx], step + 1, swap) elif not swap: return step else: return max( step+1, get_possible(idx, cur + 1, step + 1, False), get_possible(idx, positions[idx] - 1, 1, False), get_possible(idx + 1, positions[idx], 1, True), ) letters, ans = defaultdict(list), 0 for i, c in enumerate(text): letters[c].append(i) for _, positions in letters.items(): if len(positions) < 3: ans = max(ans, len(positions)) continue x = min(get_possible(1, positions[0], 1, True), len(positions)) ans = max(x, ans) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: ln=len(text) if ln==1: return 1 mx=-1 start=0 while start<ln: print(start,',',end='') i=start+1 inx=-1 while i<ln and (text[start]==text[i] or inx==-1): if text[start]!=text[i]: inx=i i+=1 print(i,',',end='') if inx!=-1 and (text[start] in text[i:] or text[start] in text[:start]): mx=max(mx,i-start) elif inx==-1 and (text[start] in text[i:] or text[start] in text[:start]): mx=max(mx,i-start+1) elif inx==-1: mx=max(mx,i-start) else: mx=max(mx,i-start-1) if inx==-1: start=i else: start=inx print(mx) return mx
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: # out tcounter = Counter(text) res = 0 counter = Counter() i = 0 for j in range(len(text)): counter[text[j]] += 1 while j-i+1-max(counter.values())>1: counter[text[i]] -= 1 i += 1 curr_max = max(counter.values()) c = [k for k in counter if counter[k]==curr_max][0] res = max(res, min(j-i+1, tcounter[c])) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: d={} f={} for i in text: d[i]=[] f[i]=f.get(i,0)+1 for i in list(d.keys()): s=0 same=0 diff=0 for j in text: if j==i: s=1 if j==i and s==1: if diff: d[i].append(-diff) same+=1 diff=0 else: if same: d[i].append(same) diff+=1 same=0 if same: d[i].append(same) if diff: d[i].append(-diff) l=0 print(d) for i in list(d.keys()): for j,v in enumerate(d[i]): if v>0: l=max(l,v) if j+2<len(d[i]) and d[i][j+1]==-1 : if d[i][j+2]+d[i][j]<f[i]: l=max(l,d[i][j+2]+d[i][j]+1) else: l=max(l,d[i][j+2]+d[i][j]) else: if d[i][j]<f[i]: l=max(l,d[i][j]+1) return l
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: if not text: return 0 cnt = Counter(list(text)) print(cnt) output = 0 for c, n in list(cnt.items()): val = 0 holder = 0 for i, t in enumerate(text): if t == c: val += 1 else: holder = val val = 0 if val + holder < n: op = val + holder + 1 else: op = val + holder output = max(output, op) return output
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: if not text: return 0 cnt = Counter(list(text)) output = 0 for c, n in cnt.items(): val = 0 holder = 0 for i, t in enumerate(text): if t == c: val += 1 else: holder = val val = 0 if val + holder < n: op = val + holder + 1 else: op = val + holder output = max(output, op) return output
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
from collections import Counter class Solution: def maxRepOpt1(self, text: str) -> int: if not text: return 0 cnt = Counter(list(text)) output = 0 for c, n in cnt.items(): val = 0 holder = 0 for i, t in enumerate(text): if t == c: val += 1 else: holder = val val = 0 if val + holder < n: op = val + holder + 1 else: op = val + holder output = max(output, op) return output
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: count = Counter(text) i = 0 diff = 1 sol = 0 while i < len(text): c = text[i] j = i+1 found = False while j < len(text): if text[j] == c: j+= 1 else: if found: if count[c] >= j-i: sol = max(sol, j-i) else: sol = max(sol, j-i-1) break found = True diff = j j += 1 if j == len(text): if count[c] > j-i and not found: sol = max(sol, j-i+1) if count[c] >= j-i: sol = max(sol, j-i) else: sol = max(sol, j-i-1) break i = diff return sol
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Unionfind: def __init__(self, n): self.par = [-1]*n self.rank = [1]*n def root(self, x): r = x while not self.par[r]<0: r = self.par[r] t = x while t!=r: tmp = t t = self.par[t] self.par[tmp] = r return r def unite(self, x, y): rx = self.root(x) ry = self.root(y) if rx==ry: return if self.rank[rx]<=self.rank[ry]: self.par[ry] += self.par[rx] self.par[rx] = ry if self.rank[rx]==self.rank[ry]: self.rank[ry] += 1 else: self.par[rx] += self.par[ry] self.par[ry] = rx def is_same(self, x, y): return self.root(x)==self.root(y) def count(self, x): return -self.par[self.root(x)] class Solution: def maxRepOpt1(self, text: str) -> int: n = len(text) cnt = Counter(list(text)) uf = Unionfind(n) for i in range(n-1): if text[i]==text[i+1]: uf.unite(i, i+1) ans = 0 for i in range(n): ans = max(ans, uf.count(i)+(1 if uf.count(i)<cnt[text[i]] else 0)) for i in range(n-2): if text[i]==text[i+2] and text[i]!=text[i+1]: total = uf.count(i)+uf.count(i+2) ans = max(ans, total+(1 if total<cnt[text[i]] else 0)) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: c = Counter(text) window = Counter() i = 0 ans = 1 for j in range(len(text)): c[text[j]] -= 1 window[text[j]] += 1 while not (len(window) == 1 or (len(window) == 2 and min(window.values()) == 1)): c[text[i]] += 1 window[text[i]] -= 1 if window[text[i]] == 0: del window[text[i]] i += 1 if len(window) == 1 or c[sorted(window, key=window.get)[1]] > 0: ans = max(ans, j - i + 1) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: n = len(text) ans = 1 mp = {} for i in text: if i in mp: mp[i]+=1 else: mp[i]=1 for i in range(26): first = 0 second = 0 if chr(i+97) in mp: for j in range(0,n): if text[j]==chr(97+i): first+=1 if first+second<mp[chr(i+97)]: ans = max(ans,first+second+1) else: ans = max(ans,first+second) else: if j!=0 and j!=n-1 and text[j-1]==chr(i+97) and text[j+1]==chr(i+97): second = first else: second = 0 first = 0 return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: lst, lookup = list(), dict() ptr = 0 while ptr < len(text): curr, count = text[ptr], 0 while ptr < len(text) and curr == text[ptr]: count += 1 ptr += 1 lookup[curr] = lookup.get(curr, 0) + count lst.append((curr, count)) res = 0 for i in range(len(lst)): if 0 < i < len(lst)-1: if lst[i-1][0] == lst[i+1][0] and lst[i][1] == 1: curlen = lst[i-1][1]+lst[i+1][1] if lookup[lst[i-1][0]] > curlen: curlen += 1 res = max(res, curlen) continue curlen = lst[i][1] if lookup[lst[i][0]] > curlen: curlen += 1 res = max(res, curlen) return res
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: hashmap = {} wordlist = [] l, r, ans = 0, 0, 0 while r < len(text): while r < len(text) and text[l] == text[r]: r += 1 wordlist.append([text[l], r-l]) if text[l] not in hashmap: hashmap[text[l]] = r-l else: hashmap[text[l]] += r-l l = r for ch, count in wordlist: ans = max(ans, min(count+1, hashmap[ch])) for i in range(1, len(wordlist)-1): if wordlist[i-1][0] == wordlist[i+1][0] and wordlist[i][1] == 1: ans = max(ans, min(wordlist[i-1][1] + wordlist[i+1][1]+1, hashmap[wordlist[i+1][0]])) return ans
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.   Example 1: Input: text = "ababa" Output: 3 Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3. Example 2: Input: text = "aaabaaa" Output: 6 Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6. Example 3: Input: text = "aaabbaaa" Output: 4 Example 4: Input: text = "aaaaa" Output: 5 Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5. Example 5: Input: text = "abcdef" Output: 1   Constraints: 1 <= text.length <= 20000 text consist of lowercase English characters only.
class Solution: def maxRepOpt1(self, text: str) -> int: for k,g in itertools.groupby(text): print(g) G = [[k,len(list(g))] for k,g in itertools.groupby(text)] c = collections.Counter(text) res = max(min(n+1, c[k]) for k,n in G) for i in range(1,len(G)-1): if G[i-1][0] == G[i+1][0] and G[i][1] == 1: res = max(min(G[i-1][1]+G[i+1][1]+1, c[G[i-1][0]]), res) return res
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: if len(s) < 2 ** k + k - 1: return False # Cannot be a string, as this is the de brujin length target = 2 ** k seen = set() cur_len = 0 for end in range(k, len(s) + 1): chunk = s[end - k: end] if chunk not in seen: cur_len += 1 seen.add(chunk) if cur_len == target: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: if len(s) < 2 ** k + k - 1: return False # Cannot be a string, as this is the de brujin length target = 2 ** k seen = set() cur_len = 0 for end in range(k, len(s) + 1): chunk = s[end - k: end] if chunk not in seen: cur_len += 1 seen.add(chunk) if cur_len == target: return True return len(seen) == target
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: a = 2**k k_string = set() for i in range(len(s)-k+1): if s[i:i+k] not in k_string: k_string.add(s[i:i+k]) if len(k_string) == a: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: nums = set() for i in range(0, len(s) - k + 1): nums.add(s[i:i+k]) if len(nums) == 2**k: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: seen, total = set(), 2 ** k for i in range(len(s) - k + 1): if s[i:i+k] not in seen: seen.add(s[i:i+k]) total -= 1 if total == 0: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: from itertools import permutations c = dict() count = 0 for i in range(0,len(s)-k+1): out = '' for j in range(i,i+k): out = out + s[j] if out not in c: c[out] = True count = count + 1 if count == pow(2,k): return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: bins = {} numbers = {} st = s print(k) def bin2dec(a): val = 0 for i in range(len(a)): if a[i] == '1': val += pow(2,(len(a)-1-i)) return (val) for i in range(len(st)-k+1): if (st[i:i+k]) in bins: continue else: bins[st[i:i+k]] = 1 for i in bins: # print(i) # print(bin2dec(i)) numbers[bin2dec(i)] = 1 c = 0 for i in range(pow(2,k)): if i in numbers: c += 1 print((c,k)) if c == pow(2,k): return True else: return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: def Hash_Function(b): num=list(b) num.reverse() total = 0 increment=1 for item in num: if item == '1': total=total+increment increment=increment*2 else: increment=increment*2 continue return total start = 0 end = k buckets = [] buckets = set(buckets) while end<=len(s): num=Hash_Function(s[start:end]) buckets.add(num) start+=1 end+=1 if len(buckets)==2**k: return True else: return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: check = [] for kk in range(2**k): t = bin(kk)[2:] check.append('0'*(k-len(t))+t) ss = set() for i in range(k,len(s)+1): ss.add(s[i-k:i]) for t in check: if t not in ss: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: l = len(s) r = set() target = 2 ** k for i in range(0, l-k+1): r.add( s[i:i+k] ) if len(r) == target: return True # else: return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: for i in range(2**k): bin_value = bin(i)[2:] if len(bin(i)[2:]) == k else '0' * (k - len(bin(i)[2:])) + bin(i)[2] if bin_value not in s: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
def binary_strings(k): res = [] stack = [''] while stack: s = stack.pop() if len(s) == k: res.append(s) else: stack.append(s + '0') stack.append(s + '1') return res class Solution: def hasAllCodes(self, s: str, k: int) -> bool: seen = set() for i in range(0, len(s) - k + 1): seen.add(s[i:i+k]) for bs in binary_strings(k): if bs not in seen: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: for i in range(2**k): ss=(bin(i)[2::]).zfill(k) if ss not in s: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: if len(s) < k: return False for i in range(2**k): if bin(i)[2:].rjust(k, '0') not in s: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
from itertools import product class Solution: def hasAllCodes(self, s: str, k: int) -> bool: p = product('01', repeat=k) for sub in p: if s.find(''.join(sub)) == -1: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: codes = {format(i, f'0{k}b'): False for i in range(2**k)} for i in range(len(s) - k + 1): ss = s[i:i + k] if ss in codes: codes[ss] = True return all(codes.values())
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: arr_2 = ['0','1'] m = k-1 while (m!=0): arr_2 = ['0'+i for i in arr_2]+['1'+i for i in arr_2] m -= 1 for i in range(2**k): if arr_2[i] in s: continue else: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: def binary_code_of_length_k(k): if k == 1: return ['0', '1'] t = binary_code_of_length_k(k-1) return ['0' + i for i in t] + ['1' + i for i in t] k_string = binary_code_of_length_k(k) for i in k_string: if s.find(i) == -1: return False return True
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: codes = set(format(i, f'0{k}b') for i in range(2**k)) found = [False for i in range(2 ** k)] for i in range(len(s) - k + 1): if s[i:i + k] in codes: found[int(s[i:i+k], base=2)] = True return all(found)
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: combinations = 2**k seen = set() for i in range(len(s)-k+1): temp = s[i:i+k] if temp not in seen: seen.add(temp) combinations -= 1 if combinations == 0: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: if len(s) < k: return False substring_set = set() need = 1 << k for start in range(len(s) - k + 1): ss = s[start:start+k] if ss not in substring_set: substring_set.add(ss) need -= 1 if need == 0: return True return False
Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False.   Example 1: Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2: Input: s = "00110", k = 2 Output: true Example 3: Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. Example 4: Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and doesn't exist in the array. Example 5: Input: s = "0000000001011100", k = 4 Output: false   Constraints: 1 <= s.length <= 5 * 10^5 s consists of 0's and 1's only. 1 <= k <= 20
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: st = set() for i in range(k, len(s) + 1): st.add(s[i - k : i]) if len(st) == 1 << k: break return len(st) == 1 << k