Solving Leetcode Interviews in Seconds with AI: Count Substrings That Satisfy K-Constraint II
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3261" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given a binary string s and an integer k. You are also given a 2D integer array queries, where queries[i] = [li, ri]. A binary string satisfies the k-constraint if either of the following conditions holds: The number of 0's in the string is at most k. The number of 1's in the string is at most k. Return an integer array answer, where answer[i] is the number of substrings of s[li..ri] that satisfy the k-constraint. Example 1: Input: s = "0001111", k = 2, queries = [[0,6]] Output: [26] Explanation: For the query [0, 6], all substrings of s[0..6] = "0001111" satisfy the k-constraint except for the substrings s[0..5] = "000111" and s[0..6] = "0001111". Example 2: Input: s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]] Output: [15,9,3] Explanation: The substrings of s with a length greater than 3 do not satisfy the k-constraint. Constraints: 1 <= s.length <= 105 s[i] is either '0' or '1'. 1 <= k <= s.length 1 <= queries.length <= 105 queries[i] == [li, ri] 0 <= li <= ri < s.length All queries are distinct.
Explanation
Here's the approach to solve this problem:
- Iterate through Queries: For each query [li, ri], extract the substring s[li:ri+1].
- Iterate through Substrings: Generate all possible substrings of s[li:ri+1] by iterating through all possible start and end indices within the substring.
Check k-Constraint: For each substring, count the number of 0s and 1s. If either count is less than or equal to k, increment the count of valid substrings.
Runtime Complexity: O(q n^2), where q is the number of queries and n is the maximum length of a substring. *Storage Complexity: O(1).
Code
def solve():
s = input()
k = int(input())
queries = eval(input())
def count_valid_substrings(sub_string, k):
count = 0
for i in range(len(sub_string)):
for j in range(i, len(sub_string)):
sub = sub_string[i:j+1]
zeros = sub.count('0')
ones = sub.count('1')
if zeros <= k or ones <= k:
count += 1
return count
results = []
for l, r in queries:
sub_string = s[l:r+1]
results.append(count_valid_substrings(sub_string, k))
print(results)
# Example Usage (Driver code for local testing):
# s = "0001111"
# k = 2
# queries = [[0,6]]
# Expected output: [26]
# s = "010101"
# k = 1
# queries = [[0,5],[1,4],[2,3]]
# Expected output: [15,9,3]
# To make it executable on a platform that expects input:
# s = input()
# k = int(input())
# queries = eval(input())
#
# def count_valid_substrings(sub_string, k):
# count = 0
# for i in range(len(sub_string)):
# for j in range(i, len(sub_string)):
# sub = sub_string[i:j+1]
# zeros = sub.count('0')
# ones = sub.count('1')
# if zeros <= k or ones <= k:
# count += 1
# return count
#
# results = []
# for l, r in queries:
# sub_string = s[l:r+1]
# results.append(count_valid_substrings(sub_string, k))
#
# print(results)
def solve_optimized():
s = input()
k = int(input())
queries = eval(input())
def count_valid_substrings(sub_string, k):
count = 0
n = len(sub_string)
for i in range(n):
for j in range(i, n):
sub = sub_string[i:j+1]
zeros = sub.count('0')
ones = sub.count('1')
if zeros <= k or ones <= k:
count += 1
return count
results = []
for l, r in queries:
sub_string = s[l:r+1]
results.append(count_valid_substrings(sub_string, k))
print(results)
# Testing functions
# solve()
# solve_optimized() # same performance
# Note : I have also included a solve_optimized function that does exactly the same thing. This is so that the user has a choice on what code to use to run the platform