codeparrot/xlcost-text-to-code huggingface dataset:
Maximum length of string formed by concatenation having even frequency of each character | Python3 implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
maxi = 0 ; NEW_LINE ans1 = " " ; NEW_LINE def calculate ( ans ) : NEW_LINE INDENT global maxi , ans1 ; NEW_LINE dp = [ 0 ] * 26 ; NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT dp [ ord ( ans [ i ] ) - ord ( ' A ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( dp [ i ] % 2 == 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT DEDENT if ( maxi < len ( ans ) ) : NEW_LINE INDENT maxi = len ( ans ) ; NEW_LINE ans1 = ans ; NEW_LINE DEDENT DEDENT def longestString ( arr , index , string ) : NEW_LINE INDENT if ( index == len ( arr ) ) : NEW_LINE INDENT return ; NEW_LINE DEDENT longestString ( arr , index + 1 , string ) ; NEW_LINE string += arr [ index ] ; NEW_LINE calculate ( string ) ; NEW_LINE longestString ( arr , index + 1 , string ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ " ABAB " , " ABF " , " CDA " , " AD " , " CCC " ] ; NEW_LINE longestString ( A , 0 , " " ) ; NEW_LINE print ( ans1 , len ( ans1 ) ) ; NEW_LINE DEDENT
Find the next number by adding natural numbers in order on alternating indices from last | Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
def generateNumber ( number ) : NEW_LINE INDENT temp = 0 ; adding_number = 0 ; NEW_LINE result = " " ; NEW_LINE l = len ( number ) ; NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT digit = ord ( number [ i ] ) - ord ( '0' ) ; NEW_LINE if ( temp % 2 == 0 ) : NEW_LINE INDENT adding_number += 1 ; NEW_LINE digit += adding_number ; NEW_LINE if ( digit >= 10 ) : NEW_LINE INDENT digit %= 9 ; NEW_LINE if ( digit == 0 ) : NEW_LINE INDENT digit = 9 ; NEW_LINE DEDENT DEDENT DEDENT result = str ( digit ) + result ; NEW_LINE temp += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1345" ; NEW_LINE print ( generateNumber ( S ) ) ; NEW_LINE DEDENT
Find the single digit sum of alphabetical values of a string | Python program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
def findTheSum ( str ) : NEW_LINE INDENT alpha = " " NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if ( ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) or ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) ) : NEW_LINE INDENT alpha += str [ i ] NEW_LINE DEDENT DEDENT score = 0 NEW_LINE n = 0 NEW_LINE for i in range ( 0 , len ( alpha ) ) : NEW_LINE INDENT if ( alpha [ i ] >= ' A ' and alpha [ i ] <= ' Z ' ) : NEW_LINE INDENT score += ord ( alpha [ i ] ) - ord ( ' A ' ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT score += ord ( alpha [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE DEDENT DEDENT while ( score > 0 or n > 9 ) : NEW_LINE INDENT if ( score == 0 ) : NEW_LINE INDENT score = n NEW_LINE n = 0 NEW_LINE DEDENT n += score % 10 NEW_LINE score = score // 10 NEW_LINE DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " GeeksforGeeks " NEW_LINE print ( findTheSum ( S ) ) NEW_LINE DEDENT
Minimum value of K such that each substring of size K has the given character | Function to find the minimum value of K such that char c occurs in all K sized substrings of string S ; Store the string length ; Store difference of lengths of segments of every two consecutive occurrences of c ; Stores the maximum difference ; Store the previous occurence of char c ; Check if the current character is c or not ; Stores the difference of consecutive occurrences of c ; Update previous occurrence of c with current occurence ; Comparing diff with max ; If string doesn 't contain c ; Return max ; Driver Code
def findK ( s , c ) : NEW_LINE INDENT n = len ( s ) NEW_LINE diff = 0 NEW_LINE max = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( s [ i ] == c ) : NEW_LINE INDENT diff = i - prev NEW_LINE prev = i NEW_LINE if ( diff > max ) : NEW_LINE INDENT max = diff NEW_LINE DEDENT DEDENT DEDENT if ( max == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abdegb " NEW_LINE ch = ' b ' NEW_LINE print ( findK ( S , ch ) ) NEW_LINE DEDENT
Count subsequences 01 in string generated by concatenation of given numeric string K times | Function to calculate the number of subsequences of "01" ; Store count of 0 ' s ▁ and ▁ 1' s ; Count of subsequences without concatenation ; Case 1 ; Case 2 ; Return the total count ; Driver Code
def countSubsequence ( S , N , K ) : NEW_LINE INDENT C = 0 NEW_LINE C1 = 0 NEW_LINE C0 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT C1 += 1 NEW_LINE DEDENT elif ( S [ i ] == '0' ) : NEW_LINE INDENT C0 += 1 NEW_LINE DEDENT DEDENT B1 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT B1 += 1 NEW_LINE DEDENT elif ( S [ i ] == '0' ) : NEW_LINE INDENT C = C + ( C1 - B1 ) NEW_LINE DEDENT DEDENT ans = C * K NEW_LINE ans += ( C1 * C0 * ( ( ( K ) * ( K - 1 ) ) // 2 ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "230013110087" NEW_LINE K = 2 NEW_LINE N = len ( S ) NEW_LINE print ( countSubsequence ( S , N , K ) ) NEW_LINE DEDENT
Count of distinct groups of strings formed after performing equivalent operation | Function to perform the find operation to find the parent of a disjoint set ; Function to perform union operation of disjoint set union ; Find the parent of node a and b ; Update the rank ; Function to find the number of distinct strings after performing the given operations ; Stores the parent elements of the sets ; Stores the rank of the sets ; Update parent [ i ] to i ; Stores the total characters traversed through the strings ; Stores the current characters traversed through a string ; Update current [ i ] to false ; Update current [ ch - ' a ' ] to true ; Check if current [ j ] is true ; Update total [ j ] to true ; Add arr [ i ] [ 0 ] - ' a ' and j elements to same set ; Stores the count of distinct strings ; Check total [ i ] is true and parent of i is i only ; Increment the value of distCount by 1 ; Print the value of distCount ; Driver Code
def Find ( parent , a ) : NEW_LINE INDENT if parent [ a ] == a : NEW_LINE INDENT parent [ a ] = a NEW_LINE return parent [ a ] NEW_LINE DEDENT else : NEW_LINE INDENT parent [ a ] = Find ( parent , parent [ a ] ) NEW_LINE return parent [ a ] NEW_LINE DEDENT DEDENT def Union ( parent , rank , a , b ) : NEW_LINE INDENT a = Find ( parent , a ) NEW_LINE b = Find ( parent , b ) NEW_LINE if ( rank [ a ] == rank [ b ] ) : NEW_LINE INDENT rank [ a ] += 1 NEW_LINE DEDENT if ( rank [ a ] > rank [ b ] ) : NEW_LINE INDENT parent [ b ] = a NEW_LINE DEDENT else : NEW_LINE INDENT parent [ a ] = b NEW_LINE DEDENT DEDENT def numOfDistinctStrings ( arr , N ) : NEW_LINE INDENT parent = [ 0 for _ in range ( 27 ) ] NEW_LINE rank = [ 0 for _ in range ( 27 ) ] NEW_LINE for j in range ( 0 , 27 ) : NEW_LINE INDENT parent [ j ] = j NEW_LINE DEDENT total = [ False for _ in range ( 26 ) ] NEW_LINE current = [ False for _ in range ( 26 ) ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT current [ j ] = False NEW_LINE DEDENT for ch in arr [ i ] : NEW_LINE INDENT current [ ord ( ch ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT if ( current [ j ] ) : NEW_LINE INDENT total [ j ] = True NEW_LINE Union ( parent , rank , ord ( arr [ i ] [ 0 ] ) - ord ( ' a ' ) , j ) NEW_LINE DEDENT DEDENT DEDENT distCount = 0 NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT if ( total [ i ] and Find ( parent , i ) == i ) : NEW_LINE INDENT distCount += 1 NEW_LINE DEDENT DEDENT print ( distCount ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " a " , " ab " , " b " , " d " ] NEW_LINE N = len ( arr ) NEW_LINE numOfDistinctStrings ( arr , N ) NEW_LINE DEDENT
Minimum adjacent swaps required to make a binary string alternating | Function to find the minimum number of adjacent swaps to make the string alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when string starts with "1" ; Keep track of the odd positions ; Checking for when the string starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when string starts with "0" ; Keep track of the odd positions ; Checking for when the string starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when string length is even ; When string length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code
def minSwaps ( s ) : NEW_LINE INDENT ones = 0 NEW_LINE zeros = 0 NEW_LINE N = len ( s ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if s [ i ] == '1' : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT DEDENT if ( ( N % 2 == 0 and ones != zeros ) or ( N % 2 == 1 and abs ( ones - zeros ) != 1 ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans_1 = 0 NEW_LINE j = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ans_1 += abs ( j - i ) NEW_LINE j += 2 NEW_LINE DEDENT DEDENT ans_0 = 0 NEW_LINE k = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT ans_0 += abs ( k - i ) NEW_LINE k += 2 NEW_LINE DEDENT DEDENT if ( N % 2 == 0 ) : NEW_LINE INDENT return min ( ans_1 , ans_0 ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( ones > zeros ) : NEW_LINE INDENT return ans_1 NEW_LINE DEDENT else : NEW_LINE INDENT return ans_0 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "110100" NEW_LINE print ( minSwaps ( S ) ) NEW_LINE DEDENT
Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
def TotalPossibleJumps ( N ) : NEW_LINE INDENT if ( ( N - 1 ) == 0 ) : NEW_LINE INDENT newvec = [ ] NEW_LINE newvec . append ( " " ) NEW_LINE return newvec NEW_LINE DEDENT else : NEW_LINE INDENT if ( N < 0 ) : NEW_LINE INDENT newvec = [ ] NEW_LINE return newvec NEW_LINE DEDENT DEDENT jump1 = TotalPossibleJumps ( N - 1 ) NEW_LINE jump2 = TotalPossibleJumps ( N - 2 ) NEW_LINE totaljumps = [ ] NEW_LINE for s in jump1 : NEW_LINE INDENT totaljumps . append ( "1" + s ) NEW_LINE DEDENT for s in jump2 : NEW_LINE INDENT totaljumps . append ( "2" + s ) NEW_LINE DEDENT return totaljumps NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE Ans = TotalPossibleJumps ( N ) NEW_LINE for it in Ans : NEW_LINE INDENT print ( it ) NEW_LINE DEDENT DEDENT
Find any permutation of Binary String of given size not present in Array | Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code
def findString ( arr , N ) : NEW_LINE INDENT ans = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans += '1' if arr [ i ] [ i ] == '0' else '0' NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ "111" , "011" , "001" ] NEW_LINE N = len ( arr ) NEW_LINE print ( findString ( arr , N ) ) NEW_LINE DEDENT
Check if the string has a reversible equal substring at the ends | Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
def commonSubstring ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE ans = " " NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( len ( ans ) == 0 ) : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " True " ) NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abca " NEW_LINE commonSubstring ( S ) NEW_LINE DEDENT
Length of Smallest Non Prime Subsequence in given numeric String | Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
def findMinimumSubsequence ( S ) : NEW_LINE INDENT flag = False NEW_LINE dummy = ' ' NEW_LINE for j in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ j ] != '2' and S [ j ] != '3' and S [ j ] != '5' and S [ j ] != '7' ) : NEW_LINE INDENT print ( 1 ) NEW_LINE flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT for j in range ( len ( S ) ) : NEW_LINE INDENT for j1 in range ( j + 1 , len ( S ) , 1 ) : NEW_LINE INDENT dummy = S [ j ] + S [ j1 ] NEW_LINE if ( dummy != "23" and dummy != "37" and dummy != "53" and dummy != "73" ) : NEW_LINE INDENT print ( 2 ) NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT if ( len ( S ) >= 3 ) : NEW_LINE INDENT print ( 3 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "237" NEW_LINE findMinimumSubsequence ( S ) NEW_LINE DEDENT
Minimize changes to make all characters equal by changing vowel to consonant and vice versa | Python 3 program for the above approach ; Function to find the minimum number of steps to make all characters equal ; Initializing the variables ; Store the frequency ; Iterate over the string ; Calculate the total number of vowels ; Storing frequency of each vowel ; Count the consonants ; Storing the frequency of each consonant ; Iterate over the 2 maps ; Maximum frequency of consonant ; Maximum frequency of vowel ; Find the result ; Driver Code
import sys NEW_LINE from collections import defaultdict NEW_LINE def operations ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE vowels = 0 NEW_LINE consonants = 0 NEW_LINE max_vowels = - sys . maxsize - 1 NEW_LINE max_consonants = - sys . maxsize - 1 NEW_LINE freq_consonants = defaultdict ( int ) NEW_LINE freq_vowels = defaultdict ( int ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT vowels += 1 NEW_LINE freq_vowels [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT consonants += 1 NEW_LINE freq_consonants [ s [ i ] ] += 1 NEW_LINE DEDENT DEDENT for it in freq_consonants : NEW_LINE INDENT max_consonants = max ( max_consonants , freq_consonants [ it ] ) NEW_LINE DEDENT for it in freq_vowels : NEW_LINE INDENT max_vowels = max ( max_vowels , freq_vowels [ it ] ) NEW_LINE DEDENT ans = min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE print ( operations ( S ) ) NEW_LINE DEDENT
Count of ways to empty given String by recursively removing all adjacent duplicates | Python3 implementation for the above approach ; Define the dp table globally ; Recursive function to calculate the dp values for range [ L , R ] ; The range is odd length ; The state is already calculated ; If the length is 2 ; Total answer for this state ; Variable to store the current answer . ; Remove characters s [ l ] and s [ i ] . ; Initialize all the states of dp to - 1 memset ( dp , - 1 , sizeof ( dp ) ) ; ; Calculate all Combinations ; Driver Code
import numpy as np NEW_LINE dp = np . zeros ( ( 505 , 505 ) ) ; NEW_LINE choose = np . zeros ( ( 502 , 502 ) ) ; NEW_LINE def calc ( l , r , s ) : NEW_LINE INDENT if ( abs ( r - l ) % 2 == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( l > r ) : NEW_LINE INDENT dp [ l ] [ r ] = 1 ; NEW_LINE return dp [ l ] [ r ] NEW_LINE DEDENT if ( dp [ l ] [ r ] != - 1 ) : NEW_LINE INDENT return dp [ l ] [ r ] ; NEW_LINE DEDENT if ( ( r - l ) == 1 ) : NEW_LINE INDENT if ( s [ l ] == s [ r ] ) : NEW_LINE INDENT dp [ l ] [ r ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ l ] [ r ] = 0 ; NEW_LINE DEDENT return dp [ l ] [ r ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for k in range ( l + 1 , r + 1 , 2 ) : NEW_LINE INDENT temp = 1 ; NEW_LINE if ( s [ l ] == s [ k ] ) : NEW_LINE INDENT temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ ( r - l + 1 ) // 2 ] [ ( r - k ) // 2 ] ; NEW_LINE ans += temp ; NEW_LINE DEDENT DEDENT dp [ l ] [ r ] = ans ; NEW_LINE return dp [ l ] [ r ] NEW_LINE DEDENT def waysToClearString ( S ) : NEW_LINE INDENT for i in range ( 505 ) : NEW_LINE INDENT for j in range ( 505 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT n = len ( S ) ; NEW_LINE choose [ 0 ] [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , ( n // 2 ) + 1 ) : NEW_LINE INDENT choose [ i ] [ 0 ] = 1 ; NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT choose [ i ] [ j ] = choose [ i - 1 ] [ j ] + choose [ i - 1 ] [ j - 1 ] ; NEW_LINE DEDENT DEDENT return calc ( 0 , n - 1 , S ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aabccb " ; NEW_LINE print ( waysToClearString ( S ) ) ; NEW_LINE DEDENT
Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | Function to check for valid subsequences ; Stores which element belongs to which subsequence ; Check for each pos if a possible subsequence exist or not ; Last member of 1 subsequence ; Last Member of 2 nd subsequence ; Check if current element can go to 2 nd subsequence ; Check if the current elements belongs to first subsequence ; If the current element does not belong to any subsequence ; Check if last digit of first subsequence is greater than pos ; If a subsequence is found , find the subsequences ; Stores the resulting subsequences ; Print the subsequence ; If no subsequence found , print - 1 ; Driver Code
def findSubsequence ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE res = [ '0' for i in range ( n ) ] NEW_LINE for pos in range ( 10 ) : NEW_LINE INDENT lst1 = '0' NEW_LINE flag = 1 NEW_LINE lst2 = chr ( pos + 48 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( lst2 <= str [ i ] ) : NEW_LINE INDENT res [ i ] = '2' NEW_LINE lst2 = str [ i ] NEW_LINE DEDENT elif ( lst1 <= str [ i ] ) : NEW_LINE INDENT res [ i ] = '1' NEW_LINE lst1 = str [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( lst1 > chr ( pos + 48 ) ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT S1 = " " NEW_LINE S2 = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( res [ i ] == '1' ) : NEW_LINE INDENT S1 += str [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT S2 += str [ i ] NEW_LINE DEDENT DEDENT print ( S1 , S2 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "040425524644" NEW_LINE findSubsequence ( S ) NEW_LINE S = "123456789" NEW_LINE findSubsequence ( S ) NEW_LINE DEDENT
Find Kth lexicographical ordered numeric string of length N with distinct products of each substring | Java program for the above approach ; Function to find the required String ; If the current length is equal to n exit from here only ; Iterate for all the characters ; Check if the product is present before ; If the current String is good then recurse for the next character ; Decrease all the products back to their original state ; Erase the last character ; Function to calculate kth ordered valid String ; Check for the base cases ; There are atmost 10 valid Strings for n = 1 ; Vector to keep a check on number of occurences of products ; Recursively construct the Strings ; Driver Code
s = ' ' NEW_LINE K = 0 NEW_LINE def getString ( curlen , N , prod ) : NEW_LINE INDENT global s , K NEW_LINE if ( curlen == N ) : NEW_LINE INDENT K -= 1 NEW_LINE if ( K == 0 ) : NEW_LINE INDENT ans = s NEW_LINE DEDENT return NEW_LINE DEDENT ch = '2' NEW_LINE while ( ch <= '9' ) : NEW_LINE INDENT s = chr ( ord ( s ) + ord ( ch ) ) NEW_LINE ok = 1 NEW_LINE t = 1 NEW_LINE i = curlen NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE while ( i >= 0 and len ( s ) ) : NEW_LINE INDENT t *= ord ( s [ i ] ) - 48 NEW_LINE if ( prod [ t ] != 0 ) : NEW_LINE INDENT ok = 0 NEW_LINE DEDENT prod [ t ] += 1 NEW_LINE i -= 1 NEW_LINE DEDENT if ( ok != 0 ) : NEW_LINE INDENT getString ( curlen + 1 , N , prod ) NEW_LINE DEDENT t = 1 NEW_LINE i = curlen NEW_LINE while ( i >= 0 and len ( s ) > i ) : NEW_LINE INDENT i -= 1 NEW_LINE t *= ord ( s [ i ] ) - 48 NEW_LINE prod [ t ] -= 1 NEW_LINE DEDENT if ( len ( s ) > 0 ) : NEW_LINE INDENT s = s [ 0 : len ( s ) - 1 ] NEW_LINE DEDENT DEDENT DEDENT def kthValidString ( N ) : NEW_LINE INDENT if ( N > 10 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT if ( N == 1 ) : NEW_LINE INDENT if ( K > 10 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT s = " " NEW_LINE K -= 1 NEW_LINE s += ( K + '0' ) NEW_LINE return s NEW_LINE DEDENT ans = " - 1" NEW_LINE s = " " NEW_LINE prod = [ 0 ] * 10005 NEW_LINE getString ( 0 , N , prod ) NEW_LINE return ans NEW_LINE DEDENT N = 3 NEW_LINE K = 4 NEW_LINE print ( kthValidString ( N ) ) NEW_LINE
Minimum swaps to balance the given brackets at any index | Function to balance the given bracket by swap ; To count the number of uunbalanced pairs ; if there is an opening bracket and we encounter closing bracket then it will decrement the count of unbalanced bracket . ; else it will increment unbalanced pair count ; Driver code
def BalancedStringBySwapping ( s ) : NEW_LINE INDENT unbalancedPair = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( unbalancedPair > 0 and s [ i ] == ' ] ' ) : NEW_LINE INDENT unbalancedPair -= 1 ; NEW_LINE DEDENT elif ( s [ i ] == ' [ ' ) : NEW_LINE INDENT unbalancedPair += 1 ; NEW_LINE DEDENT DEDENT return ( unbalancedPair + 1 ) // 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : s = " ] ] ] [ [ [ " ; NEW_LINE INDENT print ( BalancedStringBySwapping ( s ) ) ; NEW_LINE DEDENT