human
Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | Function to return the number of ways to form the sub - string.
Initialize a hash - table with 0.
Iterate in the string and count the frequency of characters that do not lie in the range L and R.
Out of range characters.
Stores the final number of ways.
Iterate for the sub - string in the range L and R.
If exists then multiply the number of ways and decrement the frequency.
If does not exist the sub - string cannot be formed.
Return the answer.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to number of ways in which the substring in range [ L , R ] can be formed using characters out of the range and the other steps required.
 def calculateWays ( s , n , l , r ) :
	freq = [ 0 for i in range ( 26 ) ]
	for i in range ( n ) :
		if ( i < l or i > r ) :
			freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1
	ways = 1
	for i in range ( l , r + 1 , 1 ) :
		if ( freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] ) :
			ways = ways * freq [ ord ( s [ i ] ) - ord ( ' a ' ) ]
			freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] -= 1
		else :
			ways = 0
			break
	return ways
if __name__ == ' _ _ main _ _ ' :
	s = " cabcaab "
	n = len ( s )
	l = 1
	r = 3
	print ( calculateWays ( s , n , l , r ) )