codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of flips to make a Binary String increasing | Function to find the minimum number of flips required to make string increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; Stores the minimum count of flips ; Traverse the given string S ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Given String ; Function Call
def minimumFlips ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE cnt0 = s . count ( '0' ) NEW_LINE cnt1 = 0 NEW_LINE res = n - cnt0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] == '0' : NEW_LINE INDENT cnt0 -= 1 NEW_LINE DEDENT elif s [ i ] == '1' : NEW_LINE INDENT res = min ( res , cnt1 + cnt0 ) NEW_LINE cnt1 += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT S = '000110' NEW_LINE print ( minimumFlips ( S ) ) NEW_LINE
Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0 | Function to find the maximum value of K such that flipping substrings of size at least K make all characters 0 s ; Stores the maximum value of K ; Traverse the given string S ; Store the minimum of the maximum of LHS and RHS length ; Flip performed ; If no flips performed ; Return the possible value of K ; Driver Code
def maximumK ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE ans = N NEW_LINE flag = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( S [ i ] != S [ i + 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE ans = min ( ans , max ( i + 1 , N - i - 1 ) ) NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "010" NEW_LINE print ( maximumK ( S ) ) NEW_LINE DEDENT
Check if a pair of strings exists that starts with and without the character K or not | Function to check whether a pair of strings exists satisfying the conditions ; Stores the visited strings ; Iterate over the array arr [ ] ; If first character of current string is K ; Otherwise ; Adding to the visited ; Driver Code ; Given Input
def checkhappy ( arr , K , N ) : NEW_LINE INDENT visited = set ( ) NEW_LINE for s in arr : NEW_LINE INDENT if ( s [ 0 ] == K ) : NEW_LINE INDENT if s [ 1 : ] in visited : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( K + s ) in visited : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT visited . add ( s ) NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ ' a ' , ' ! ▁ a ' , ' b ' , ' ! ▁ c ' , ' d ' , ' ! ▁ d ' ] NEW_LINE K = ' ! ' NEW_LINE N = len ( arr ) NEW_LINE print ( checkhappy ( arr , K , N ) ) NEW_LINE DEDENT
Minimum moves to make count of lowercase and uppercase letters equal | Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the S ; If current character is uppercase ; Increment count of Uppercase characters ; Otherwise , ; Increment count of Lowercase characters ; Stores minimum number of moves needed ; If there are more upper case characters ; Iterate until upper is greater than N / 2 ; Convert uppercase into lowercase until upper = N / 2 ; Increment the pointer ; If there are more lower case characters ; Iterate until lower is greater than N / 2 ; Convert lowercase into uppercase until lower = N / 2 ; Increment the pointer ; Print moves required ; Print resultant string ; Driver Code ; Given string ; Function call
def minimumTimeToConvertString ( S , N ) : NEW_LINE INDENT S = [ i for i in S ] NEW_LINE upper = 0 NEW_LINE lower = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT c = S [ i ] NEW_LINE if ( c . isupper ( ) ) : NEW_LINE INDENT upper += 1 NEW_LINE DEDENT else : NEW_LINE INDENT lower += 1 NEW_LINE DEDENT DEDENT moves = 0 NEW_LINE if ( upper > N // 2 ) : NEW_LINE INDENT i = 0 NEW_LINE while ( upper > N // 2 and i < N ) : NEW_LINE INDENT if ( S [ i ] . isupper ( ) ) : NEW_LINE INDENT S [ i ] += 32 NEW_LINE moves += 1 NEW_LINE upper -= 1 NEW_LINE lower += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT elif ( lower > N // 2 ) : NEW_LINE INDENT i = 0 NEW_LINE while ( lower > N // 2 and i < N ) : NEW_LINE INDENT if ( S [ i ] . islower ( ) ) : NEW_LINE INDENT S [ i ] = chr ( ord ( S [ i ] ) - 32 ) NEW_LINE moves += 1 NEW_LINE upper += 1 NEW_LINE lower -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT print ( moves ) NEW_LINE print ( " " . join ( S ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " AbcdEf " NEW_LINE N = len ( S ) NEW_LINE minimumTimeToConvertString ( S , N ) NEW_LINE DEDENT
Program to print a string in vertical zigzag manner | Function to print any string in zigzag fashion ; Store the gap between the major columns ; Traverse through rows ; Store the step value for each row ; Iterate in the range [ 1 , N - 1 ] ; Print the character ; Print the spaces before character s [ j + step ] ; Print the character ; Print the spaces after character after s [ j + step ] ; Print the spaces for first and last rows ; Driver Code ; Given Input ; Function Call
def zigzag ( s , rows ) : NEW_LINE INDENT interval = 2 * rows - 2 NEW_LINE for i in range ( rows ) : NEW_LINE INDENT step = interval - 2 * i NEW_LINE for j in range ( i , len ( s ) , interval ) : NEW_LINE INDENT print ( s [ j ] , end = " " ) NEW_LINE if ( step > 0 and step < interval and step + j < len ( s ) ) : NEW_LINE INDENT for k in range ( ( interval - rows - i ) ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT print ( s [ j + step ] , end = " " ) NEW_LINE for k in range ( i - 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for k in range ( interval - rows ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "123456789ABCDEFGHIJKL " " MNOPQRSTUVWXYZabcdefghi " rows = 9 NEW_LINE zigzag ( s , rows ) NEW_LINE DEDENT
Number of substrings with each character occurring even times | Function to count substrings having even frequency of each character ; Stores the count of a character ; Stores bitmask ; Stores the count of substrings with even count of each character ; Traverse the string S ; Flip the ord ( i ) - 97 bits in pre ; Increment the count by hash [ pre ] ; Increment count of pre in hash ; Return the total count obtained ; Driver Code
def subString ( s , n ) : NEW_LINE INDENT hash = { 0 : 1 } NEW_LINE pre = 0 NEW_LINE count = 0 NEW_LINE for i in s : NEW_LINE INDENT pre ^= ( 1 << ord ( i ) - 97 ) NEW_LINE count += hash . get ( pre , 0 ) NEW_LINE hash [ pre ] = hash . get ( pre , 0 ) + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT S = " abbaa " NEW_LINE N = len ( S ) NEW_LINE print ( subString ( S , N ) ) NEW_LINE
Count number of substrings having at least K distinct characters | Python 3 program for the above approach ; Function to count number of substrings having atleast k distinct characters ; Stores the size of the string ; Initialize a HashMap ; Stores the start and end indices of sliding window ; Stores the required result ; Iterate while the end pointer is less than n ; Include the character at the end of the window ; Increment end pointer by 1 ; Iterate until count of distinct characters becomes less than K ; Remove the character from the beginning of window ; If its frequency is 0 , remove it from the map ; Update the answer ; Print the result ; Driver Code
from collections import defaultdict NEW_LINE def atleastkDistinctChars ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE mp = defaultdict ( int ) NEW_LINE begin = 0 NEW_LINE end = 0 NEW_LINE ans = 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT c = s [ end ] NEW_LINE mp += 1 NEW_LINE end += 1 NEW_LINE while ( len ( mp ) >= k ) : NEW_LINE INDENT pre = s [ begin ] NEW_LINE mp [ pre ] -= 1 NEW_LINE if ( mp [ pre ] == 0 ) : NEW_LINE INDENT del mp [ pre ] NEW_LINE DEDENT ans += len ( s ) - end + 1 NEW_LINE begin += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abcca " NEW_LINE K = 3 NEW_LINE atleastkDistinctChars ( S , K ) NEW_LINE DEDENT
Rearrange characters of a string to make it a concatenation of palindromic substrings | Function to check if a string can be modified such that it can be split into palindromic substrings of length >= 2 ; Stores frequencies of characters ; Traverse the string ; Update frequency of each character ; Traverse the frequency array ; Update values of odd and eve ; Print the result ; Driver Code
def canSplit ( S ) : NEW_LINE INDENT frequency = [ 0 ] * 26 NEW_LINE cnt_singles = 0 NEW_LINE k = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT frequency [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT odd = 0 NEW_LINE eve = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( frequency [ i ] ) : NEW_LINE INDENT odd += ( frequency [ i ] & 1 ) NEW_LINE eve += frequency [ i ] // 2 NEW_LINE DEDENT DEDENT if ( eve >= odd ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aaabbbccc " NEW_LINE canSplit ( S ) NEW_LINE DEDENT
Smallest element in an array that is repeated exactly ' k ' times . | Python program to find smallest number in array that is repeated exactly ' k ' times . ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
MAX = 1000 NEW_LINE def findDuplicate ( arr , n , k ) : NEW_LINE INDENT freq = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 1 and arr [ i ] > MAX ) : NEW_LINE INDENT print " Out ▁ of ▁ range " NEW_LINE return - 1 NEW_LINE DEDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] == k ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 3 , 1 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print findDuplicate ( arr , n , k ) NEW_LINE
Maximize count of occurrences of S2 in S1 as a subsequence by concatenating N1 and N2 times respectively | Function to count maximum number of occurrences of s2 as subsequence in s1 by concatenating s1 , n1 times and s2 , n2 times ; Stores number of times s1 is traversed ; Stores number of times s2 is traversed ; Mapping index of s2 to number of times s1 and s2 are traversed ; Stores index of s1 circularly ; Stores index of s2 circularly ; Traverse the string s1 , n1 times ; If current character of both the string are equal ; Update j ; Update i ; If j is length of s2 ; Update j for circular traversal ; Update s2_reps ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If already mapped j to ( s1_reps , s2_reps ) ; Mapping j to ( s1_reps , s2_reps ) ; If s1 already traversed n1 times ; Otherwis , traverse string s1 by multiple of s1_reps and update both s1_reps and s2_reps ; Update s2_reps ; Update s1_reps ; If s1 is traversed less than n1 times ; If current character in both the string are equal ; Update j ; Update i ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If j is length of ss ; Update j for circular traversal ; Update s2_reps ; Function to count maximum number of occurrences of s2 as subsequence in s1 by concatenating s1 , n1 times and s2 , n2 times
def getMaxRepetitions ( s1 , n1 , s2 , n2 ) : NEW_LINE INDENT if any ( c for c in set ( s2 ) if c not in set ( s1 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s1_reps = 0 NEW_LINE DEDENT + Y4E4 : Z4C4E4 : W4E4 : Y4E4 : Z4E4 : AA4E4 : Z4E4 : Y4E4 : X4E4 : W4 NEW_LINE INDENT s2_reps = 0 NEW_LINE s2_index_to_reps = { 0 : ( 0 , 0 ) } NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while s1_reps < n1 : NEW_LINE INDENT if s1 [ i ] == s2 [ j ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE if j == len ( s2 ) : NEW_LINE INDENT j = 0 NEW_LINE s2_reps += 1 NEW_LINE DEDENT if i == len ( s1 ) : NEW_LINE INDENT i = 0 NEW_LINE s1_reps += 1 NEW_LINE if j in s2_index_to_reps : NEW_LINE INDENT break NEW_LINE DEDENT s2_index_to_reps [ j ] = ( s1_reps , s2_reps ) NEW_LINE DEDENT DEDENT if s1_reps == n1 : NEW_LINE INDENT return s2_reps // n2 NEW_LINE DEDENT initial_s1_reps , initial_s2_reps = s2_index_to_reps [ j ] NEW_LINE loop_s1_reps = s1_reps - initial_s1_reps NEW_LINE loop_s2_reps = s2_reps - initial_s2_reps NEW_LINE loops = ( n1 - initial_s1_reps ) NEW_LINE s2_reps = initial_s2_reps + loops * loop_s2_reps NEW_LINE s1_reps = initial_s1_reps + loops * loop_s1_reps NEW_LINE while s1_reps < n1 : NEW_LINE INDENT if s1 [ i ] == s2 [ j ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE if i == len ( s1 ) : NEW_LINE INDENT i = 0 NEW_LINE s1_reps += 1 NEW_LINE DEDENT if j == len ( s2 ) : NEW_LINE INDENT j = 0 NEW_LINE s2_reps += 1 NEW_LINE DEDENT DEDENT return s2_reps // n2 NEW_LINE DEDENT def getMaxRepetitions ( s1 , n1 , s2 , n2 ) : A4 : AJ4 NEW_LINE INDENT if any ( c for c in set ( s2 ) if c not in set ( s1 ) ) : NEW_LINE INDENT return 0 + E4 : W4 NEW_LINE DEDENT DEDENT
Minimum removals required such that a string can be rearranged to form a palindrome | Function to find the number of deletions required such that characters of the string can be rearranged to form a palindrome ; Stores frequency of characters ; Store the frequency of each character in frequency array ; Count number of characters with odd frequency ; If count is 1 or 0 , return 0 ; Otherwise , return count - 1 ; Driver Code ; Function call to find minimum number of deletions required
def minDeletions ( str ) : NEW_LINE INDENT fre = [ 0 ] * 26 NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT fre [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( fre [ i ] % 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == 0 or count == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return count - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababbccca " NEW_LINE print ( minDeletions ( str ) ) NEW_LINE DEDENT
Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calculating the length of array A ; calling pendulum function
def pendulumArrangement ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE op = [ 0 ] * n NEW_LINE mid = int ( ( n - 1 ) / 2 ) NEW_LINE j = 1 NEW_LINE i = 1 NEW_LINE op [ mid ] = arr [ 0 ] NEW_LINE for i in range ( 1 , mid + 1 ) : NEW_LINE INDENT op [ mid + i ] = arr [ j ] NEW_LINE j += 1 NEW_LINE op [ mid - i ] = arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT if ( int ( n % 2 ) == 0 ) : NEW_LINE INDENT op [ mid + i ] = arr [ j ] NEW_LINE DEDENT print ( " Pendulum ▁ arrangement : " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( op [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 14 , 6 , 19 , 21 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE pendulumArrangement ( arr , n ) NEW_LINE
Calculate score of parentheses from a given string | Function to calculate the score of the parentheses using stack ; To keep track of the score ; Initially , push 0 to stack ; Traverse the string s ; If ' ( ' is encountered , then push 0 to stack ; Otherwise ; Balance the last ' ( ' , and store the score of inner parentheses ; If tmp is not zero , it means inner parentheses exists ; Otherwise , it means no inner parentheses exists ; Pass the score of this level to parent parentheses ; Print the score ; Driver Code ; Function call
def scoreOfParentheses ( s ) : NEW_LINE INDENT stack = [ ] NEW_LINE stack . append ( 0 ) NEW_LINE for c in s : NEW_LINE INDENT if ( c == ' ( ' ) : NEW_LINE INDENT stack . append ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT tmp = stack [ len ( stack ) - 1 ] NEW_LINE stack = stack [ : - 1 ] NEW_LINE val = 0 NEW_LINE if ( tmp > 0 ) : NEW_LINE INDENT val = tmp * 2 NEW_LINE DEDENT else : NEW_LINE INDENT val = 1 NEW_LINE DEDENT stack [ len ( stack ) - 1 ] += val NEW_LINE DEDENT DEDENT print ( stack [ len ( stack ) - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ( ( ) ( ( ) ) ) " NEW_LINE scoreOfParentheses ( S ) NEW_LINE DEDENT
Count unique substrings of a string S present in a wraparound string | Function to find the count of non - empty substrings of p present in s ; Stores the required answer ; Stores the length of substring present in p ; Stores the current length of substring that is present in string s starting from each character of p ; Iterate over the characters of the string ; Check if the current character can be added with previous substring to form the required substring ; Increment current length ; To avoid repetition ; Update arr [ cur ] ; Print the answer ; Driver Code ; Function call to find the count of non - empty substrings of p present in s
def findSubstringInWraproundString ( p ) : NEW_LINE INDENT ans = 0 NEW_LINE curLen = 0 NEW_LINE arr = [ 0 ] * 26 NEW_LINE for i in range ( 0 , len ( p ) ) : NEW_LINE INDENT curr = ord ( p [ i ] ) - ord ( ' a ' ) NEW_LINE if ( i > 0 and ( ord ( p [ i - 1 ] ) != ( ( curr + 26 - 1 ) % 26 + ord ( ' a ' ) ) ) ) : NEW_LINE INDENT curLen = 0 NEW_LINE DEDENT curLen += 1 NEW_LINE if ( curLen > arr [ curr ] ) : NEW_LINE INDENT ans += ( curLen - arr [ curr ] ) NEW_LINE arr [ curr ] = curLen NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT p = " zab " NEW_LINE findSubstringInWraproundString ( p ) NEW_LINE
Minimize sum of given array by removing all occurrences of a single digit | Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver code
def remove ( N , digit ) : NEW_LINE INDENT strN = str ( N ) NEW_LINE ans = ' ' NEW_LINE for i in strN : NEW_LINE INDENT if int ( i ) == digit : NEW_LINE INDENT continue NEW_LINE DEDENT ans += i NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT def getMin ( arr ) : NEW_LINE INDENT minSum = float ( ' inf ' ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT curSum = 0 NEW_LINE for num in arr : NEW_LINE INDENT curSum += remove ( num , i ) NEW_LINE DEDENT minSum = min ( minSum , curSum ) NEW_LINE DEDENT print ( minSum ) NEW_LINE DEDENT arr = [ 34 , 23 , 85 , 93 ] NEW_LINE getMin ( arr ) NEW_LINE
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Python3 program for the above approach ; Function to update frequency of a character in Fenwick tree ; Update frequency of ( idx + ' a ' ) ; Update i ; Function to find the frequency of a character ( idx + ' a ' ) in range [ 1 , i ] ; Stores frequency of character , ( idx + ' a ' ) in range [ 1 , i ] ; Update ans ; Update i ; Function to find difference between maximum and minimum frequency of a character in given range ; BIT [ i ] [ j ] : Stores frequency of ( i + ' a ' ) If j is a power of 2 , then it stores the frequency ( i + ' a ' ) of from [ 1 ] [ j ] ; Stores length of String ; Iterate over the characters of the String ; Update the frequency of s [ i ] in fenwick tree ; Stores count of queries ; Iterate over all the queries ; Stores maximum frequency of a character in range [ l , r ] ; Stores minimum frequency of a character in range [ l , r ] ; Iterate over all possible characters ; Stores frequency of ( j + ' a ' ) in range [ 1 , r ] ; Stores frequency of ( j + ' a ' ) in range [ 1 , l - 1 ] ; Update mx ; If a character ( i + ' a ' ) present in range [ l , r ] ; Update mn ; Print the difference between max and min freq ; Given String ; Given queries ; Function Call
import sys NEW_LINE def update ( BIT , idx , i , val ) : NEW_LINE INDENT while ( i < 10005 ) : NEW_LINE BIT [ idx ] [ i ] += val NEW_LINE i = i + ( i & ( - i ) ) NEW_LINE DEDENT def query ( BIT , idx , i ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( i > 0 ) : NEW_LINE ans += BIT [ idx ] [ i ] NEW_LINE i = i - ( i & ( - i ) ) NEW_LINE return ans NEW_LINE DEDENT def maxDiffFreq ( s , queries ) : NEW_LINE INDENT BIT = [ [ 0 for i in range ( 10005 ) ] for j in range ( 26 ) ] NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE update ( BIT , ord ( s [ i ] ) - ord ( ' a ' ) , i + 1 , 1 ) NEW_LINE Q = len ( queries ) NEW_LINE for i in range ( Q ) : NEW_LINE mx = 0 NEW_LINE mn = sys . maxsize NEW_LINE l = queries [ i ] [ 0 ] NEW_LINE r = queries [ i ] [ 1 ] NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT p = query ( BIT , j , r ) NEW_LINE q = query ( BIT , j , l - 1 ) NEW_LINE mx = max ( mx , p - q ) NEW_LINE if ( p > 0 ) : NEW_LINE mn = min ( mn , p - q ) NEW_LINE DEDENT print ( mx - mn ) NEW_LINE DEDENT S = " abaabac " NEW_LINE queries = [ [ 2 , 6 ] , [ 1 , 7 ] ] NEW_LINE maxDiffFreq ( S , queries ) NEW_LINE
Make all strings from a given array equal by replacing minimum number of characters | Function to find the minimum count of operations required to make all Strings equal by replacing characters of Strings ; Stores minimum count of operations required to make all Strings equal ; Stores length of the String ; hash [ i ] [ j ] : Stores frequency of character i present at j - th index of all Strings ; Traverse the array arr ; Iterate over characters of current String ; Update frequency of arr [ i ] [ j ] ; Traverse hash array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
def minOperation ( arr , N ) : NEW_LINE INDENT cntMinOP = 0 ; NEW_LINE M = len ( arr [ 0 ] ) ; NEW_LINE hash = [ [ 0 for i in range ( M ) ] for j in range ( 256 ) ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT hash [ ord ( arr [ i ] [ j ] ) ] [ j ] += 1 ; NEW_LINE DEDENT DEDENT for i in range ( M ) : NEW_LINE INDENT Sum = 0 ; NEW_LINE Max = 0 ; NEW_LINE for j in range ( 256 ) : NEW_LINE INDENT Sum += hash [ j ] [ i ] ; NEW_LINE Max = max ( Max , hash [ j ] [ i ] ) ; NEW_LINE DEDENT cntMinOP += ( Sum - Max ) ; NEW_LINE DEDENT return cntMinOP ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " abcd " , " bcde " , " cdef " ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( minOperation ( arr , N ) ) ; NEW_LINE DEDENT
Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ;  ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call
def countStrings ( s , k ) : NEW_LINE INDENT ans = 1 NEW_LINE lenn = len ( s ) NEW_LINE flag = 0 NEW_LINE DEDENT / * Store the starting index of NEW_LINE INDENT the substring * / NEW_LINE for i in range ( lenn - 1 ) : NEW_LINE INDENT if ( ord ( s [ i ] ) - ord ( '0' ) + ord ( s [ i + 1 ] ) - ord ( '0' ) == k and flag == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE start_ind = i NEW_LINE DEDENT if ( flag == 1 and ord ( s [ i ] ) - ord ( '0' ) + ord ( s [ i + 1 ] ) - ord ( '0' ) != k ) : NEW_LINE INDENT flag = 0 NEW_LINE if ( ( i - start_ind + 1 ) % 2 != 0 ) : NEW_LINE INDENT ans *= ( i - start_ind + 1 - 1 ) // 2 + 1 NEW_LINE DEDENT DEDENT DEDENT if ( flag == 1 and ( lenn - start_ind ) % 2 != 0 ) : NEW_LINE INDENT ans *= ( lenn - start_ind ) // 2 + 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT S = "313" NEW_LINE K = 4 NEW_LINE countStrings ( S , K ) NEW_LINE