codeparrot/xlcost-text-to-code huggingface dataset:
Reverse the Words of a String using Stack | Function to reverse the words of the given sentence ; Create an empty character array stack ; Push words into the stack ; Get the words in reverse order ; Driver code
def reverse ( k ) : NEW_LINE INDENT s = [ ] NEW_LINE token = k . split ( ) NEW_LINE for word in token : NEW_LINE INDENT s . append ( word ) ; NEW_LINE DEDENT while ( len ( s ) ) : NEW_LINE INDENT print ( s . pop ( ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = " geeks ▁ for ▁ geeks " ; NEW_LINE reverse ( k ) ; NEW_LINE DEDENT
Count of substrings which contains a given character K times | Function to count the number of substrings which contains the character C exactly K times ; left and right counters for characters on both sides of subwindow ; left and right pointer on both sides of subwindow ; Initialize the frequency ; result and Length of string ; initialize the left pointer ; initialize the right pointer ; traverse all the window substrings ; counting the characters on leftSide of subwindow ; counting the characters on rightSide of subwindow ; Add the possible substrings on both sides to result ; Setting the frequency for next subwindow ; reset the left , right counters ; Driver code
def countSubString ( s , c , k ) : NEW_LINE INDENT leftCount = 0 NEW_LINE rightCount = 0 NEW_LINE left = 0 NEW_LINE right = 0 NEW_LINE freq = 0 NEW_LINE result = 0 NEW_LINE Len = len ( s ) NEW_LINE while ( s [ left ] != c and left < Len ) : NEW_LINE INDENT left += 1 NEW_LINE leftCount += 1 NEW_LINE DEDENT right = left + 1 NEW_LINE while ( freq != ( k - 1 ) and ( right - 1 ) < Len ) : NEW_LINE INDENT if ( s [ right ] == c ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT right += 1 NEW_LINE DEDENT while ( left < Len and ( right - 1 ) < Len ) : NEW_LINE INDENT while ( s [ left ] != c and left < Len ) : NEW_LINE INDENT left += 1 NEW_LINE leftCount += 1 NEW_LINE DEDENT while ( right < Len and s [ right ] != c ) : NEW_LINE INDENT if ( s [ right ] == c ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT right += 1 NEW_LINE rightCount += 1 NEW_LINE DEDENT result = ( result + ( leftCount + 1 ) * ( rightCount + 1 ) ) NEW_LINE freq = k - 1 NEW_LINE leftCount = 0 NEW_LINE rightCount = 0 NEW_LINE left += 1 NEW_LINE right += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT s = "3123231" NEW_LINE c = '3' NEW_LINE k = 2 NEW_LINE print ( countSubString ( s , c , k ) ) NEW_LINE
Count of sub | Python3 implementation of the approach ; Function to return the total required sub - sequences ; Find ways for all values of x ; x + 1 ; Removing all unnecessary digits ; Prefix Sum Array for X + 1 digit ; Sum of squares ; Previous sum of all possible pairs ; To find sum of multiplication of all possible pairs ; To prevent overcounting ; Adding ways for all possible x ; Driver code
MOD = 1000000007 NEW_LINE def solve ( test ) : NEW_LINE INDENT size = len ( test ) NEW_LINE total = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT x = i NEW_LINE y = i + 1 NEW_LINE newtest = " " NEW_LINE for j in range ( size ) : NEW_LINE INDENT if ( ord ( test [ j ] ) == x + 48 or ord ( test [ j ] ) == y + 48 ) : NEW_LINE INDENT newtest += test [ j ] NEW_LINE DEDENT DEDENT if ( len ( newtest ) > 0 ) : NEW_LINE INDENT size1 = len ( newtest ) NEW_LINE prefix = [ 0 for i in range ( size1 ) ] NEW_LINE for j in range ( size1 ) : NEW_LINE INDENT if ( ord ( newtest [ j ] ) == y + 48 ) : NEW_LINE INDENT prefix [ j ] += 1 NEW_LINE DEDENT DEDENT for j in range ( 1 , size1 ) : NEW_LINE INDENT prefix [ j ] += prefix [ j - 1 ] NEW_LINE DEDENT count = 0 NEW_LINE firstcount = 0 NEW_LINE ss = 0 NEW_LINE prev = 0 NEW_LINE for j in range ( size1 ) : NEW_LINE INDENT if ( ord ( newtest [ j ] ) == x + 48 ) : NEW_LINE INDENT count += 1 NEW_LINE firstcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ss += count * count NEW_LINE pairsum = ( firstcount * firstcount - ss ) // 2 NEW_LINE temp = pairsum NEW_LINE pairsum -= prev NEW_LINE prev = temp NEW_LINE secondway = prefix [ size1 - 1 ] NEW_LINE if ( j != 0 ) : NEW_LINE INDENT secondway -= prefix [ j - 1 ] NEW_LINE DEDENT answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) NEW_LINE answer //= 4 NEW_LINE answer += ( pairsum * secondway * ( secondway - 1 ) ) // 2 NEW_LINE total += answer NEW_LINE count = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT return total NEW_LINE DEDENT test = "13134422" NEW_LINE print ( solve ( test ) ) NEW_LINE
Find if it is possible to make a binary string which contanins given number of "0" , "1" , "01" and "10" as sub sequences | Function that returns true if it is possible to make a binary string consisting of l 0 ' s , ▁ m ▁ 1' s , x "01" sub - sequences and y "10" sub - sequences ; Driver code
def isPossible ( l , m , x , y ) : NEW_LINE INDENT if ( l * m == x + y ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 3 ; m = 2 ; x = 4 ; y = 2 ; NEW_LINE if ( isPossible ( l , m , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Count pairs of characters in a string whose ASCII value difference is K | Python3 implementation of the approach ; Function to return the count of required pairs of characters ; Length of the string ; To store the frequency of each character ; Update the frequency of each character ; To store the required count of pairs ; If ascii value difference is zero ; If there exists similar characters more than once ; If there exits characters with ASCII value difference as k ; Return the required count ; Driver code
MAX = 26 NEW_LINE def countPairs ( string , k ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE freq = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE if ( k == 0 ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] > 1 ) : NEW_LINE INDENT cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) // 2 ) ; NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] > 0 and i + k < MAX and freq [ i + k ] > 0 ) : NEW_LINE INDENT cnt += ( freq [ i ] * freq [ i + k ] ) ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abcdab " ; NEW_LINE k = 0 ; NEW_LINE print ( countPairs ( string , k ) ) ; NEW_LINE DEDENT
Count of three non | Function that returns true if s [ i ... j ] + s [ k ... l ] + s [ p ... q ] is a palindrome ; Function to return the count of valid sub - strings ; To store the count of required sub - strings ; For choosing the first sub - string ; For choosing the second sub - string ; For choosing the third sub - string ; Check if the concatenation is a palindrome ; Driver code
def isPalin ( i , j , k , l , p , q , s ) : NEW_LINE INDENT start = i ; end = q ; NEW_LINE while ( start < end ) : NEW_LINE INDENT if ( s [ start ] != s [ end ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT start += 1 ; NEW_LINE if ( start == j + 1 ) : NEW_LINE INDENT start = k ; NEW_LINE DEDENT end -= 1 ; NEW_LINE if ( end == p - 1 ) : NEW_LINE INDENT end = l ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def countSubStr ( s ) : NEW_LINE INDENT count = 0 ; NEW_LINE n = len ( s ) ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT for j in range ( i , n - 2 ) : NEW_LINE INDENT for k in range ( j + 1 , n - 1 ) : NEW_LINE INDENT for l in range ( k , n - 1 ) : NEW_LINE INDENT for p in range ( l + 1 , n ) : NEW_LINE INDENT for q in range ( p , n ) : NEW_LINE INDENT if ( isPalin ( i , j , k , l , p , q , s ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abca " ; NEW_LINE print ( countSubStr ( s ) ) ; NEW_LINE DEDENT
Find the sum of the ascii values of characters which are present at prime positions | Python3 implementation of the approach ; Function that returns true if n is prime ; Function to return the sum of the ascii values of the characters which are present at prime positions ; To store the sum ; For every character ; If current position is prime then add the ASCII value of the character at the current position ; Driver code
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def sumAscii ( string , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPrime ( i + 1 ) ) : NEW_LINE INDENT sum += ord ( string [ i ] ) ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE n = len ( string ) ; NEW_LINE print ( sumAscii ( string , n ) ) ; NEW_LINE DEDENT
Count the nodes of the tree which make a pangram when concatenated with the sub | Python3 implementation of the approach ; Function that returns if the string x is a pangram ; Function to return the count of nodes which make pangram with the sub - tree nodes ; Function to perform dfs and update the nodes such that weight [ i ] will store the weight [ i ] concatenated with the weights of all the nodes in the sub - tree ; Driver code ; Weights of the nodes ; Edges of the tree
graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE def Pangram ( x ) : NEW_LINE INDENT mp = { } NEW_LINE n = len ( x ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if x [ i ] not in mp : NEW_LINE INDENT mp [ x [ i ] ] = 0 NEW_LINE DEDENT mp [ x [ i ] ] += 1 NEW_LINE DEDENT if ( len ( mp ) == 26 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def countTotalPangram ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( Pangram ( weight [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT def dfs ( node , parent ) : NEW_LINE INDENT for to in graph [ node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE weight [ node ] += weight [ to ] NEW_LINE DEDENT DEDENT n = 6 NEW_LINE weight [ 1 ] = " abcde " NEW_LINE weight [ 2 ] = " fghijkl " NEW_LINE weight [ 3 ] = " abcdefg " NEW_LINE weight [ 4 ] = " mnopqr " NEW_LINE weight [ 5 ] = " stuvwxy " NEW_LINE weight [ 6 ] = " zabcdef " NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE graph [ 5 ] . append ( 6 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( countTotalPangram ( n ) ) NEW_LINE
Count the nodes of a tree whose weighted string does not contain any duplicate characters | Python3 implementation of the approach ; Function that returns true if the string contains unique characters ; Function to perform dfs ; If weight of the current node node contains unique characters ; Driver code ; Weights of the node ; Edges of the tree
cnt = 0 NEW_LINE graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE def uniqueChars ( x ) : NEW_LINE INDENT mp = { } NEW_LINE n = len ( x ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if x [ i ] not in mp : NEW_LINE INDENT mp [ x [ i ] ] = 0 NEW_LINE DEDENT mp [ x [ i ] ] += 1 NEW_LINE DEDENT if ( len ( mp ) == len ( x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def dfs ( node , parent ) : NEW_LINE INDENT global cnt , x NEW_LINE if ( uniqueChars ( weight [ node ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT for to in graph [ node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE DEDENT DEDENT x = 5 NEW_LINE weight [ 1 ] = " abc " NEW_LINE weight [ 2 ] = " aba " NEW_LINE weight [ 3 ] = " bcb " NEW_LINE weight [ 4 ] = " moh " NEW_LINE weight [ 5 ] = " aa " NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( cnt ) NEW_LINE
Check if a string contains two non overlapping sub | Function that returns true if s contains two non overlapping sub strings " geek " and " keeg " ; If " geek " and " keeg " are both present in s without over - lapping and " keeg " starts after " geek " ends ; Driver code
def isValid ( s ) : NEW_LINE INDENT p = " " NEW_LINE p = s . find ( " geek " ) NEW_LINE if ( s . find ( " keeg " , p + 4 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geekeekeeg " NEW_LINE if ( isValid ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the last non repeating character in string | Maximum distinct characters possible ; Function to return the last non - repeating character ; To store the frequency of each of the character of the given string ; Update the frequencies ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the string are repeating ; Driver code
MAX = 256 ; NEW_LINE def lastNonRepeating ( string , n ) : NEW_LINE INDENT freq = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) ] += 1 ; NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT ch = string [ i ] ; NEW_LINE if ( freq [ ord ( ch ) ] == 1 ) : NEW_LINE INDENT return ( " " + ch ) ; NEW_LINE DEDENT DEDENT return " - 1" ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GeeksForGeeks " ; NEW_LINE n = len ( string ) ; NEW_LINE print ( lastNonRepeating ( string , n ) ) ; NEW_LINE DEDENT
Minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B | Function to return the minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B ; Initialize result ; Loop through last b digits ; Driver code
def findCount ( s , n , a , b ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( b ) : NEW_LINE INDENT if ( i == a ) : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '1' ) NEW_LINE DEDENT else : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '0' ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "1001011001" NEW_LINE N = len ( str ) NEW_LINE A = 3 NEW_LINE B = 6 NEW_LINE print ( findCount ( str , N , A , B ) ) NEW_LINE DEDENT
Find letter 's position in Alphabet using Bit operation | Python3 implementation of the approach ; Function to calculate the position of characters ; Performing AND operation with number 31 ; Driver code
NUM = 31 NEW_LINE def positions ( str ) : NEW_LINE INDENT for i in str : NEW_LINE INDENT print ( ( ord ( i ) & NUM ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT str = " Geeks " NEW_LINE positions ( str ) NEW_LINE
Generate all permutations of a string that follow given constraints | Simple Python program to print all permutations of a string that follow given constraint ; Check if current permutation is valid ; Recursively generate all permutation ; Driver Code
def permute ( str , l , r ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT if " AB " not in ' ' . join ( str ) : NEW_LINE INDENT print ( ' ' . join ( str ) , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( l , r + 1 ) : NEW_LINE INDENT str [ l ] , str [ i ] = str [ i ] , str [ l ] NEW_LINE permute ( str , l + 1 , r ) NEW_LINE str [ l ] , str [ i ] = str [ i ] , str [ l ] NEW_LINE DEDENT DEDENT str = " ABC " NEW_LINE permute ( list ( str ) , 0 , len ( str ) - 1 ) NEW_LINE
Length of the longest substring that do not contain any palindrome | Function to find the length of the longest substring ; initializing the variables ; checking palindrome of size 2 example : aa ; checking palindrome of size 3 example : aba ; else : incrementing length of substring ; If there exits single character then it is always palindrome ; Driver Code
def lenoflongestnonpalindrome ( s ) : NEW_LINE INDENT max1 , length = 1 , 0 NEW_LINE for i in range ( 0 , len ( s ) - 1 ) : NEW_LINE INDENT if s [ i ] == s [ i + 1 ] : NEW_LINE INDENT length = 0 NEW_LINE DEDENT elif s [ i + 1 ] == s [ i - 1 ] and i > 0 : NEW_LINE INDENT length = 1 NEW_LINE length += 1 NEW_LINE DEDENT DEDENT if max1 == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return max1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " synapse " NEW_LINE print ( lenoflongestnonpalindrome ( s ) ) NEW_LINE DEDENT
Calculate score for the given binary string | Function to return the score for the given binary string ; Traverse through string character ; Initialize current chunk 's size ; Get current character ; Calculate total chunk size of same characters ; Add / subtract pow ( chunkSize , 2 ) depending upon character ; Return the score ; Driver code
def calcScore ( str ) : NEW_LINE INDENT score = 0 NEW_LINE len1 = len ( str ) NEW_LINE i = 0 NEW_LINE while ( i < len1 ) : NEW_LINE INDENT chunkSize = 1 NEW_LINE currentChar = str [ i ] NEW_LINE i += 1 NEW_LINE while ( i < len1 and str [ i ] == currentChar ) : NEW_LINE INDENT chunkSize += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( currentChar == '1' ) : NEW_LINE INDENT score += pow ( chunkSize , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT score -= pow ( chunkSize , 2 ) NEW_LINE DEDENT DEDENT return score NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "11011" NEW_LINE print ( calcScore ( str ) ) NEW_LINE DEDENT
Number of sub | Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code
def countSubString ( s , c , k ) : NEW_LINE INDENT leftCount = 0 NEW_LINE rightCount = 0 NEW_LINE left = 0 NEW_LINE right = 0 NEW_LINE freq = 0 NEW_LINE result = 0 NEW_LINE len1 = len ( s ) NEW_LINE while ( s [ left ] != c and left < len1 ) : NEW_LINE INDENT left += 1 NEW_LINE leftCount += 1 NEW_LINE DEDENT right = left + 1 NEW_LINE while ( freq != ( k - 1 ) and ( right - 1 ) < len1 ) : NEW_LINE INDENT if ( s [ right ] == c ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT right += 1 NEW_LINE DEDENT while ( left < len1 and ( right - 1 ) < len1 ) : NEW_LINE INDENT while ( s [ left ] != c and left < len1 ) : NEW_LINE INDENT left += 1 NEW_LINE leftCount += 1 NEW_LINE DEDENT while ( right < len1 and s [ right ] != c ) : NEW_LINE INDENT if ( s [ right ] == c ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT right += 1 NEW_LINE rightCount += 1 NEW_LINE DEDENT result = ( result + ( leftCount + 1 ) * ( rightCount + 1 ) ) NEW_LINE freq = k - 1 NEW_LINE leftCount = 0 NEW_LINE rightCount = 0 NEW_LINE left += 1 NEW_LINE right += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " abada " NEW_LINE c = ' a ' NEW_LINE k = 2 NEW_LINE print ( countSubString ( s , c , k ) ) NEW_LINE DEDENT
Maximum length palindrome that can be created with characters in range L and R | Python3 implementation of the approach ; Function to return the length of the longest palindrome that can be formed using the characters in the range [ l , r ] ; 0 - based indexing ; Marks if there is an odd frequency character ; Length of the longest palindrome possible from characters in range ; Traverse for all characters and count their frequencies ; Find the frequency in range 1 - r ; Exclude the frequencies in range 1 - ( l - 1 ) ; If frequency is odd , then add 1 less than the original frequency to make it even ; Else completely add if even ; If any odd frequency character is present then add 1 ; Function to pre - calculate the frequencies of the characters to reduce complexity ; Iterate and increase the count ; Create a prefix type array ; Driver code ; Pre - calculate prefix array ; Perform queries
N = 4 NEW_LINE def performQueries ( l , r , prefix ) : NEW_LINE INDENT l -= 1 NEW_LINE r -= 1 NEW_LINE flag = False NEW_LINE count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT cnt = prefix [ r ] [ i ] NEW_LINE if ( l > 0 ) : NEW_LINE INDENT cnt -= prefix [ l - 1 ] [ i ] NEW_LINE DEDENT if ( cnt % 2 == 1 ) : NEW_LINE INDENT flag = True NEW_LINE count += cnt - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += cnt NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def preCalculate ( s , prefix ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT prefix [ i ] [ j ] += prefix [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT s = " amim " NEW_LINE prefix = [ [ 0 for i in range ( 26 ) ] for i in range ( N ) ] NEW_LINE preCalculate ( s , prefix ) NEW_LINE queries = [ [ 1 , 4 ] , [ 3 , 4 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( performQueries ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , prefix ) ) NEW_LINE DEDENT
Number of times the given string occurs in the array in the range [ l , r ] | Python implementation of the approach ; Function to return the number of occurrences of ; To store the indices of strings in the array ; If current string doesn 't  have an entry in the map  then create the entry ; If the given string is not present in the array ; If the given string is present in the array ; Driver Code
from bisect import bisect_right as upper_bound NEW_LINE from collections import defaultdict NEW_LINE def numOccurences ( arr : list , n : int , string : str , L : int , R : int ) -> int : NEW_LINE INDENT M = defaultdict ( lambda : list ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if temp not in M : NEW_LINE INDENT A = [ ] NEW_LINE A . append ( i + 1 ) NEW_LINE M [ temp ] = A NEW_LINE DEDENT else : NEW_LINE INDENT M [ temp ] . append ( i + 1 ) NEW_LINE DEDENT DEDENT if string not in M : NEW_LINE INDENT return 0 NEW_LINE DEDENT A = M [ string ] NEW_LINE y = upper_bound ( A , R ) NEW_LINE x = upper_bound ( A , L - 1 ) NEW_LINE return ( y - x ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " abc " , " abcabc " , " abc " ] NEW_LINE n = len ( arr ) NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE string = " abc " NEW_LINE print ( numOccurences ( arr , n , string , L , R ) ) NEW_LINE DEDENT