Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: cnt = collections.Counter() cnt[0] = 0 ans = 0 prefix = 0 for n in nums: prefix += n # if p if prefix-target in cnt: ans += 1 cnt = collections.Counter() cnt[prefix] += 1 # print(cnt, ans) # print('--') return ans # dic = {0:0} # ans = 0 # sum = 0 # for i in range(len(nums)): # sum+=nums[i] # if sum-target in dic: # ans+=1 # dic = {} # dic[sum] = i # return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) prefix = [0] * (n + 1) for i in range(n): prefix[i + 1] = prefix[i] + nums[i] seen = {0:0} ans = 0 for i in range(1,n + 1): curr = prefix[i] a = curr - target if a in seen: ans += 1 seen = {} seen[curr] = i return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: d = {0:1} total = 0 count = 0 for i in nums: total += i if total - target in d: print(total) count += 1 d = {0:1} total = 0 else : d[total] = 1 return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pos={0:-1} n=len(nums) dp=[0]*(n+1) s=0 for i in range(n): s+=nums[i] dp[i+1]=dp[i] t=s-target if t in pos: dp[i+1]=max(dp[i+1],dp[pos[t]+1]+1) pos[s]=i return dp[-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) pref_sum = [0 for _ in range(n + 1)] for i in range(n): pref_sum[i + 1] = pref_sum[i] + nums[i] dp = collections.defaultdict(lambda: -1) cur_max = 0 dp[0] = 0 for pref in range(1, n + 1): cur_max = max(cur_max, dp[pref_sum[pref] - target] + 1) dp[pref_sum[pref]] = cur_max return cur_max
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) dp = [0 for i in range(n+1)] d = {} d[0] = -1 s = 0 for i in range(n): s+=nums[i] t = s-target dp[i+1] = dp[i] if t in d: dp[i+1] = max(dp[i+1],dp[d[t]+1]+1) d[s] = i # print(d) return dp[n]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) dp = [0] * (n+1) dic = {} dic[0] = 0 cur = 0 for i in range(1, n+1): cur += nums[i - 1] if cur - target in dic: dp[i] = 1 + dp[dic[cur - target]] dp[i] = max(dp[i-1], dp[i]) dic[cur] = i return dp[n]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) preSum = [0 for i in range(n+1)] hashmap = {0:0} lastend = 0 ans = 0 for i in range(1, n+1): preSum[i] = preSum[i-1] + nums[i-1] begin = hashmap.get(preSum[i]-target, -1) if begin >= 0 and begin >= lastend: lastend = i ans += 1 pos = hashmap.get(preSum[i], 0) hashmap[preSum[i]] = max(pos, i) return ans # n = len(nums) # dp = [0 for i in range(n)] # ans = 0 # if target == nums[0]: # dp[0] = 1 # for i in range(1, n): # dp[i] = dp[i-1] # tmpsum = 0 # for j in range(i, -1, -1): # tmpsum += nums[j] # if tmpsum == target: # if j > 0: # dp[i] = max(dp[i], dp[j-1] + 1) # else: # dp[i] = max(dp[i], 1) # break # # print(dp) # return dp[n-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: seen = set() seen.add(0) curr = 0 res = 0 for n in nums: curr += n if curr-target in seen: res += 1 curr = 0 seen = set([0]) else: seen.add(curr) return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: d = {0:True} sm = 0 count = 0 for i in nums: sm += i if sm-target in d: count += 1 sm = 0 d = {0:True} else: d[sm] = True # print(sm,count) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: subarrays = [] summ = 0 lookup = set() count = 0 lookup.add(0) for num in nums: summ += num if summ - target in lookup: count += 1 summ = 0 lookup = set([0]) else: lookup.add(summ) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) prefix_sum = [0] * n # prefix_sum[i] = sum of nums[0...i] inclusive lookup = {} # lookup[sum] = rightmost index st prefix_sum[idx] == sum dp = [0] * n # dp[i] = most non overlapping subarrays = target in nums[0...i] # dp[i] = max(dp[i - 1], dp[j] + 1 (if j+1...i == target)) if i > 0 # dp[0] = 1 if nums[0] == target else 0 dp[0] = 1 if nums[0] == target else 0 prefix_sum[0] = nums[0] lookup[nums[0]] = 0 for i in range(1, len(nums)): prefix_sum[i] = prefix_sum[i - 1] + nums[i] check_for = prefix_sum[i] - target cmp_val = 0 if check_for in lookup: cmp_val = dp[lookup[check_for]] + 1 elif check_for == 0: cmp_val = 1 dp[i] = max(dp[i - 1], cmp_val) lookup[prefix_sum[i]] = i return dp[-1] # 0 0 0 # prefix_sum [0, 0] # lookup {0:1} # dp [1, ]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums, target): d = {0: -1} prefix = 0 count = 0 for i in range(len(nums)): prefix += nums[i] found = prefix - target if found in d: count += 1 d = {} d[prefix] = i return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pos = {0: 0} cumsum = 0 ans = 0 for num in nums: cumsum += num pos[cumsum] = max(ans, pos.get(cumsum-target, -1)+1) ans = max(ans, pos[cumsum]) #print(pos, cumsum) return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, A: List[int], T: int) -> int: N = len(A) prefix_sum = [0 for _ in range(N + 1)] for i in range(1, N + 1): prefix_sum[i] = prefix_sum[i - 1] + A[i - 1] seen = set([0]) res = 0 for i in range(N): curr = prefix_sum[i + 1] want = curr - T if want in seen: res += 1 seen = set() seen.add(curr) return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: cnt = collections.Counter() cnt[0] = 1 ans = 0 prefix = 0 for n in nums: prefix += n if cnt[prefix-target]!=0: ans += 1 cnt = collections.Counter() cnt[0] = 0 cnt[prefix] += 1 # print(cnt, ans) print('--') return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
from collections import defaultdict class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: # cum_sum=[0] # for i in range(len(nums)): cum_sum.append(cum_sum[-1]+nums[i]) # mem=defaultdict(list) # for i in range(len(nums)+1): # mem[cum_sum[i]].append(i) # def binSearch(index): # arr=mem[cum_sum[index]-target] # right=len(arr)-1 # left=0 # best=None # while left<=right: # mid=(left+right)//2 # if arr[mid]<index: # best=arr[mid] # left=mid+1 # else: # right=mid-1 # return best # dp=[0 for i in range(len(nums)+1)] # for i in range(1,len(nums)+1): # ind=binSearch(i) # s=0 # if ind!=None: # s=1+dp[ind] # dp[i]=max(dp[i-1],s) # return dp[-1] dp=[0 for i in range(len(nums)+1)] mp={} s=0 mp[0]=0 for i in range(1,len(nums)+1): s+=nums[i-1] dp[i]=dp[i-1] if s-target in mp: dp[i]=max(dp[i],1+dp[mp[s-target]]) if nums[i-1]==target: dp[i]=max(dp[i],1+dp[i-1]) mp[s]=i return dp[-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: sett = set([0]) ans = 0 cur = 0 for val in nums: cur += val if cur - target in sett: ans += 1 sett = set() sett.add(cur) return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: prefix = [0]*(len(nums)+1) counts = defaultdict(int) counts[0] = 1 tot = 0 for i in range(1,len(nums)+1): prefix[i] = nums[i-1] + prefix[i-1] if counts[prefix[i]-target] != 0: tot += 1 counts = defaultdict(int) prefix[i] = 0 counts[prefix[i]] += 1 return tot
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: walk = {0:-1} prefixSum = 0 delimiter = -1 res = 0 for i in range(len(nums)): prefixSum += nums[i] remain = prefixSum - target if (remain in walk.keys() and walk[remain] >= delimiter): res += 1 delimiter = i walk[prefixSum] = i return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) d = {0: -1} cursum = 0 ans = 0 for i,n in enumerate(nums): cursum += n prev = cursum - target if prev in d: ans += 1 d = {} d[cursum] = i continue d[cursum] = i return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, arr: List[int], target: int) -> int: prevSums, n = {0: -1}, len(arr) prefixSum, count, lastIndex = 0, 0, -1 for i in range(n): prefixSum += arr[i] rem = prefixSum - target if rem in prevSums and prevSums[rem] >= lastIndex: count += 1 lastIndex = i prevSums[prefixSum] = i return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
# idea: greedy. keep a check of cumulated sum, and as soon as we find a subarray whose sum equals target, we reset cumulated sum class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: cum_sum = 0 seen_cum_sum = set([0]) count = 0 for num in nums: cum_sum += num complement = cum_sum-target # if we found a subarray whose sum equals target, we reset cumulated sum if complement in seen_cum_sum: count += 1 cum_sum = 0 seen_cum_sum = set([0]) seen_cum_sum.add(cum_sum) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: # if nums[0:j] sum larger than target and there is a subarry nums[i:j] sum equal to target # then sum(0:j)-sum(0:i) == target # base on this idea, we could store each prefix_sum in to a map, # check whether sum(0:j)-target is in the prefix_sum map. # since we need overlapping, so store the right_most index for each prefix sum # update the just found subarry's rightmose index, check current end index map = {0:-1} prefix, rightmost, count = 0, -1, 0 for i,val in enumerate(nums): prefix += val if prefix-target in map and map[prefix-target]>=rightmost: # for overlapping count += 1 rightmost = i map[prefix] = i return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: s = {0} res = 0 cur = 0 for x in nums: cur += x if cur - target in s: #print(x) res += 1 cur = 0 s = {0} continue s.add(cur) return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: sums = [0 for num in nums] sum2index = {0:-1} nums_subarrays = [0 for num in nums] for i, num in enumerate(nums): if i == 0: sums[i] = nums[i] else: sums[i] = sums[i-1] + nums[i] if sums[i]-target in sum2index: if i == 0: nums_subarrays[i] = 1 else: nums_subarrays[i] = max(nums_subarrays[i-1], nums_subarrays[sum2index[sums[i]-target]]+1) else: if i == 0: nums_subarrays[i] = 0 else: nums_subarrays[i] = nums_subarrays[i-1] sum2index[sums[i]] = i return nums_subarrays[-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) result = 0 pre_sum = [0] * (n+1) for i,num in enumerate(nums): pre_sum [i+1] = pre_sum[i] + num seen = {0:0} for i in range(1,n+1): cur = pre_sum[i] if cur-target in seen: result += 1 seen = {} seen[cur] = i return result
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: if len(nums) == 0: return 0 result = 0 # target = 10 # [-2,6, 6, 3, 5, 4, 1, 2, 8] # [-2,4,10,13,18,22,23,25,33] # ^ # seen: [0] # result = 2 prefixSums = [] partial = 0 seen = set([0]) for i in range(len(nums)): partial += nums[i] prefixSums.append(partial) required = prefixSums[i] - target if required in seen: result += 1 seen = set() seen.add(prefixSums[i]) return result
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: list, target: int) -> int: sumDict=collections.defaultdict(collections.deque) preSum=0;sumDict[0].append(-1);right=-1;ans=0 for i in range(len(nums)): preSum+=nums[i] if preSum-target in sumDict: while sumDict[preSum-target] and sumDict[preSum-target][0]<right: sumDict[preSum-target].popleft() if sumDict[preSum-target]: ans+=1 right=i sumDict[preSum].append(i) return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) presum = [0] * (n + 1) for i in range(n): presum[i+1] = presum[i] + nums[i] seen = {0: 0} ans = 0 for i in range(1, n + 1): curr = presum[i] prev = curr - target if prev in seen: ans += 1 seen = {} seen[curr] = i return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) dp = [0] * (n+1) m = {0: 0} for i, pre in enumerate(itertools.accumulate(nums)): if pre-target in m: dp[i+1] = max(dp[i], dp[m[pre-target]]+1) else: dp[i+1] = dp[i] m[pre] = i+1 return dp[-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: # n = len(nums) # prefix = [0] * (n + 1) # idx = [] # for i in range(1, n + 1): # prefix[i] = prefix[i - 1] + nums[i - 1] # for i in range(n + 1): # for j in range(i + 1, n + 1): # if prefix[j] - prefix[i] == target: # idx.append([i, j]) # break # self.ans = 0 # def backtrk(cur, start): # if start == len(idx): # self.ans = max(self.ans, len(cur)) # return # visited = set() # for i in range(start, len(idx)): # if len(cur) >= 1 and idx[i][0] < cur[-1][1]: # continue # x, y = idx[i][0], idx[i][1] # backtrk(cur + [(x, y)], i + 1) # backtrk([], 0) # return self.ans prefix = [0] for x in nums: prefix.append(prefix[-1]+x) res = 0 seen = set() for x in prefix: if x-target in seen: res += 1 seen = {x} else: seen.add(x) return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: #maintain a list of end indices hmap = collections.defaultdict(int) rsum = 0 res = 0 last = -1 hmap[0] = -1 for index,num in enumerate(nums): rsum += num csum = rsum - target # if rsum == target: # res += 1 if csum in hmap and hmap[csum] >= last: res += 1 last = index hmap[rsum] = index return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: seen = {0: -1} prev = -1 count = 0 curr = 0 for i in range(len(nums)): curr += nums[i] if nums[i] == target or (curr - target in seen and prev < seen[curr - target] + 1): prev = i count += 1 seen[curr] = i return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: dp = collections.Counter() dp[0] = -1 s = cnt = 0 right = -1 for i, n in enumerate(nums): s += n if s - target in dp: left = dp[s - target] if right <= left: cnt += 1 right = i dp[s] = i return cnt
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, A: List[int], T: int) -> int: N = len(A) prefix = [0 for _ in range(N + 1)] for i in range(1, N + 1): prefix[i] = prefix[i - 1] + A[i - 1] seen = set([0]) res = 0 for i in range(N): curr = prefix[i + 1] want = curr - T if want in seen: res += 1 seen = set() seen.add(curr) return res
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: curSum = 0 sumsSet = {0} result = 0 for x in nums: curSum += x if (curSum - target) in sumsSet: result += 1 curSum = 0 sumsSet = {0} else: sumsSet.add(curSum) return result
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, A: List[int], target: int) -> int: # dp[i + 1] = Math.max(dp[i + 1], dp[j] + (sum == target ? 1 : 0)); # a = [0] * (len(A) + 1) # for i in range (len(A)): # sums = 0 # for j in reversed(range(i + 1)): # sums += A[j] # a[i + 1] = max(a[i + 1], a[j] + 1 if sums == target else 0) # print(a) # return a[len(A)] seen = {0} prefix_sum = count = 0 for a in A: prefix_sum += a if -target + prefix_sum in seen: prefix_sum = 0 count += 1 seen = set() seen.add(prefix_sum) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
''' sliding window approach initalize left at 0, curr at 0 enumerate through right ''' class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) presum = [0] * (n + 1) for i in range(n): presum[i+1] = presum[i] + nums[i] seen = {0: 0} ans = 0 for i in range(1, n + 1): curr = presum[i] prev = curr - target if prev in seen: ans += 1 seen = {} seen[curr] = i return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: if len(nums) == 0: return 0 result = 0 # target = 10 # [-2,6, 6, 3, 5, 4, 1, 2, 8] # [-2,4,10,13,18,22,23,25,33] # ^ # seen: [0] # result = 2 prefixSums = [] partial = 0 for i in range(len(nums)): partial += nums[i] prefixSums.append(partial) seen = set([0]) for i in range(len(nums)): required = prefixSums[i] - target if required in seen: result += 1 seen = set() seen.add(prefixSums[i]) return result
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: store=set([0]) s=0 count=0 for i in nums: s+=i if s-target in store: count+=1 store.clear() store.add(s) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: existingSums = set([0]) prefixsum = 0 count = 0 for num in nums: prefixsum += num if prefixsum - target in existingSums: count += 1 existingSums = set([0]) prefixsum = 0 existingSums.add(prefixsum) return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, lis: List[int], tar: int) -> int: n = len(lis) pre=[0]*(n+1) ans=0 for i in range(n): pre[i+1]=pre[i]+lis[i] d={0:0} for i in range(1,n+1): cur = pre[i] prev = cur-tar if prev in d: ans+=1 d={} d[cur]=1 return ans
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: sums = [0] # prefix sum for num in nums: sums.append(sums[-1] + num) # two sum hashmap = {} results = [] for i, s in enumerate(sums): if s in hashmap: start = hashmap[s] results.append((start, i-1)) need = s + target hashmap[need] = i prevEnd = -1 count = 0 for start, end in results: if start > prevEnd: count += 1 prevEnd = end return count
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: seen = {0: -1} dp = [0]*(len(nums)) ans = 0 for i in range(len(nums)): if(i > 0): nums[i] += nums[i-1] dp[i] = dp[i-1] if((nums[i]-target) in seen): dp[i]=max(dp[i], dp[seen[nums[i]-target]]+1) seen[nums[i]] = i return dp[-1]
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
from collections import defaultdict class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: dp = defaultdict(list) res = [(0,-1)] tmp_sum = 0 for i, val in enumerate(nums): tmp_sum += val if tmp_sum - target == 0 and i > res[-1][1]: res.append((0,i)) tmp_sum = 0 else: if tmp_sum - target in dp and dp[tmp_sum - target][-1] >= res[-1][1] : res.append((0,i)) tmp_sum = 0 dp[tmp_sum].append(i) # print(dp) return len(res)-1
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: res, sm = 0, 0 hs = set() hs.add(0) for v in nums: sm += v if (sm-target) in hs: res += 1 sm = 0 hs.clear() hs.add(0) else: hs.add(sm) return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = [0]*len(s) unique = set() n_distinct = 0 for i, l in enumerate(s): if l not in unique: unique.add(l) n_distinct += 1 left[i] = n_distinct count = 0 unique = set() n_distinct = 0 for i in range(len(s)-1, 0,-1): if s[i] not in unique: unique.add(s[i]) n_distinct += 1 if n_distinct == left[i-1]: count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: index_one = 0 index_two = 0 set_one = set() set_two = set() s_r = s[::-1] for i in range(len(s)): # if s[i] in set_one: # continue # else: print((i, len(set(s[:i + 1])), len(set(s[i + 1:])))) if len(set(s[:i + 1])) == len(set(s[i + 1:])): index_one = i break else: set_one.add(s[i]) for i in range(len(s_r)): # if s_r[i] in set_two: # continue # else: print((i, len(set(s_r[:i + 1])), len(set(s_r[i + 1:])))) if len(set(s_r[:i + 1])) == len(set(s_r[i + 1:])): print(i) index_two = len(s) - i - 1 break else: set_two.add(s_r[i]) print((index_one, index_two)) return index_two - index_one
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: right = Counter(s) left = set() res = 0 for i in range(len(s) - 1): left.add(s[i]) right[s[i]] -= 1 res += len(left) == sum(i > 0 for i in right.values()) return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: def decorate(a, b): final = {1: defaultdict(int), 2 : defaultdict(int)} count1, count2 = 0, 0 for char in a: final[1][char] += 1 if final[1][char] == 1: count1 += 1 for char in b: final[2][char] += 1 if final[2][char] == 1: count2 += 1 return final, count1, count2 split = None count1, count2 = 0, 0 total = 0 for point in range(1, len(s)): if split == None: split, count1, count2 = decorate(s[:point], s[point:]) else: split[1][s[point-1]] += 1 if split[1][s[point-1]] == 1: count1 += 1 split[2][s[point-1]] -= 1 if split[2][s[point-1]] == 0: count2 -= 1 # print(s[:point], s[point:], count1, count2) if count1 == count2: total += 1 return total
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def count_chars(self, s): count = {} for c in s: if c in count: count[c] += 1 else: count[c] = 1 return count def dict_decr(self, d, c): if c in d: d[c] -= 1 if d[c] <= 0: del d[c] def numSplits(self, s: str) -> int: left, right = s[0], s[1:] left_chars = set([left]) right_chars = self.count_chars(right) num_splits = 0 while right: if len(left_chars) == len(right_chars.keys()): num_splits += 1 c = right[0] self.dict_decr(right_chars, c) left_chars.add(c) left += c right = right[1:] return num_splits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: count = 0 left = set(s[:1]) right = set(s[1:]) if len(left) == len(right): count += 1 k = 0 for i in range(1, len(s)): if s[i] not in left: left.add(s[i]) if s[i] not in s[i+1:]: k += 1 diff = len(left) - (len(right) - k) if diff == 0: count += 1 elif diff > 0: break return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: no = 0 x=Counter(s) y=Counter() if len(list(x.keys()))==len(list(y.keys())): no+=1 for i in range(0,len(s)-1): x[s[i]]-=1 if x[s[i]]==0: del x[s[i]] if s[i] in y: y[s[i]] += 1 else: y[s[i]]=1 if len(list(x.keys())) == len(list(y.keys())): no+=1 return no
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: num_splits = 0 left = Counter() right = Counter(s) for char in s: left.update(char) right.subtract(char) if right[char] == 0: del right[char] if len(left) == len(right): num_splits += 1 return num_splits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: pre = [0] * len(s) acc = set() cnt = 0 for i in range(len(s) - 1): if s[i] not in acc: cnt += 1 acc.add(s[i]) pre[i] = cnt cnt = 0 acc.clear() res = 0 for i in range(len(s) - 1, 0, -1): if s[i] not in acc: cnt += 1 acc.add(s[i]) if pre[i - 1] == cnt: res += 1 return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: l_count = [] r_count = [] tmp = 0 for i in range(len(s)): if s[i] not in s[:i]: tmp += 1 l_count.append(tmp) tmp = 0 for i in range(len(s))[::-1]: if s[i] not in s[(i+1):]: tmp += 1 r_count.append(tmp) r_count = r_count[::-1] count = 0 for i in range(len(r_count)-1): if l_count[i] == r_count[i+1]: count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
# class Solution: # def numSplits(self, s: str) -> int: # n = len(s) # output = 0 # for i in range(1, n): # if len(set(s[:i])) == len(set(s[i:n])): # output += 1 # if len(set(s[:i])) > len(set(s[i:n])): # break # return output class Solution: def numSplits(self, s: str) -> int: n = len(s) output = 0 dic = {} for i, x in enumerate(s): if not x in dic: dic[x] = [i, i] else: dic[x][1] = i # print(dic) for i in range(0, n-1): left = 0 right = 0 for letter, indexes in list(dic.items()): if i >= indexes[0]: left += 1 if i + 1 <= indexes[1]: right += 1 if left == right: output += 1 return output
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: prev_counter = {} post_counter = dict(Counter(s)) count = 0 for partition in range(len(s)): prev, post = s[:partition], s[partition:] if not prev or not post: continue curr = prev[-1] if curr not in prev_counter: prev_counter[curr] = 0 prev_counter[curr] += 1 post_counter[curr] -= 1 if post_counter[curr] == 0: post_counter.pop(curr) if len(prev_counter) == len(post_counter): count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: prev_counter = {} post_counter = Counter(s) count = 0 for partition in range(len(s)): prev, post = s[:partition], s[partition:] if not prev or not post: continue curr = prev[-1] if curr not in prev_counter: prev_counter[curr] = 0 prev_counter[curr] += 1 post_counter[curr] -= 1 if post_counter[curr] == 0: post_counter.pop(curr) if len(prev_counter) == len(post_counter): count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def sub(self, h, h2): nh = h.copy() for k in h2: if k in nh: nh[k] = nh[k]-h2[k] if nh[k] == 0: del nh[k] return nh def numSplits(self, s: str) -> int: h = {} for c in s: if c not in h: h[c] = 0 h[c] = h[c] + 1 ct = 0 h2 = {} for i, c in enumerate(s): if c not in h2: h2[c] = 0 h2[c] = h2[c] + 1 uh = self.sub(h, h2) if len(h2) == len(uh): ct = ct + 1 return ct
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import defaultdict, Counter class Solution: def numSplits(self, s: str) -> int: len_s = len(s) if len_s == 0: return 0 if len_s == 1: return 1 nb_good_splits = 0 left_counter = defaultdict(int) right_counter = Counter(s) for i in range(1, len_s): left, right = s[0:i], s[i:] swing_letter = left[-1] left_counter[swing_letter] += 1 right_counter[swing_letter] -= 1 if right_counter[swing_letter] == 0: del right_counter[swing_letter] if len(left_counter) == len(right_counter): nb_good_splits += 1 return nb_good_splits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: b_c = collections.defaultdict(lambda:0) a_c = dict(collections.Counter(list(s))) # print('initial', b_c, a_c) ans = 0 for i, ch in enumerate(list(s)): # print(i, ch) b_c[ch] += 1 a_c[ch] -= 1 # print(b_c, a_c) if len({k for k, v in list(b_c.items()) if v > 0}) == len({k for k,v in list(a_c.items()) if v>0}): ans += 1 return ans
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: good_splits = 0 first_map = {} s_map = {} for letter in s: if s_map.get(letter): s_map[letter] += 1 else: s_map[letter] = 1 for split_point in range(len(s)): if first_map.get(s[split_point]): first_map[s[split_point]] = first_map[s[split_point]] + 1 else: first_map[s[split_point]] = 1 s_map[s[split_point]] = s_map[s[split_point]] - 1 f_keys = [k for k,v in list(first_map.items()) if v > 0] s_keys = [k for k,v in list(s_map.items()) if v > 0] if len(f_keys) == len(s_keys): good_splits += 1 return good_splits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if len(s) <= 1: return 0 left = len(s)*[0] right = len(s)*[0] seen = set(s[0]) # right[-1] = 1 left [0] = 1 for i in range(1, len(s)): if s[i] not in seen: seen.add(s[i]) left[i] = left[i-1]+1 else: left[i] = left[i-1] seen= set() rtotal = 0 count = 0 for i in range(len(s)-1, 0,-1): if s[i] not in seen: seen.add(s[i]) rtotal +=1 if rtotal == left[i-1]: count +=1 elif rtotal > left[i-1]: break # count = 0 # # print(left) # # print(right) # for i in range(0, len(s)-1): # if left[i]== right[i+1]: # count +=1 # elif right[i+1] < left[i]: # break return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: characters = [] possibleSplits = 0 for i in range(len(s)): if s[i] not in characters: characters.append(s[i]) for i in range(len(s)): leftChars = 0 rightChars = 0 leftSide = s[:i] rightSide = s[i:] for j in range(len(characters)): if characters[j] in leftSide: leftChars += 1 if characters[j] in rightSide: rightChars += 1 if leftChars == rightChars: possibleSplits += 1 return possibleSplits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: c = 0 c1 = Counter() c2 = Counter(s) for i in range(len(s)): c1[s[i]] += 1 c2[s[i]] -= 1 if self.Count(c1) == self.Count(c2): c += 1 return c def Count(self, counter): return sum([1 for k in counter if counter[k] !=0])
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: s_len = len(s) if s_len == 0 or s_len == 1: return 0 seen = set() dp = [0 for _ in range(s_len-1)] dp[0] = 1 seen.add(s[0]) for i in range(1,s_len-1): if s[i] in seen: dp[i] = dp[i-1] else: seen.add(s[i]) dp[i] = dp[i-1]+1 seen = set() acc = 0 count = 0 for i in range(s_len-1,0,-1): if (s[i] not in seen): seen.add(s[i]) acc += 1 if (acc == dp[i-1]): count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits111(self, s: str) -> int: s_len = len(s) if s_len == 0 or s_len == 1: return 0 seen = set() dp = [0 for i in range(s_len-1)] dp[0] = 1 seen.add(s[0]) for i in range(1,s_len-1): if s[i] in seen: dp[i] == dp[i-1] else: seen.add(s[i]) dp[i] = dp[i-1]+1 seen = set() acc = 0 count = 0 for i in range(s_len-1,0,-1): if (s[i] not in seen): seen.add(s[i]) acc += 1 if (acc == dp[i-1]): count += 1 return count def numSplits(self, s: str) -> int: n = len(s) if n <= 1: return 0 #forward pass seen = set() dp = [0 for _ in range(n-1)] dp[0] = 1 seen.add(s[0]) for i in range(1,n-1): if s[i] in seen: dp[i] = dp[i-1] else: seen.add(s[i]) dp[i] = dp[i-1] + 1 count = 0 #backward pass seen = set() current = 0 for i in range(n-1, 0, -1): if s[i] not in seen: seen.add(s[i]) current += 1 if current == dp[i-1]: count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: cnt = 0 l_set = set() d = {c: s.count(c) for c in set(s)} for c in s: l_set.add(c) d[c] -= 1 if d[c] == 0: d.pop(c, None) if len(l_set) == len(d): cnt += 1 return cnt
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: dictionary={} sol=0 for i in range(len(s)): dictionary[s[i]]=i dictionary2={} for i in range(len(s)-1): dictionary2[s[i]]=i if i==dictionary[s[i]]: dictionary.pop(s[i]) if len(dictionary)==len(dictionary2): sol+=1 return sol
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: uniqueLeft = 0 uniqueRight = 0 ans = 0 leftDict = {} rightDict = {} for i in s: if i in rightDict: rightDict[i] += 1 else: rightDict[i] = 1 uniqueRight += 1 for i in s: if i in leftDict: leftDict[i] += 1 else: leftDict[i] = 1 uniqueLeft += 1 rightDict[i] -= 1 if rightDict[i] == 0: uniqueRight -= 1 if uniqueRight == uniqueLeft: ans += 1 return ans
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
# time complexity: O(26N), N = len(s) #class Solution: # def numSplits(self, s: str) -> int: # N = len(s) # freq = [[0] * (N+1) for _ in range(26)] # cumulative freq of each character # for i in range(1, N+1): # for j in range(26): # if ord(s[i-1]) - ord('a') == j: # freq[j][i] = freq[j][i-1] + 1 # else: # freq[j][i] = freq[j][i-1] # # good = 0 # for i in range(1, N): # left = 0 # right = 0 # for j in range(26): # if freq[j][i] > 0: # there is at least one chr(ord(j) + ord('a')) on the left of s[i] # left += 1 # if freq[j][i] < freq[j][-1]: # there is at least one chr(ord(j) + ord('a')) on the right of s[i] # right += 1 # if left == right: # good += 1 # return good # time complexity: O(3N), N = len(s) class Solution: def numSplits(self, s: str) -> int: N = len(s) left = [0] * (N+1) # unique char in s[:left], exclude left seen = set() for i in range(1,N+1): c = s[i-1] if c not in seen: left[i] = left[i-1] + 1 seen.add(c) else: left[i] = left[i-1] right = [0] * (N+1) # unique char in s[right:], include right seen.clear() for i in range(N-1,-1,-1): c = s[i] if c not in seen: right[i] = right[i+1] + 1 seen.add(c) else: right[i] = right[i+1] good = 0 for i in range(N+1): if left[i] == right[i]: good += 1 return good
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: set_f = set() map_b = defaultdict(int) for c in s: map_b[c] += 1 num = 0 for c in s: map_b[c] -= 1 if not map_b[c]: del map_b[c] set_f.add(c) if len(set_f) == len(map_b): num += 1 return num
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import defaultdict class Solution: def numSplits(self, s: str) -> int: rst = 0 lDict = defaultdict(int) rDict = defaultdict(int) lDict[s[0]] = 1 for char in s[1:]: rDict[char]+=1 if(len(rDict)==len(lDict)): rst+=1 for char in s[1:-1]: rDict[char]-=1 if(rDict[char]==0): rDict.pop(char) lDict[char]+=1 #print(lDict,rDict) if(len(rDict)==len(lDict)): rst+=1 return rst
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
import collections class Solution: def numSplits(self, s: str) -> int: leftMap = collections.defaultdict(int) rightMap = collections.defaultdict(int) for ch in s: rightMap[ch] += 1 output = 0 for ch in s: rightMap[ch] -= 1 if rightMap[ch] == 0: del rightMap[ch] leftMap[ch] += 1 if len(rightMap) == len(leftMap): output += 1 return output
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: initial = {} for char in s: if char in initial: initial[char] += 1 else: initial[char] = 1 other = {} output = 0 for char in s: initial[char] -= 1 if initial[char] == 0: del initial[char] other[char] = 1 if len(initial) == len(other): output += 1 return output
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import defaultdict class Solution: def numSplits(self, s: str) -> int: hmap = defaultdict(int) for ele in s: hmap[ele]+=1 count = 0 size = len(hmap) second_map = defaultdict(int) for i in range(len(s)): c = s[i] second_map[c]+=1 hmap[c]-=1 if hmap[c] == 0: del hmap[c] if len(second_map) == len(hmap): count+=1 elif len(second_map) > len(hmap): break return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: n = len(s) lc = [1]*n rc = [1]*n ls = set() rs = set() for i in range(n): ls.add(s[i]) rs.add(s[-(i+1)]) lc[i] = len(ls) rc[-(i+1)] = len(rs) r = 0 for i in range(n-1): if lc[i]==rc[i+1]: r+=1 return r
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: lc, rc = [], [] ls, rs = set(), set() for c in s: ls.add(c) lc.append(len(ls)) for c in reversed(s): rs.add(c) rc.append(len(rs)) res = 0 rc = rc[::-1] for i in range(len(lc) - 1): if lc[i] == rc[i + 1]: res += 1 return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: auxDict = {} auxLeftToRight = [0] * len(s) count = 0 for pos in range(len(s)): if s[pos] not in auxDict: auxDict[s[pos]] = 1 count += 1 auxLeftToRight[pos] = count auxDict = {} auxRightToLeft = [0] * len(s) count = 0 for pos in range(len(s)-1): if s[len(s)-1-pos] not in auxDict: auxDict[s[len(s)-1-pos]] = 1 count += 1 auxRightToLeft[len(s)-2-pos] = count total = 0 for pos in range(len(s)): if auxLeftToRight[pos] == auxRightToLeft[pos]: total += 1 return total
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: n=len(s) prefix=[0]*n suffix=[0]*n pset=set() sset=set() ans=0 for i in range(0,n): l=s[i] r=s[n-i-1] pset.add(l) sset.add(r) prefix[i]=len(pset) suffix[n-1-i]=len(sset) for i in range(0,n-1): if prefix[i]==suffix[i+1]: ans=ans+1 return ans
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: # preprocess_prefix[k], number of different letters in s[:k] # preprocess_postfix[k], number of different letters in s[k:] # Fill preprocess_prefix seen_letters = [] preprocess_prefix = [0] for i in range(len(s)): if s[i] not in seen_letters: seen_letters.append(s[i]) preprocess_prefix.append(len(seen_letters)) # Fill preprocess_postfix[k] seen_letters = [] preprocess_postfix = [0] for i in range(len(s)-1, -1, -1): if s[i] not in seen_letters: seen_letters.append(s[i]) preprocess_postfix.append(len(seen_letters)) preprocess_postfix.reverse() count = 0 for i in range(len(preprocess_postfix)): if preprocess_postfix[i] == preprocess_prefix[i]: count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: count = 0 left, right = defaultdict(int), Counter(s) count = 0 for letter in s: left[letter] += 1 right[letter] -= 1 if right[letter] == 0: del right[letter] count += (len(left) == len(right)) return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: le_set, ri_set = [s[0]], [s[-1]] le_arr, ri_arr = [1]*len(s), [1]*len(s) # print(le_arr[0]) # print(ri_arr) for i in range(1, len(s)): le_arr[i] = le_arr[i-1] if s[i] not in le_set: le_set.append(s[i]) le_arr[i]+=1 for i in range(len(s)-2, -1, -1): ri_arr[i] = ri_arr[i+1] if s[i] not in ri_set: ri_set.append(s[i]) ri_arr[i]+=1 # print(le_arr) # print(ri_arr) cnt=0 for i in range(0, len(s)-1): if le_arr[i]==ri_arr[i+1]: cnt+=1 return cnt
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: total_chars_1 = defaultdict(int) total_chars_1[s[0]] = 1 total_chars_2 = defaultdict(int) splits = 0 for i in range(1, len(s)): total_chars_2[s[i]] += 1 for i in range(len(s) - 1): if len(total_chars_1) == len(total_chars_2): splits += 1 total_chars_1[s[i+1]] += 1 total_chars_2[s[i+1]] -= 1 if total_chars_2[s[i+1]] == 0: del total_chars_2[s[i+1]] return splits
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: c = Counter(s) res = 0 d = dict() for i,v in enumerate(s): if v not in d: d[v] = 1 else: d[v] += 1 c[v] -= 1 if c[v] == 0: del c[v] if len(c) == len(d): res += 1 return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: counter = 0 left = defaultdict(int) right = defaultdict(int) for c in s: right[c] += 1 # pre-populate\\ for i in range(len(s)): left[s[i]] += 1 right[s[i]] -= 1 if right[s[i]] == 0: right.pop(s[i]) if len(left.keys()) == len(right.keys()): counter += 1 return counter
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left_count = collections.Counter() right_count = collections.Counter(s) ans = 0 for ch in s: left_count[ch] += 1 right_count[ch] -= 1 if right_count[ch] == 0: del right_count[ch] if len(left_count) == len(right_count): ans += 1 return ans
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: leftCount = collections.Counter() rightCount = collections.Counter(s) res = 0 for c in s: leftCount[c] += 1 rightCount[c] -= 1 if rightCount[c] == 0: del rightCount[c] if len(rightCount) == len(leftCount): res += 1 return res
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left=sorted([s.find(l) for l in set(s)]) + [len(s)] right=sorted([s.rfind(l) for l in set(s)], reverse=True)+[0] ind=max(ind for ind in range(len(left)) if left[ind]<=right[ind]) return min(right[ind], left[ind+1])-max(left[ind], right[ind+1]) ''' \"aacaba\" \"abcd\" \"aaaaa\" \"acbadbaada\" \"a\" \"abc\" \"abcd\" '''
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: s_cnt = Counter(s) p_cnt = defaultdict(int) count = 0 for ch in s: p_cnt[ch] += 1 s_cnt[ch] -= 1 if s_cnt[ch] == 0: del(s_cnt[ch]) if len(s_cnt.keys()) == len(p_cnt.keys()): count += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: #pseudo: #start left window at 0 #have right window open for all letters in s #keep count for left and right #keep overall count #left woud start with first letter, right would be rest of letters #loop through each letter #left window should increase by one [i+1] #right window should decrease by 1 #if the letter in the right dictionary already exists, then remove and decrease the right count by 1 #if current letter not in the left dictionary then +1 to left count and add to dictionary #if left count and right counter are both equal to each other then add 1 to count l_dict = Counter() r_dict = Counter(s) counter = 0 print(r_dict) for i in s: r_dict[i] -= 1 l_dict[i] = l_dict.get(i, 0) + 1 if r_dict[i] == 0: del r_dict[i] if len(l_dict) == len(r_dict): counter += 1 return counter
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplitsBF(self, s: str) -> int: cntr = 0 for i in range(1, len(s)): # a, b = s[:i], s[i:] # print(a, b) a, b = collections.Counter(s[:i]), collections.Counter(s[i:]) if len(a) == len(b): cntr += 1 return cntr def numSplits(self, s: str) -> int: a = collections.defaultdict(int) a[s[0]] = 1 b = collections.Counter(s[1:]) pntr = 1 cntr = 0 if len(a) != len(b) else 1 while pntr < len(s): a[s[pntr]] += 1 b[s[pntr]] -= 1 if b[s[pntr]] == 0: del b[s[pntr]] # print(a, b) if len(a) == len(b): cntr += 1 pntr += 1 return cntr
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = {s[0]:1} right = {} for i in range(1, len(s)): right[s[i]] = right.get(s[i], 0) + 1 #check if the left set has as many items as the right set #then we incremenet the middle pointer #remove the item from right #add it to left middle_i = 1 count = 0 while middle_i < len(s): if len(left) == len(right): count += 1 middle = s[middle_i] right[middle] -= 1 if right[middle] == 0: right.pop(middle) left[middle] = left.get(middle, 0) + 1 middle_i += 1 return count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
from collections import Counter class Solution: def numSplits(self, s: str) -> int: left_counts = {s[0]:1} right_counts = Counter(s[1:]) split_count = 0 if len(list(left_counts.items())) == len(list(right_counts.items())): split_count += 1 for i in range(1, len(s)): if s[i] in left_counts: left_counts[s[i]] += 1 else: left_counts[s[i]] = 1 if right_counts[s[i]] == 1: del right_counts[s[i]] else: right_counts[s[i]] -= 1 if len(list(left_counts.items())) == len(list(right_counts.items())): split_count += 1 return split_count
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, ss: str) -> int: a=set() k={} p={} n=len(ss) def myfunc(s,aa): for i in range(n-1): if(s[i] not in a): a.add(s[i]) aa[i]=1 else: aa[i]=0 if(i!=0): aa[i]+=aa[i-1] myfunc(ss,k) # k[i] gives,no of distinct letters,to left of left of index i+1 in ss a.clear() myfunc(ss[::-1],p) # p[i] gives,no of distinct letters,to left of left of index i+1...in ss_reverse ans=0 # print(k) # print(p) for j in range(n-1): if(k[j]==p[n-1-(j+1)]): ans+=1 return ans
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: leftMap = {} rightMap = {} for j in range(len(s)): rightMap[s[j]] = rightMap.get(s[j], 0) + 1 i = 0 result = 0 while (i < len(s) - 1): leftMap[s[i]] = leftMap.get(s[i], 0) + 1 rightMap[s[i]] = rightMap[s[i]] - 1 if (rightMap.get(s[i], 0) <= 0): rightMap.pop(s[i]) if (len(leftMap) == len(rightMap)): result += 1 i += 1 return result # method 2 # prefix = [0] * len(s) # distinct number from s[0] to s[i] # suffix = [0] * len(s) # distinct number from s[len(s)-1] to s[i] # prefixMap = {} # suffixMap = {} # for i in range(len(s)): # j = len(s) - 1 - i # prefixMap[s[i]] = prefixMap.get(s[i], 0) + 1 # suffixMap[s[j]] = suffixMap.get(s[j], 0) + 1 # prefix[i] = len(prefixMap) # suffix[j] = len(suffixMap) # result = 0 # k = 0 # while (k < len(s)-1): # if (prefix[k] == suffix[k+1]): # result += 1 # k += 1 # return result
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: leftMap = {} rightMap = {} for j in range(len(s)): rightMap[s[j]] = rightMap.get(s[j], 0) + 1 i = 0 result = 0 while (i < len(s) - 1): leftMap[s[i]] = leftMap.get(s[i], 0) + 1 rightMap[s[i]] = rightMap[s[i]] - 1 if (rightMap.get(s[i], 0) <= 0): rightMap.pop(s[i]) if (len(leftMap) == len(rightMap)): result += 1 i += 1 # print(\"leftMap\", leftMap) # print(\"rightMap\", rightMap) return result # method 2 # prefix = [0] * len(s) # distinct number from s[0] to s[i] # suffix = [0] * len(s) # distinct number from s[len(s)-1] to s[i] # prefixMap = {} # suffixMap = {} # for i in range(len(s)): # j = len(s) - 1 - i # prefixMap[s[i]] = prefixMap.get(s[i], 0) + 1 # suffixMap[s[j]] = suffixMap.get(s[j], 0) + 1 # prefix[i] = len(prefixMap) # suffix[j] = len(suffixMap) # result = 0 # k = 0 # while (k < len(s)-1): # if (prefix[k] == suffix[k+1]): # result += 1 # k += 1 # return result
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left = Counter() right = Counter(s) res = 0 for ch in s : if right[ch] == 1: del right[ch] else: right[ch] -= 1 left[ch] += 1 #print (right,left) if len(list(left.keys())) == len(list(right.keys())): res += 1 return res