text
stringlengths
1
636
code
stringlengths
8
1.89k
Given matrix mat [ ] [ ]
mat = [ [ 1 , 2 , 3 , 1 ] , [ 3 , 1 , 2 , 4 ] , [ 2 , 3 , 1 , 1 ] ] NEW_LINE
Function Call
countUniquePaths ( mat , M , N , 1 ) NEW_LINE
Print the final count
print ( countPaths ) NEW_LINE
Python3 program for the above approach
import sys NEW_LINE MAX = 100000 NEW_LINE
dp array to memoize the results
dp = [ - 1 ] * ( MAX + 1 ) NEW_LINE
List to store the result
denomination = [ ] NEW_LINE
Function to find the minimum number of coins to make the sum equals to X
def countMinCoins ( n , C , m ) : NEW_LINE
Base case
if ( n == 0 ) : NEW_LINE INDENT dp [ 0 ] = 0 NEW_LINE return 0 NEW_LINE DEDENT
If previously computed subproblem occurred
if ( dp [ n ] != - 1 ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT
Initialize result
ret = sys . maxsize NEW_LINE
Try every coin that has smaller value than n
for i in range ( m ) : NEW_LINE INDENT if ( C [ i ] <= n ) : NEW_LINE INDENT x = countMinCoins ( n - C [ i ] , C , m ) NEW_LINE DEDENT DEDENT
Check for INT_MAX to avoid overflow and see if result . an be minimized
if ( x != sys . maxsize ) : NEW_LINE INDENT ret = min ( ret , 1 + x ) NEW_LINE DEDENT
Memoizing value of current state
dp [ n ] = ret NEW_LINE return ret NEW_LINE
Function to find the possible combination of coins to make the sum equal to X
def findSolution ( n , C , m ) : NEW_LINE
Base Case
if ( n == 0 ) : NEW_LINE
Print Solutions
for it in denomination : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE for i in range ( m ) : NEW_LINE
Try every coin that has value smaller than n
if ( n - C [ i ] >= 0 and dp [ n - C [ i ] ] + 1 == dp [ n ] ) : NEW_LINE
Add current denominations
denomination . append ( C [ i ] ) NEW_LINE
Backtrack
findSolution ( n - C [ i ] , C , m ) NEW_LINE break NEW_LINE
Function to find the minimum combinations of coins for value X
def countMinCoinsUtil ( X , C , N ) : NEW_LINE
Min coins
isPossible = countMinCoins ( X , C , N ) NEW_LINE
If no solution exists
if ( isPossible == sys . maxsize ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT
Backtrack to find the solution
else : NEW_LINE INDENT findSolution ( X , C , N ) NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 21 NEW_LINE DEDENT
Set of possible denominations
arr = [ 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE
Function call
countMinCoinsUtil ( X , arr , N ) NEW_LINE
Function to calculate Binomial Coefficient C ( n , r )
def binCoff ( n , r ) : NEW_LINE INDENT val = 1 NEW_LINE if ( r > ( n - r ) ) : NEW_LINE DEDENT
C ( n , r ) = C ( n , n - r )
r = ( n - r ) NEW_LINE for i in range ( r ) : NEW_LINE
[ n * ( n - 1 ) * -- - * ( n - r + 1 ) ] / [ r * ( r - 1 ) * -- -- * 1 ]
val *= ( n - i ) NEW_LINE val //= ( i + 1 ) NEW_LINE return val NEW_LINE
Function to calculate the total possible paths
def findWays ( n ) : NEW_LINE
Update n to n - 1
n = n - 1 NEW_LINE
Stores 2 nCn
a = binCoff ( 2 * n , n ) NEW_LINE
Stores Nth Catalan number
b = a // ( n + 1 ) NEW_LINE
Stores the required answer
ans = b NEW_LINE return ans NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( findWays ( n ) ) NEW_LINE DEDENT
Python3 program for the above approach
dp = [ [ 0 ] * 1010 ] * 1010 NEW_LINE
Function that finds the minimum number of steps to find the minimum characters must be moved to convert string s to t
def solve ( s , t ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT
r = maximum value over all dp [ i ] [ j ] computed so far
r = 0 NEW_LINE
dp [ i ] [ j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ]
for j in range ( 0 , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE if ( i > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE DEDENT if ( s [ i ] == t [ j ] ) : NEW_LINE INDENT ans = 1 NEW_LINE if ( i > 0 and j > 0 ) : NEW_LINE INDENT ans = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT
Update the maximum length
dp [ i ] [ j ] = max ( dp [ i ] [ j ] , ans ) NEW_LINE r = max ( r , dp [ i ] [ j ] ) NEW_LINE
Return the resulting length
return ( n - r ) NEW_LINE
Given string s , t
s = " abcde " NEW_LINE t = " edacb " NEW_LINE
Function call
print ( solve ( s , t ) ) NEW_LINE
Function to get the length of longest substring whose characters can be arranged to form a palindromic string
def longestSubstring ( s : str , n : int ) : NEW_LINE
To keep track of the last index of each xor
index = dict ( ) NEW_LINE
Initialize answer with 0
answer = 0 NEW_LINE mask = 0 NEW_LINE index [ mask ] = - 1 NEW_LINE
Now iterate through each character of the string
for i in range ( n ) : NEW_LINE
Convert the character from [ a , z ] to [ 0 , 25 ]
temp = ord ( s [ i ] ) - 97 NEW_LINE
Turn the temp - th bit on if character occurs odd number of times and turn off the temp - th bit off if the character occurs ever number of times
mask ^= ( 1 << temp ) NEW_LINE
If a mask is present in the index Therefore a palindrome is found from index [ mask ] to i
if mask in index . keys ( ) : NEW_LINE INDENT answer = max ( answer , i - index [ mask ] ) NEW_LINE DEDENT
If x is not found then add its position in the index dict .
else : NEW_LINE INDENT index [ mask ] = i NEW_LINE DEDENT
Check for the palindrome of odd length
for j in range ( 26 ) : NEW_LINE
We cancel the occurrence of a character if it occurs odd number times
mask2 = mask ^ ( 1 << j ) NEW_LINE if mask2 in index . keys ( ) : NEW_LINE INDENT answer = max ( answer , i - index [ mask2 ] ) NEW_LINE DEDENT return answer NEW_LINE
Given String
s = " adbabd " NEW_LINE
Length of given string
n = len ( s ) NEW_LINE
Function call
print ( longestSubstring ( s , n ) ) NEW_LINE
Function to find count of N - digit numbers with single digit XOR
def countNums ( N ) : NEW_LINE
dp [ i ] [ j ] stores the number of i - digit numbers with XOR equal to j
dp = [ [ 0 for i in range ( 16 ) ] for j in range ( N ) ] ; NEW_LINE
For 1 - 9 store the value
for i in range ( 1 , 10 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 ; NEW_LINE DEDENT
Iterate till N
for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 0 , 10 ) : NEW_LINE INDENT for k in range ( 0 , 16 ) : NEW_LINE DEDENT DEDENT
Calculate XOR
xor = j ^ k ; NEW_LINE
Store in DP table
dp [ i ] [ xor ] += dp [ i - 1 ] [ k ] ; NEW_LINE
Initialize count
count = 0 ; NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT count += dp [ N - 1 ] [ i ] ; NEW_LINE DEDENT
Print answer
print ( count ) ; NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given number N
N = 1 ; NEW_LINE
Function Call
countNums ( N ) ; NEW_LINE
Function to count the numbers that are divisible by the numbers in the array from range 1 to M
def count ( a , M , N ) : NEW_LINE
Initialize the count variable
cnt = 0 NEW_LINE
Iterate over [ 1 , M ]
for i in range ( 1 , M + 1 ) : NEW_LINE
Iterate over array elements arr [ ]
for j in range ( N ) : NEW_LINE
Check if i is divisible by a [ j ]
if ( i % a [ j ] == 0 ) : NEW_LINE
Increment the count
cnt += 1 NEW_LINE break NEW_LINE
Return the answer
return cnt NEW_LINE
Given list lst
lst = [ 2 , 3 , 5 , 7 ] NEW_LINE
Given number M
m = 100 NEW_LINE n = len ( lst ) NEW_LINE
Function call
print ( count ( lst , m , n ) ) NEW_LINE
Python3 implementation to count the numbers upto N digits such that no two zeros are adjacent
dp = [ [ 0 ] * 10 for j in range ( 15 ) ] NEW_LINE
Function to count the numbers upto N digits such that no two zeros are adjacent
def solve ( n , last , k ) : NEW_LINE
Condition to check if only one element remains
if ( n == 1 ) : NEW_LINE
If last element is non zero , return K - 1
if ( last == k ) : NEW_LINE INDENT return ( k - 1 ) NEW_LINE DEDENT
If last element is 0
else : NEW_LINE INDENT return 1 NEW_LINE DEDENT
Condition to check if value calculated already
if ( dp [ n ] [ last ] ) : NEW_LINE INDENT return dp [ n ] [ last ] NEW_LINE DEDENT
If last element is non zero , then two cases arise , current element can be either zero or non zero
if ( last == k ) : NEW_LINE
Memoize this case
dp [ n ] [ last ] = ( ( k - 1 ) * solve ( n - 1 , k , k ) + ( k - 1 ) * solve ( n - 1 , 1 , k ) ) NEW_LINE return dp [ n ] [ last ] NEW_LINE
If last is 0 , then current can only be non zero
else : NEW_LINE
Memoize and return
dp [ n ] [ last ] = solve ( n - 1 , k , k ) NEW_LINE return dp [ n ] [ last ] NEW_LINE
Given N and K
n = 2 NEW_LINE k = 3 NEW_LINE
Function call
x = solve ( n , k , k ) + solve ( n , 1 , k ) NEW_LINE print ( x ) NEW_LINE
Function to print the count of all prefix in the given string
def Print ( occ , s ) : NEW_LINE
Iterate over string s
for i in range ( 1 , len ( s ) + 1 ) : NEW_LINE
Print the prefix and their frequency
print ( s [ 0 : i ] , " occur " , occ [ i ] , " times . " ) NEW_LINE
Function to implement the LPS array to store the longest prefix which is also a suffix for every substring of the string S
def prefix_function ( s ) : NEW_LINE
Array to store LPS values Value of lps [ 0 ] is 0 by definition
LPS = [ 0 for i in range ( len ( s ) ) ] NEW_LINE
Find the values of LPS [ i ] for the rest of the string using two pointers and DP
for i in range ( 1 , len ( s ) ) : NEW_LINE
Initially set the value of j as the longest prefix that is also a suffix for i as LPS [ i - 1 ]
j = LPS [ i - 1 ] NEW_LINE
Check if the suffix of length j + 1 is also a prefix
while ( j > 0 and s [ i ] != s [ j ] ) : NEW_LINE INDENT j = LPS [ j - 1 ] NEW_LINE DEDENT
If s [ i ] = s [ j ] then , assign LPS [ i ] as j + 1
if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT LPS [ i ] = j + 1 NEW_LINE DEDENT
If we reached j = 0 , assign LPS [ i ] as 0 as there was no prefix equal to suffix
else : NEW_LINE INDENT LPS [ i ] = 0 NEW_LINE DEDENT
Return the calculated LPS array
return LPS NEW_LINE