CPP Solution: class Solution { public: int countGoodStrings(int low, int high, int zero, int one) { const int mod = 1000000007; // Modulo to prevent overflow vector ways(high + 1, 0); // Array to store ways to construct strings of each length ways[0] = 1; // Base case: 1 way to construct an empty string // Calculate ways for each length up to 'high' for (int i = 1; i = 0) { ways[i] = (ways[i] + ways[i - zero]) % mod; // Add ways by appending 'zero' } if (i - one >= 0) { ways[i] = (ways[i] + ways[i - one]) % mod; // Add ways by appending 'one' } } int result = 0; // Sum up the valid ways for lengths between 'low' and 'high' for (int i = low; i
@indiccoder28 күн бұрын
Java Solution: class Solution { public int countGoodStrings(int low, int high, int zero, int one) { int mod = 1000000007; // Modulo to prevent overflow int[] ways = new int[high + 1]; // Array to store ways to construct strings of each length ways[0] = 1; // Base case: 1 way to construct an empty string // Calculate ways for each length up to 'high' for (int i = 1; i = 0) { ways[i] = (ways[i] + ways[i - zero]) % mod; // Add ways by appending 'zero' } if (i - one >= 0) { ways[i] = (ways[i] + ways[i - one]) % mod; // Add ways by appending 'one' } } int result = 0; // Sum up the valid ways for lengths between 'low' and 'high' for (int i = low; i
@indiccoder28 күн бұрын
Python Solution: class Solution: def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: mod = 10**9+7 # Modulo to prevent overflow and keep the result within limits ways = [0] * (high + 1) # Array to store the number of ways to build strings of each length ways[0] = 1 # Base case: 1 way to construct an empty string # Loop through each length from 1 to 'high' and calculate the number of ways to construct it for i in range(1, high + 1): if i - zero >= 0: # If the current length is greater than or equal to 'zero' ways[i] = (ways[i] + ways[i - zero]) % mod # Add the ways of the previous length (i - zero) if i - one >= 0: # If the current length is greater than or equal to 'one' ways[i] = (ways[i] + ways[i - one]) % mod # Add the ways of the previous length (i - one) # Return the sum of valid ways for lengths between 'low' and 'high' (inclusive) return sum(ways[low:high + 1]) % mod