Count Ways To Build Good Strings: LeetCode POTD Tamil தமிழ்

  Рет қаралды 69

Indic Coder

Indic Coder

Күн бұрын

Пікірлер: 3
@indiccoder
@indiccoder 28 күн бұрын
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
@indiccoder
@indiccoder 28 күн бұрын
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
@indiccoder
@indiccoder 28 күн бұрын
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
5 Simple Steps for Solving Dynamic Programming Problems
21:27
Reducible
Рет қаралды 1,1 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
How To Speak Fluently In English About Almost Anything
1:49:55
EnglishAnyone
Рет қаралды 3,3 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН