codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to split a string into two subsets that are reverse of each other | Function to find the total number of ways to partitiaon the string into two subset satisfying the conditions ; Stores the resultant number of ways of splitting ; Iterate over the range [ 0 , 2 ^ N ] ; Traverse the string S ; If ith bit is set , then append the character S [ i ] to X ; Otherwise , append the character S [ i ] to Y ; Reverse the second string ; If X is equal to Y ; Return the total number of ways ; Driver Code
def countWays ( S , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for mask in range ( ( 1 << N ) ) : NEW_LINE INDENT X , Y = " " , " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( mask >> i & 1 ) : NEW_LINE INDENT X += S [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT Y += S [ i ] NEW_LINE DEDENT DEDENT Y = Y [ : : - 1 ] NEW_LINE if ( X == Y ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " mippiisssisssiipsspiim " NEW_LINE N = len ( S ) NEW_LINE print ( countWays ( S , N ) ) NEW_LINE DEDENT
Check if String formed by first and last X characters of a String is a Palindrome | Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given input ; Function Call
def isEqualSubstring ( string , x ) : NEW_LINE INDENT n = len ( string ) NEW_LINE i = 0 NEW_LINE while i < n and i < x : NEW_LINE INDENT if ( string [ i ] != string [ n - i - 1 ] ) : NEW_LINE INDENT print ( " false " ) NEW_LINE return NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( " true " ) NEW_LINE return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " GeeksforGeeks " NEW_LINE x = 3 NEW_LINE isEqualSubstring ( string , x ) NEW_LINE DEDENT
Flip the String by either swapping given characters or rotating it horizontally for Q queries | Function to find final string after applying all Queries on it ; Traverse the Queries array ; Convert A , B to zero indexing ; Query of 1 st type ; Swap ath and bth characters ; Query of 2 nd type ; Swap first N characters with last N characters ; Print answer ; Driver code ; Input ; Function call
def solve ( S , N , Queries , Q ) : NEW_LINE INDENT S = list ( S ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT T = Queries [ i ] [ 0 ] NEW_LINE A = Queries [ i ] [ 1 ] NEW_LINE B = Queries [ i ] [ 2 ] NEW_LINE A -= 1 NEW_LINE B -= 1 NEW_LINE if ( T == 1 ) : NEW_LINE INDENT temp = S [ A ] NEW_LINE S [ A ] = S [ B ] NEW_LINE S [ B ] = temp NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT temp = S [ j ] NEW_LINE S [ j ] = S [ j + N ] NEW_LINE S [ j + N ] = temp NEW_LINE DEDENT DEDENT DEDENT S = ' ' . join ( S ) NEW_LINE print ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ABCD " NEW_LINE N = len ( S ) // 2 NEW_LINE Queries = [ [ 2 , 0 , 0 ] , [ 1 , 1 , 3 ] , [ 2 , 0 , 0 ] ] NEW_LINE Q = len ( Queries ) NEW_LINE solve ( S , N , Queries , Q ) NEW_LINE DEDENT
Minimum number of replacement done of substring "01" with "110" to remove it completely | Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code
def minimumOperations ( S , N ) : NEW_LINE INDENT ans = 0 NEW_LINE cntOne = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT ans += cntOne NEW_LINE cntOne *= 2 NEW_LINE DEDENT else : NEW_LINE INDENT cntOne += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "001" NEW_LINE N = len ( S ) NEW_LINE minimumOperations ( S , N ) NEW_LINE DEDENT
Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string | Function to find the number of ways to obtain different Bitwise OR ; Stores the count of pairs t00 , t10 , t01 , t11 ; Traverse the characters of the string S1 and S2 ; Count the pair ( 0 , 0 ) ; Count the pair ( 1 , 0 ) ; Count the pair ( 1 , 1 ) ; Count the pair ( 0 , 1 ) ; Number of ways to calculate the different bitwise OR ; Print the result ; Driver Code
def differentBitwiseOR ( s1 , s2 ) : NEW_LINE INDENT n = len ( s1 ) NEW_LINE t00 = 0 NEW_LINE t10 = 0 NEW_LINE t01 = 0 NEW_LINE t11 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s1 [ i ] == '0' and s2 [ i ] == '0' ) : NEW_LINE INDENT t00 += 1 NEW_LINE DEDENT if ( s1 [ i ] == '1' and s2 [ i ] == '0' ) : NEW_LINE INDENT t10 += 1 NEW_LINE DEDENT if ( s1 [ i ] == '1' and s2 [ i ] == '1' ) : NEW_LINE INDENT t11 += 1 NEW_LINE DEDENT if ( s1 [ i ] == '0' and s2 [ i ] == '1' ) : NEW_LINE INDENT t01 += 1 NEW_LINE DEDENT DEDENT ans = t00 * t10 + t01 * t10 + t00 * t11 NEW_LINE print ( ans ) NEW_LINE DEDENT S1 = "01001" NEW_LINE S2 = "11011" NEW_LINE differentBitwiseOR ( S1 , S2 ) NEW_LINE
Flip all 0 s in given Binary Strings K times with different neighbours | Function to modify the given string K number of times by flipping 0 s having different adjacent characters ; Size of the string ; Stores modified string after each iteration ; Iterate over the range [ 0 k ] ; Traverse the string S ; If '0' is present at 0 th index then replace it with 1 st index ; If '0' is present at the last index then replace it with last index - 1 character ; Otherwise , convert 0 s to 1 if the adjacent characters are different ; If during this iteration there is no change in the string then break this loop ; Update the string S ; Print the updated string ; Driver Code
def convertString ( S , k ) : NEW_LINE INDENT n = len ( S ) NEW_LINE temp = S NEW_LINE temp = list ( temp ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT if ( j == 0 and S [ j ] == '0' ) : NEW_LINE INDENT temp [ j ] = S [ j + 1 ] NEW_LINE DEDENT elif ( j == n - 1 and S [ j ] == '0' ) : NEW_LINE INDENT temp [ j ] = S [ j - 1 ] NEW_LINE DEDENT elif ( S [ j - 1 ] != S [ j + 1 ] and S [ j ] == '0' ) : NEW_LINE INDENT temp [ j ] = '1' NEW_LINE DEDENT DEDENT if ( S == temp ) : NEW_LINE INDENT break NEW_LINE DEDENT temp = ' ' . join ( temp ) NEW_LINE S = temp NEW_LINE DEDENT print ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "10010001" NEW_LINE K = 1 NEW_LINE convertString ( S , K ) NEW_LINE DEDENT
Minimum number of removals required such that no subsequence of length 2 occurs more than once | Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver Code
def RemoveCharacters ( s ) : NEW_LINE INDENT ans = " " NEW_LINE c = [ 0 for i in range ( 26 ) ] NEW_LINE pos = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( c [ ord ( s [ i ] ) - 97 ] == 0 ) : NEW_LINE INDENT c [ ord ( s [ i ] ) - 97 ] = 1 NEW_LINE pos = i NEW_LINE ans += s [ i ] NEW_LINE DEDENT DEDENT for i in range ( pos + 1 , len ( s ) , 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ 0 ] ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcaadbcd " NEW_LINE RemoveCharacters ( S ) NEW_LINE DEDENT
Check if a numeric string can be split into substrings having difference between consecutive numbers equal to K | Function to check if a numeric string can be split into substrings such that the difference between the consecutive substrings is K ; Stores the size of the string ; Iterate over the range [ 1 , N ] and try each possible starting number ; Convert the number to string ; Build up a sequence starting with the number ; Compare it with the original string s ; Print the result ; Driver Code
def isPossible ( s , K ) : NEW_LINE INDENT valid = False NEW_LINE firstx = - 1 NEW_LINE n = len ( s ) NEW_LINE for i in range ( 1 , n // 2 + 1 , 1 ) : NEW_LINE INDENT x = int ( s [ 0 : i ] ) NEW_LINE firstx = x NEW_LINE test = str ( x ) NEW_LINE while ( len ( test ) < len ( s ) ) : NEW_LINE INDENT x -= K NEW_LINE test += str ( x ) NEW_LINE DEDENT if ( test == s ) : NEW_LINE INDENT valid = True NEW_LINE break NEW_LINE DEDENT DEDENT print ( " Yes " ) if valid == True else print ( " No " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "8642" NEW_LINE K = 2 NEW_LINE isPossible ( S , K ) NEW_LINE DEDENT
Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , pr " YES " else pr " NO " ; Driver code ; Given Input ; Function call
def changeCharacters ( S , N , K ) : NEW_LINE INDENT flag = 1 NEW_LINE count = 0 NEW_LINE st = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if len ( st ) == 0 : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT count = 0 NEW_LINE st . append ( S [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == K ) : NEW_LINE INDENT del st [ - 1 ] NEW_LINE count = 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT count = 0 NEW_LINE DEDENT DEDENT DEDENT if ( flag ) : 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 = "100100" NEW_LINE N = len ( S ) NEW_LINE K = 2 NEW_LINE changeCharacters ( S , N , K ) NEW_LINE DEDENT
Check if all prefixes of a number is divisible by remaining count of digits | Function to check if all prefixes of a number is divisible by remaining count of digits or not ; Traverse and check divisibility for each updated number ; Update the original number ; Given Input ; Function Call
def prefixDivisble ( n ) : NEW_LINE INDENT i = 1 NEW_LINE while n > 0 : NEW_LINE INDENT if n % i != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // 10 NEW_LINE i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 52248 NEW_LINE if ( prefixDivisble ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Length of the longest substring that contains even number of vowels | Function to find the length of the longest substring having even number of vowels ; Create two hashmaps ; Keep the track of frequencies of the vowels ; Stores the maximum length ; Traverse the given string S ; If it is a vowel , then update the frequency ; Find the index of occurence of the string evenOdd in map ; Update the maximum length ; Print the maximum length ; Driver Code
def longestSubstring ( s ) : NEW_LINE INDENT indexes = { } NEW_LINE chars = { } NEW_LINE chars [ ' a ' ] = 0 NEW_LINE chars [ ' e ' ] = 1 NEW_LINE chars [ ' i ' ] = 2 NEW_LINE chars [ ' o ' ] = 3 NEW_LINE chars [ ' u ' ] = 4 NEW_LINE evenOdd = "00000" NEW_LINE evenOdd = [ i for i in evenOdd ] NEW_LINE indexes [ " " . join ( evenOdd ) ] = - 1 NEW_LINE length = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT c = s [ i ] NEW_LINE if ( c in chars ) : NEW_LINE INDENT evenOdd [ chars [ it ] ] = '1' if evenOdd [ chars [ it ] ] else '0' NEW_LINE DEDENT if ( " " . join ( evenOdd ) not in indexes ) : NEW_LINE INDENT indexes [ " " . join ( evenOdd ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT length = max ( length , i - indexes [ " " . join ( evenOdd ) ] ) NEW_LINE DEDENT DEDENT print ( length ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " bcbcbc " NEW_LINE longestSubstring ( S ) NEW_LINE DEDENT
Minimum number whose binary form is not a subsequence of given binary string | Function to check if string str1 is a subsequence of string str2 ; Store index of str1 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 ; If matched , move ahead in str1 ; If all characters of str1 were found in str2 ; Function to find the minimum number which is not a subsequence of the given binary string in its binary form ; Store the decimal number of string , S ; Initialize the required result ; Iterate in the range [ 0 , R ] ; Convert integer i to binary string ; Check if the string is not a subsequence ; Update ans and break ; Print the required result ; Driver Code ; Function Call ; Function Call
def isSubsequence ( str1 , str2 , m , n ) : NEW_LINE INDENT j = 0 NEW_LINE i = 0 NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( str1 [ j ] == str2 [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( j == m ) NEW_LINE DEDENT def findMinimumNumber ( s ) : NEW_LINE INDENT r = int ( s , 2 ) NEW_LINE ans = r + 1 NEW_LINE for i in range ( r + 1 ) : NEW_LINE INDENT p = " " NEW_LINE j = i NEW_LINE while ( j > 0 ) : NEW_LINE INDENT p += str ( j % 2 ) NEW_LINE j = j // 2 NEW_LINE DEDENT m = len ( p ) NEW_LINE n = len ( s ) NEW_LINE p = p [ : : - 1 ] NEW_LINE if ( isSubsequence ( p , s , m , n ) == False ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "10101" NEW_LINE findMinimumNumber ( s ) NEW_LINE DEDENT
Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings | Function to calculate minimum cost to convert all the characters of S to '1 ; Stores the result ; Stores the number of groups that have 0 as characters ; Traverse the S ; If current character is '0 ; If count is greater than 0 ; Set the count to 0 ; If the last few consecutive characters are '0 ; If contains all characters as '1 ; Minimum Cost ; Driver Code ; Given Input ; Function Call
' NEW_LINE def MinimumCost ( S , A , B ) : NEW_LINE INDENT count = 0 NEW_LINE group = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count > 0 ) : NEW_LINE INDENT group += 1 NEW_LINE DEDENT count = 0 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( count > 0 ) : NEW_LINE INDENT group += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( group == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( min ( A , B ) * ( group - 1 ) + B ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 5 NEW_LINE S = "01100" NEW_LINE MinimumCost ( S , A , B ) NEW_LINE DEDENT
Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
def isPrime ( num ) : NEW_LINE INDENT flag = False NEW_LINE if num > 1 : NEW_LINE INDENT for i in range ( 2 , num ) : NEW_LINE INDENT if ( num % i ) == 0 : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if flag : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def order ( x ) : NEW_LINE INDENT n = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT n = n + 1 NEW_LINE x = x // 10 NEW_LINE DEDENT return n NEW_LINE DEDENT def isArmstrong ( x ) : NEW_LINE INDENT n = order ( x ) NEW_LINE temp = x NEW_LINE sum1 = 0 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT r = temp % 10 NEW_LINE sum1 = sum1 + r ** n NEW_LINE temp = temp // 10 NEW_LINE DEDENT return ( sum1 == x ) NEW_LINE DEDENT def count_armstrong ( li ) : NEW_LINE INDENT c = 0 NEW_LINE for ele in li : NEW_LINE INDENT val = 0 NEW_LINE for che in ele : NEW_LINE INDENT val += ord ( che ) NEW_LINE DEDENT if isArmstrong ( val ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT def count_prime ( li ) : NEW_LINE INDENT c = 0 NEW_LINE for ele in li : NEW_LINE INDENT val = 0 NEW_LINE for che in ele : NEW_LINE INDENT val += ord ( che ) NEW_LINE DEDENT if isPrime ( val ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT arr = [ " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " ] NEW_LINE print ( " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : " , count_armstrong ( arr ) ) NEW_LINE print ( " Number ▁ of ▁ Prime ▁ Strings ▁ are : " , count_prime ( arr ) ) NEW_LINE
Minimize cost of flipping or swaps to make a Binary String balanced | Function to find the minimum cost to convert the given into balanced string ; Stores count of 1 ' s ▁ and ▁ 0' s in the string ; Traverse the string ; Increment count1 ; Increment count 0 ; Stores absolute difference of counts of 0 ' s ▁ and ▁ 1' s ; If consists of only 0 ' s ▁ and ▁ 1' s ; Print the minimum cost ; Driver Code
def findMinimumCost ( s , N ) : NEW_LINE INDENT count_1 , count_0 = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT DEDENT k = abs ( count_0 - count_1 ) NEW_LINE if ( count_1 == N or count_0 == N ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( k // 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "110110" NEW_LINE N = len ( S ) NEW_LINE findMinimumCost ( S , N ) NEW_LINE DEDENT
Minimum time required to complete all tasks with alteration of their order allowed | Python3 program for the above approach ; Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver Code
import sys NEW_LINE from collections import defaultdict NEW_LINE def findMinimumTime ( S , N , K ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT maxfreq = - sys . maxsize NEW_LINE um = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT um [ S [ i ] ] += 1 NEW_LINE if ( um [ S [ i ] ] > maxfreq ) : NEW_LINE INDENT maxfreq = um [ S [ i ] ] NEW_LINE maxchar = S [ i ] NEW_LINE DEDENT DEDENT emptySlots = ( maxfreq - 1 ) * K NEW_LINE for it in um : NEW_LINE INDENT if ( it == maxchar ) : NEW_LINE INDENT continue NEW_LINE DEDENT emptySlots -= min ( um [ it ] , maxfreq - 1 ) NEW_LINE DEDENT ans = N + max ( 0 , emptySlots ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " AAABBB " NEW_LINE K = 2 NEW_LINE N = len ( S ) NEW_LINE findMinimumTime ( S , N , K ) NEW_LINE DEDENT
Check if all characters of a string can be made equal by increments or decrements | Function to check if it is possible to make all characters of string S same or not ; Length of string ; Stores the sum of ASCII value ; Traverse the string S ; Update the weightOfString ; If the sum is divisible by N then pr " Yes " ; Otherwise pr " No " ; Driver Code
def canMakeEqual ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE weightOfString = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT weightOfString += ord ( S [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE DEDENT if ( weightOfString % N == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT S = " beb " NEW_LINE canMakeEqual ( S ) NEW_LINE
Modify a string by circularly shifting each character to the right by respective frequencies | Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
def addFrequencyToCharacter ( S ) : NEW_LINE INDENT frequency = [ 0 for i in range ( 26 ) ] NEW_LINE N = len ( S ) NEW_LINE S = list ( S ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT frequency [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT add = frequency [ ord ( S [ i ] ) - ord ( ' a ' ) ] % 26 NEW_LINE if ord ( S [ i ] ) + add <= ord ( ' z ' ) : NEW_LINE INDENT S [ i ] = chr ( ord ( S [ i ] ) + add ) NEW_LINE DEDENT else : NEW_LINE INDENT add = ord ( S [ i ] ) + add - ord ( ' z ' ) NEW_LINE S [ i ] = chr ( ord ( ' a ' ) + add - 1 ) NEW_LINE DEDENT DEDENT s = " " NEW_LINE print ( s . join ( S ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE addFrequencyToCharacter ( S ) NEW_LINE DEDENT
Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code
def minLength ( S ) : NEW_LINE INDENT st = [ ] NEW_LINE for ch in S : NEW_LINE INDENT if ( len ( st ) == 0 ) : NEW_LINE INDENT st . append ( ch ) NEW_LINE DEDENT else : NEW_LINE INDENT top = st [ - 1 ] NEW_LINE if ( abs ( ord ( ch ) - ord ( top ) ) == 1 ) : NEW_LINE INDENT st . pop ( ) NEW_LINE DEDENT else : NEW_LINE INDENT st . append ( ch ) NEW_LINE DEDENT DEDENT DEDENT return len ( st ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "12213" NEW_LINE print ( minLength ( S ) ) NEW_LINE DEDENT
Minimum time required to complete all tasks without altering their order | Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code
def findMinimumTime ( tasks , K ) : NEW_LINE INDENT map = { } NEW_LINE curr_time = 0 NEW_LINE for c in tasks : NEW_LINE INDENT if ( c in map ) : NEW_LINE INDENT if ( curr_time - map <= K ) : NEW_LINE INDENT curr_time += K - ( curr_time - map ) + 1 NEW_LINE DEDENT DEDENT map = curr_time NEW_LINE curr_time += 1 NEW_LINE DEDENT print ( curr_time ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ABACCA " NEW_LINE K = 2 NEW_LINE findMinimumTime ( S , K ) NEW_LINE DEDENT