codeparrot/xlcost-text-to-code huggingface dataset:
Find the missing digit in given product of large positive integers | Function to find the replaced digit in the product of a * b ; Keeps track of the sign of the current digit ; Stores the value of a % 11 ; Find the value of a mod 11 for large value of a as per the derived formula ; Stores the value of b % 11 ; Find the value of b mod 11 for large value of a as per the derived formula ; Stores the value of c % 11 ; Keeps track of the sign of x ; If the current digit is the missing digit , then keep the track of its sign ; Find the value of x using the derived equation ; Check if x has a negative sign ; Return positive equivaluent of x mod 11 ; Driver Code
def findMissingDigit ( a , b , c ) : NEW_LINE INDENT w = 1 NEW_LINE a_mod_11 = 0 NEW_LINE for i in range ( len ( a ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT a_mod_11 = ( a_mod_11 + w * ( ord ( a [ i ] ) - ord ( '0' ) ) ) % 11 NEW_LINE w = w * - 1 NEW_LINE DEDENT b_mod_11 = 0 NEW_LINE w = 1 NEW_LINE for i in range ( len ( b ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT b_mod_11 = ( b_mod_11 + w * ( ord ( b [ i ] ) - ord ( '0' ) ) ) % 11 NEW_LINE w = w * - 1 NEW_LINE DEDENT c_mod_11 = 0 NEW_LINE xSignIsPositive = True NEW_LINE w = 1 NEW_LINE for i in range ( len ( c ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( c [ i ] == ' x ' ) : NEW_LINE INDENT xSignIsPositive = ( w == 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT c_mod_11 = ( c_mod_11 + w * ( ord ( c [ i ] ) - ord ( '0' ) ) ) % 11 NEW_LINE DEDENT w = w * - 1 NEW_LINE DEDENT x = ( ( a_mod_11 * b_mod_11 ) - c_mod_11 ) % 11 NEW_LINE if ( not xSignIsPositive ) : NEW_LINE INDENT x = - x NEW_LINE DEDENT return ( x % 11 + 11 ) % 11 NEW_LINE DEDENT A = "123456789" NEW_LINE B = "987654321" NEW_LINE C = "12193263111x635269" NEW_LINE print ( findMissingDigit ( A , B , C ) ) NEW_LINE
Check if a string can be made empty by repeatedly removing given subsequence | Function to check if a string can be made empty by removing all subsequences of the form " GFG " or not ; Driver Code
def findIfPossible ( N , str_ ) : NEW_LINE INDENT countG = 0 NEW_LINE countF = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if str_ [ i ] == ' G ' : NEW_LINE INDENT countG += 1 NEW_LINE DEDENT else : NEW_LINE INDENT countF += 1 NEW_LINE DEDENT DEDENT if 2 * countF != countG : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT id = 0 NEW_LINE flag = True NEW_LINE for i in range ( N ) : NEW_LINE INDENT if str_ [ i ] == ' G ' : NEW_LINE INDENT countG -= 1 NEW_LINE id += 1 NEW_LINE DEDENT else : NEW_LINE INDENT countF -= 1 NEW_LINE id -= 1 NEW_LINE DEDENT if id < 0 : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT if countG < countF : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if flag : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT DEDENT n = 6 NEW_LINE str_ = " GFGFGG " NEW_LINE findIfPossible ( n , str_ ) NEW_LINE
Check whether second string can be formed from characters of first string used any number of times | Function to check if str2 can be made by characters of str1 or not ; To store the occurrence of every character ; Length of the two strings ; Assume that it is possible to compose the string str2 from str1 ; Iterate over str1 ; Store the presence or every element ; Iterate over str2 ; Ignore the spaces ; Check for the presence of character in str1 ; If it is possible to make str2 from str1 ; Given strings ; Function call .
def isPossible ( str1 , str2 ) : NEW_LINE INDENT arr = { } NEW_LINE l1 = len ( str1 ) NEW_LINE l2 = len ( str2 ) NEW_LINE possible = True NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT arr [ str1 [ i ] ] = 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT if str2 [ i ] != ' ▁ ' : NEW_LINE INDENT if arr [ str2 [ i ] ] == 1 : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT possible = False NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if possible : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT str1 = " we ▁ all ▁ love ▁ geeksforgeeks " NEW_LINE str2 = " we ▁ all ▁ love ▁ geeks " NEW_LINE isPossible ( str1 , str2 ) NEW_LINE
Minimum number of flipping adjacent bits required to make given Binary Strings equal | Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
def find_Min_Inversion ( n , s1 , s2 ) : NEW_LINE INDENT count = 0 NEW_LINE s1 = list ( s1 ) NEW_LINE s2 = list ( s2 ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT if ( s1 [ i ] == '1' ) : NEW_LINE INDENT s1 [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT s1 [ i ] = '1' NEW_LINE DEDENT if ( s1 [ i + 1 ] == '1' ) : NEW_LINE INDENT s1 [ i + 1 ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT s1 [ i + 1 ] = '1' NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT s1 = ' ' . join ( s1 ) NEW_LINE s2 = ' ' . join ( s2 ) NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT return count NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE s1 = "0101" NEW_LINE s2 = "1111" NEW_LINE print ( find_Min_Inversion ( n , s1 , s2 ) ) NEW_LINE DEDENT
Longest subsequence with consecutive English alphabets | Function to find the length of subsequence starting with character ch ; Length of the string ; Stores the maximum length ; Traverse the given string ; If s [ i ] is required character ch ; Increment ans by 1 ; Increment character ch ; Return the current maximum length with character ch ; Function to find the maximum length of subsequence of consecutive characters ; Stores the maximum length of consecutive characters ; Update ans ; Return the maximum length of subsequence ; Driver Code ; Input ; Function Call
def findSubsequence ( S , ch ) : NEW_LINE INDENT N = len ( S ) NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ch ) : NEW_LINE INDENT ans += 1 NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def findMaxSubsequence ( S ) : NEW_LINE INDENT ans = 0 NEW_LINE for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT ans = max ( ans , findSubsequence ( S , chr ( ch ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcabefghijk " NEW_LINE print ( findMaxSubsequence ( S ) ) NEW_LINE DEDENT
Minimum number of alternate subsequences required to be removed to empty a Binary String | Function to find the minimum number of operations to empty a binary string ; Stores the resultant number of operations ; Stores the number of 0 s ; Stores the number of 1 s ; Traverse the given string ; To balance 0 with 1 if possible ; Increment the value of cn0 by 1 ; To balance 1 with 0 if possible ; Increment the value of cn1 ; Update the maximum number of unused 0 s and 1 s ; Print resultant count ; Driver Code
def minOpsToEmptyString ( s ) : NEW_LINE INDENT ans = - 10 ** 9 NEW_LINE cn0 = 0 NEW_LINE cn1 = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT if ( cn1 > 0 ) : NEW_LINE INDENT cn1 -= 1 NEW_LINE DEDENT cn0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( cn0 > 0 ) : NEW_LINE INDENT cn0 -= 1 NEW_LINE DEDENT cn1 += 1 NEW_LINE DEDENT ans = max ( [ ans , cn0 , cn1 ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "010101" NEW_LINE minOpsToEmptyString ( S ) NEW_LINE DEDENT
Longest Non | Function to find the length of the longest non - increasing subsequence ; Stores the prefix and suffix count of 1 s and 0 s respectively ; Store the number of '1' s up to current index i in pre ; Find the prefix sum ; If the current element is '1' , update the pre [ i ] ; Store the number of '0' s over the range [ i , N - 1 ] ; Find the suffix sum ; If the current element is '0' , update post [ i ] ; Stores the maximum length ; Find the maximum value of pre [ i ] + post [ i ] ; Return the answer ; Driver Code
def findLength ( str , n ) : NEW_LINE INDENT pre = [ 0 ] * n NEW_LINE post = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT pre [ i ] += pre [ i - 1 ] NEW_LINE DEDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT pre [ i ] += 1 NEW_LINE DEDENT DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i != ( n - 1 ) ) : NEW_LINE INDENT post [ i ] += post [ i + 1 ] NEW_LINE DEDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT post [ i ] += 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , pre [ i ] + post [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT S = "0101110110100001011" NEW_LINE n = len ( S ) NEW_LINE print ( findLength ( S , n ) ) NEW_LINE
Number of substrings having an equal number of lowercase and uppercase letters | Function to find the count of substrings having an equal number of uppercase and lowercase characters ; Stores the count of prefixes having sum S considering uppercase and lowercase characters as 1 and - 1 ; Stores the count of substrings having equal number of lowercase and uppercase characters ; Stores the sum obtained so far ; If the character is uppercase ; Otherwise ; If currsum is o ; If the current sum exists in the HashMap prevSum ; Increment the resultant count by 1 ; Update the frequency of the current sum by 1 ; Return the resultant count of the subarrays ; Driver Code
def countSubstring ( S , N ) : NEW_LINE INDENT prevSum = { } NEW_LINE res = 0 NEW_LINE currentSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] >= ' A ' and S [ i ] <= ' Z ' ) : NEW_LINE INDENT currentSum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT currentSum -= 1 NEW_LINE DEDENT if ( currentSum == 0 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( currentSum in prevSum ) : NEW_LINE INDENT res += ( prevSum [ currentSum ] ) NEW_LINE DEDENT if currentSum in prevSum : NEW_LINE INDENT prevSum [ currentSum ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prevSum [ currentSum ] = 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " gEEk " NEW_LINE print ( countSubstring ( S , len ( S ) ) ) NEW_LINE DEDENT
Number of substrings with each character occurring even times | Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code
def subString ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for len in range ( i + 1 , n + 1 ) : NEW_LINE INDENT test_str = ( s [ i : len ] ) NEW_LINE res = { } NEW_LINE for keys in test_str : NEW_LINE INDENT res [ keys ] = res . get ( keys , 0 ) + 1 NEW_LINE DEDENT flag = 0 NEW_LINE for keys in res : NEW_LINE INDENT if res [ keys ] % 2 != 0 : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if flag == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT S = " abbaa " NEW_LINE N = len ( S ) NEW_LINE print ( subString ( S , N ) ) NEW_LINE
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Function to count new pairs of strings that can be obtained by swapping first characters of any pair of strings ; Stores the count of pairs ; Generate all possible pairs of strings from the array arr [ ] ; Stores the current pair of strings ; Swap the first characters ; Check if they are already present in the array or not ; If both the strings are not present ; Increment the ans by 1 ; Print the resultant count ; Driver Code
def countStringPairs ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT p = a [ i ] NEW_LINE q = a [ j ] NEW_LINE if ( p [ 0 ] != q [ 0 ] ) : NEW_LINE INDENT p = list ( p ) NEW_LINE q = list ( q ) NEW_LINE temp = p [ 0 ] NEW_LINE p [ 0 ] = q [ 0 ] NEW_LINE q [ 0 ] = temp NEW_LINE p = ' ' . join ( p ) NEW_LINE q = ' ' . join ( q ) NEW_LINE flag1 = 0 NEW_LINE flag2 = 0 NEW_LINE for k in range ( n ) : NEW_LINE INDENT if ( a [ k ] == p ) : NEW_LINE INDENT flag1 = 1 NEW_LINE DEDENT if ( a [ k ] == q ) : NEW_LINE INDENT flag2 = 1 NEW_LINE DEDENT DEDENT if ( flag1 == 0 and flag2 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " good " , " bad " , " food " ] NEW_LINE N = len ( arr ) NEW_LINE countStringPairs ( arr , N ) NEW_LINE DEDENT
Check if it is possible to reach any point on the circumference of a given circle from origin | Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , 'R ; Stores the count of ' U ' , 'D ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code
def isPossible ( S , R , N ) : NEW_LINE ' NEW_LINE INDENT cntl = 0 NEW_LINE cntr = 0 NEW_LINE DEDENT ' NEW_LINE INDENT cntu = 0 NEW_LINE cntd = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' L ' ) : NEW_LINE INDENT cntl += 1 NEW_LINE DEDENT elif ( S [ i ] == ' R ' ) : NEW_LINE INDENT cntr += 1 NEW_LINE DEDENT elif ( S [ i ] == ' U ' ) : NEW_LINE INDENT cntu += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntd += 1 NEW_LINE DEDENT DEDENT if ( max ( max ( cntl , cntr ) , max ( cntu , cntd ) ) >= R ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT mp = { } NEW_LINE r_square = R * R NEW_LINE i = 1 NEW_LINE while i * i <= r_square : NEW_LINE INDENT mp [ i * i ] = i NEW_LINE if ( ( r_square - i * i ) in mp ) : NEW_LINE INDENT if ( max ( cntl , cntr ) >= mp [ r_square - i * i ] and max ( cntu , cntd ) >= i ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT if ( max ( cntl , cntr ) >= i and max ( cntu , cntd ) >= mp [ r_square - i * i ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " RDLLDDLDU " NEW_LINE R = 5 NEW_LINE N = len ( S ) NEW_LINE print ( isPossible ( S , R , N ) ) NEW_LINE DEDENT
Modify characters of a string by adding integer values of same | Function to modify a given string by adding ASCII value of characters from a string S to integer values of same indexed characters in string N ; Traverse the string ; Stores integer value of character in string N ; Stores ASCII value of character in string S ; If b exceeds 122 ; Replace the character ; Print resultant string ; Driver Code ; Given strings ; Function call to modify string S by given operations
def addASCII ( S , N ) : NEW_LINE INDENT for i in range ( len ( S ) ) : NEW_LINE INDENT a = ord ( N [ i ] ) - ord ( '0' ) NEW_LINE b = ord ( S [ i ] ) + a NEW_LINE if ( b > 122 ) : NEW_LINE INDENT b -= 26 NEW_LINE DEDENT S = S . replace ( S [ i ] , chr ( b ) ) NEW_LINE DEDENT print ( S ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " sun " NEW_LINE N = "966" NEW_LINE addASCII ( S , N ) NEW_LINE DEDENT
Modify array by removing characters from their Hexadecimal representations which are present in a given string | Function to convert a decimal number to its equivalent hexadecimal number ; Function to convert hexadecimal number to its equavalent decimal number ; Stores characters with their respective hexadecimal values ; Stores answer ; Traverse the string ; If digit ; If character ; Return the answer ; Function to move all the alphabets to front ; Function to modify each array element by removing characters from their hexadecimal representation which are present in a given string ; Traverse the array ; Stores hexadecimal value ; Remove the characters from hexadecimal representation present in string S ; Stores decimal value ; Replace array element ; Print the modified array ; Given array ; Given string ; Function call to modify array by given operations
def decHex ( n ) : NEW_LINE INDENT alpha = [ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' ] NEW_LINE ans = ' ' NEW_LINE while n : NEW_LINE INDENT if n % 16 < 10 : NEW_LINE INDENT ans += str ( n % 16 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += alpha [ n % 16 - 10 ] NEW_LINE DEDENT n //= 16 NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE return ans NEW_LINE DEDENT def hexDec ( convertedHex ) : NEW_LINE INDENT mp = { " A " : 10 , " B " : 11 , " C " : 12 , " D " : 13 , " E " : 14 , " F " : 15 } NEW_LINE ans = 0 NEW_LINE pos = 0 NEW_LINE for i in convertedHex [ : : - 1 ] : NEW_LINE INDENT if i . isdigit ( ) : NEW_LINE INDENT ans += ( 16 ** pos ) * int ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( 16 ** pos ) * mp [ i ] NEW_LINE DEDENT pos += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def removeChars ( hexaVal , S ) : NEW_LINE INDENT setk = set ( ) NEW_LINE for i in S : NEW_LINE INDENT setk . add ( i ) NEW_LINE DEDENT ans = ' ' NEW_LINE for i in hexaVal : NEW_LINE INDENT if i in setk : NEW_LINE INDENT continue NEW_LINE DEDENT ans += i NEW_LINE DEDENT return ans NEW_LINE DEDENT def convertArr ( arr , S ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT hexaVal = decHex ( arr [ i ] ) NEW_LINE convertedHex = removeChars ( hexaVal , S ) NEW_LINE decVal = hexDec ( convertedHex ) NEW_LINE arr [ i ] = decVal NEW_LINE DEDENT print ( arr ) NEW_LINE DEDENT arr = [ 74 , 91 , 31 , 122 ] NEW_LINE S = "1AB " NEW_LINE convertArr ( arr , S ) NEW_LINE
Modify string by inserting characters such that every K | Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver Code
def fillString ( s , k ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] != ' ? ' ) : NEW_LINE INDENT mp [ i % k ] = s [ i ] NEW_LINE DEDENT DEDENT s = list ( s ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( i % k ) not in mp ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT s [ i ] = mp [ i % k ] NEW_LINE DEDENT s = ' ' . join ( s ) NEW_LINE print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ? ? ? ? abcd " NEW_LINE K = 4 NEW_LINE fillString ( S , K ) NEW_LINE DEDENT
Rearrange a string S1 such that another given string S2 is not its subsequence | Function to rearrange characters in S1 such that S2 is not a subsequence of it ; Store the frequencies of characters of s2 ; Traverse the s2 ; Update the frequency ; Find the number of unique characters in s2 ; Increment unique by 1 if the condition satisfies ; Check if the number of unique characters in s2 is 1 ; Store the unique character frequency ; Store occurence of it in s1 ; Find count of that character in the s1 ; Increment count by 1 if that unique character is same as current character ; If count count_in_s1 is less than count_in_s2 , then prs1 and return ; Otherwise , there is no possible arrangement ; Checks if any character in s2 is less than its next character ; Iterate the string , s2 ; If s [ i ] is greater than the s [ i + 1 ] ; Set inc to 0 ; If inc = 1 , prs1 in decreasing order ; Otherwise , prs1 in increasing order ; Driver code
def rearrangeString ( s1 , s2 ) : NEW_LINE INDENT cnt = [ 0 ] * 26 NEW_LINE for i in range ( len ( s2 ) ) : NEW_LINE INDENT cnt [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT unique = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( cnt [ i ] != 0 ) : NEW_LINE INDENT unique += 1 NEW_LINE DEDENT DEDENT if ( unique == 1 ) : NEW_LINE INDENT count_in_s2 = len ( s2 ) NEW_LINE count_in_s1 = 0 NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == s2 [ 0 ] ) : NEW_LINE INDENT count_in_s1 += 1 NEW_LINE DEDENT DEDENT if ( count_in_s1 < count_in_s2 ) : NEW_LINE INDENT print ( s1 , end = " " ) NEW_LINE return NEW_LINE DEDENT print ( - 1 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT inc = 1 NEW_LINE for i in range ( len ( s2 ) - 1 ) : NEW_LINE INDENT if ( s2 [ i ] > s2 [ i + 1 ] ) : NEW_LINE INDENT inc = 0 NEW_LINE DEDENT DEDENT if ( inc == 1 ) : NEW_LINE INDENT s1 = sorted ( s1 ) [ : : - 1 ] NEW_LINE print ( " " . join ( s1 ) , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT s1 = sorted ( s1 ) NEW_LINE print ( " " . join ( s1 ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 , s2 = " abcd " , " ab " NEW_LINE rearrangeString ( s1 , s2 ) NEW_LINE DEDENT
Check if a string can be emptied by removing all subsequences of the form "10" | Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver code
def isReducible ( Str ) : NEW_LINE INDENT N = len ( Str ) NEW_LINE s = [ ] NEW_LINE for i in range ( N ) : NEW_LINE if ( Str [ i ] == '1' ) : NEW_LINE INDENT s . append ( Str [ i ] ) NEW_LINE DEDENT elif ( len ( s ) > 0 ) : NEW_LINE INDENT del s [ len ( s ) - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT if ( len ( s ) == 0 ) : NEW_LINE return True NEW_LINE else : NEW_LINE return False NEW_LINE DEDENT Str = "11011000" NEW_LINE if ( isReducible ( Str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Rearrange a string to maximize the minimum distance between any pair of vowels | Function to rearrange the string such that the minimum distance between any of vowels is maximum . ; store vowels and consonants ; Iterate over the characters of string ; if current character is a vowel ; if current character is consonant ; store count of vowels and consonants respectively ; store the resultant string ; store count of consonants append into ans ; Append vowel to ans ; Append consonants ; Appendconsonant to ans ; update temp ; Remove the taken elements of consonant ; return final answer ; Driver code ; Function Call
def solution ( S ) : NEW_LINE INDENT vowels = [ ] NEW_LINE consonants = [ ] NEW_LINE for i in S : NEW_LINE INDENT if ( i == ' a ' or i == ' e ' or i == ' i ' or i == ' o ' or i == ' u ' ) : NEW_LINE INDENT vowels . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT consonants . append ( i ) NEW_LINE DEDENT DEDENT Nc = len ( consonants ) NEW_LINE Nv = len ( vowels ) NEW_LINE M = Nc // ( Nv - 1 ) NEW_LINE ans = " " NEW_LINE consonant_till = 0 NEW_LINE for i in vowels : NEW_LINE INDENT ans += i NEW_LINE temp = 0 NEW_LINE for j in range ( consonant_till , min ( Nc , consonant_till + M ) ) : NEW_LINE INDENT ans += consonants [ j ] NEW_LINE temp += 1 NEW_LINE DEDENT consonant_till += temp NEW_LINE DEDENT return ans NEW_LINE DEDENT S = " aaaabbbcc " NEW_LINE print ( solution ( S ) ) NEW_LINE