codeparrot/xlcost-text-to-code huggingface dataset:
Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | Java program for the above approach ; 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
class GFG { static void findSubsequence ( String str ) { int n = str . length ( ) ; char [ ] res = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( int pos = 0 ; pos <= 9 ; pos ++ ) { char lst1 = '0' ; boolean flag = true ; char lst2 = ( char ) ( pos + '0' ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( lst2 <= str . charAt ( i ) ) { res [ i ] = '2' ; lst2 = str . charAt ( i ) ; } else if ( lst1 <= str . charAt ( i ) ) { res [ i ] = '1' ; lst1 = str . charAt ( i ) ; } else flag = false ; } if ( lst1 > pos + '0' ) flag = false ; if ( flag ) { String S1 = " " ; String S2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( res [ i ] == '1' ) { S1 += str . charAt ( i ) ; } else { S2 += str . charAt ( i ) ; } } System . out . print ( S1 + " ▁ " + S2 + "NEW_LINE"); return ; } } System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { String S = "040425524644" ; findSubsequence ( S ) ; S = "123456789" ; findSubsequence ( S ) ; } }
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
import java . util . * ; class GFG { static String ans , s ; static int K ; static void getString ( int curlen , int N , int [ ] prod ) { if ( curlen == N ) { K -- ; if ( K == 0 ) ans = s ; return ; } char ch ; int ok , t , i ; for ( ch = '2' ; ch <= '9' ; ch ++ ) { s += ch ; ok = 1 ; t = 1 ; for ( i = curlen ; i >= 0 && s . length ( ) > i ; i -- ) { t *= s . charAt ( i ) - 48 ; if ( prod [ t ] != 0 ) ok = 0 ; prod [ t ] ++ ; } if ( ok != 0 ) getString ( curlen + 1 , N , prod ) ; t = 1 ; for ( i = curlen ; i >= 0 && s . length ( ) > i ; i -- ) { t *= s . charAt ( i ) - 48 ; prod [ t ] -- ; } if ( s . length ( ) > 0 ) s = s . substring ( 0 , s . length ( ) - 1 ) ; } } static String kthValidString ( int N ) { if ( N > 10 ) { return " - 1" ; } if ( N == 1 ) { if ( K > 10 ) { return " - 1" ; } String s = " " ; K -- ; s += ( K + '0' ) ; return s ; } ans = " - 1" ; s = " " ; int [ ] prod = new int [ 10005 ] ; getString ( 0 , N , prod ) ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; K = 4 ; System . out . print ( kthValidString ( N ) ) ; } }
Minimum swaps to balance the given brackets at any index | Java implementation for the above approach ; 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
import java . io . * ; import java . util . * ; class GFG { static int BalancedStringBySwapping ( String s ) { int unbalancedPair = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( unbalancedPair > 0 && s . charAt ( i ) == ' ] ' ) { -- unbalancedPair ; } else if ( s . charAt ( i ) == ' [ ' ) { ++ unbalancedPair ; } } return ( unbalancedPair + 1 ) / 2 ; } public static void main ( String [ ] args ) { String s = " ] ] ] [ [ [ " ; System . out . println ( BalancedStringBySwapping ( s ) ) ; } }
Count of palindromic strings of size upto N consisting of first K alphabets occurring at most twice | Java program for the above approach ; Function of return the number of palindromic strings of length N with first K alphabets possible ; If N is odd , half + 1 position can be filled to cope with the extra middle element ; K is reduced by one , because count of choices for the next position is reduced by 1 as a element can only once ; Return the possible count ; Function to find the count of palindromic string of first K characters according to the given criteria ; If N = 1 , then only K palindromic strings possible . ; If N = 2 , the 2 * K palindromic strings possible , K for N = 1 and K for N = 2 ; Initialize ans with the count of strings possible till N = 2 ; Return the possible count ; Driver Code
import java . io . * ; class GFG { static int lengthNPalindrome ( int N , int K ) { int half = N / 2 ; if ( N % 2 == 1 ) { half += 1 ; } int ans = 1 ; for ( int i = 1 ; i <= half ; i ++ ) { ans *= K ; K -- ; } return ans ; } static int palindromicStrings ( int N , int K ) { if ( N == 1 ) { return K ; } if ( N == 2 ) { return 2 * K ; } int ans = 0 ; ans += ( 2 * K ) ; for ( int i = 3 ; i <= N ; i ++ ) { ans += lengthNPalindrome ( i , K ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 , K = 3 ; System . out . println ( palindromicStrings ( N , K ) ) ; } }
Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | Java program for the above approach ; Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code
import java . io . * ; class GFG { static int LCS ( String S , int N , String T , int M , int dp [ ] [ ] ) { if ( N == 0 M == 0 ) return 0 ; if ( dp [ N ] [ M ] != 0 ) return dp [ N ] [ M ] ; if ( S . charAt ( N - 1 ) == T . charAt ( M - 1 ) ) { return dp [ N ] [ M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; } return dp [ N ] [ M ] = Math . max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; } static int minimumCharacter ( String S ) { String T = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) , M = T . length ( ) ; int dp [ ] [ ] = new int [ N + 1 ] [ M + 1 ] ; return ( 26 - LCS ( S , N , T , M , dp ) ) ; } public static void main ( String [ ] args ) { String S = " abcdadc " ; System . out . println ( minimumCharacter ( S ) ) ; } }
Count ways to make the number formed by K concatenations of a numeric string divisible by 5 | Java program for the above approach ; Function to find the value of a ^ b modulo 1000000007 ; Stores the resultant value a ^ b ; Find the value of a ^ b ; Function to count the number of ways such that the formed number is divisible by 5 by removing digits ; Stores the count of ways ; Find the count for string S ; If the digit is 5 or 0 ; Find the count of string for K concatenation of string S ; Find the total count ; Driver Code
class GFG { static long MOD = 1000000007 ; public static long exp_mod ( long a , long b ) { long ret = 1 ; for ( ; b > 0 ; b >>= 1 , a = a * a % MOD ) { if ( ( b & 1 ) > 0 ) ret = ret * a % MOD ; } return ret ; } public static long countOfWays ( String s , int k ) { int N = s . length ( ) ; long ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '5' || s . charAt ( 0 ) == '0' ) { ans = ( ans + exp_mod ( 2 , i ) ) % MOD ; } } long q = exp_mod ( 2 , N ) ; long qk = exp_mod ( q , k ) ; long inv = exp_mod ( q - 1 , MOD - 2 ) ; ans = ans * ( qk - 1 ) % MOD ; ans = ans * inv % MOD ; return ans ; } public static void main ( String args [ ] ) { String S = "1256" ; int K = 1 ; System . out . println ( countOfWays ( S , K ) ) ; } }
Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | Java Program for the above approach ; Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code
import java . util . * ; class GFG { static void printMiddleString ( String S , String T , int N ) { int [ ] a1 = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = ( int ) S . charAt ( i ) - 97 + ( int ) T . charAt ( i ) - 97 ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += ( int ) a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( ( a1 [ i ] & 1 ) != 0 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] = ( int ) a1 [ i ] / 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( ( char ) ( a1 [ i ] + 97 ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; String S = " afogk " ; String T = " asdji " ; printMiddleString ( S , T , N ) ; } }
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Java program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
import java . io . * ; class GFG { static char KthCharacter ( String s , String t , long k ) { long f = 1 ; long ss = 2 ; String tmp = " " ; int len = tmp . length ( ) ; while ( len < k ) { long tf = f ; long ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ( ) ; } char output = tmp . charAt ( ( int ) k - 1 ) ; return output ; } public static void main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; System . out . println ( ans ) ; } }
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Java program to solve the above approach ; storing length ; variables for temporary strings of s and t ; final output variable ; if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k ; incrementing first by 2 , as said in problem statement ; if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k ; incrementing second by two as said in problem statement ; if k is smaller than y , then the character will be at position k % m . ; returning character at k index ; if k is smaller than x , then the character is at position k % n ; returning character at k index ; Driver code
import java . io . * ; class GFG { static char KthCharacter ( String s , String t , long k ) { long n = s . length ( ) ; long m = t . length ( ) ; long first = 1 ; long second = 2 ; char output = ' ? ' ; while ( k > 0 ) { if ( k > first * n ) { long x = first * n ; k = k - x ; first = first + 2 ; if ( k > second * m ) { long y = second * m ; k = k - y ; second = second + 2 ; } else { long ind = k % m ; ind -- ; if ( ind < 0 ) ind = m - 1 ; output = t . charAt ( ( int ) ind ) ; break ; } } else { long ind = k % n ; ind -- ; if ( ind < 0 ) ind = n - 1 ; output = s . charAt ( ( int ) ind ) ; break ; } } return output ; } public static void main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; System . out . println ( ans ) ; } }
Minimum count of 0 s to be removed from given Binary string to make all 1 s occurs consecutively | Java program for the above approach ; Function to find minimum count of 0 s removed from the String S such that all 1 s occurs consecutively ; Stores the first and the last occurrence of 1 ; Iterate over the characters the String , S ; If current character is '1' ; Update fst_occur ; Iterate over the characters the String , S ; If current character is '1' ; Update lst_occur ; Stores the count of 0 s between fst_occur and lst_occur ; Iterate over the characters of S between fst_occur and lst_occur ; If current character is '0' ; Update count ; Print the resultant minimum count ; Driver Code
import java . util . * ; class GFG { static void makeContiguous ( String S , int N ) { int fst_occur = 0 , lst_occur = 0 ; for ( int x = 0 ; x < N ; x ++ ) { if ( S . charAt ( x ) == '1' ) { fst_occur = x ; break ; } } for ( int x = N - 1 ; x >= 0 ; x -- ) { if ( S . charAt ( x ) == '1' ) { lst_occur = x ; break ; } } int count = 0 ; for ( int x = fst_occur ; x <= lst_occur ; x ++ ) { if ( S . charAt ( x ) == '0' ) { count ++ ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S = "010001011" ; int N = S . length ( ) ; makeContiguous ( S , N ) ; } }
Count of ways to rearrange N digits and M alphabets keeping all alphabets together | Java program for the above approach ; Function to find the factorial of the given number ; Function to count ways to rearrange characters of the String such that all alphabets are adjacent . ; Stores factorial of ( N + 1 ) ; Stores factorial of ; Driver Code ; Given a and b ; Function call
import java . util . * ; class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } static int findComb ( int N , int M ) { int x = fact ( N + 1 ) ; int y = fact ( M ) ; return ( x * y ) ; } public static void main ( String [ ] args ) { int N = 2 ; int M = 2 ; System . out . print ( findComb ( N , M ) ) ; } }
Count of camel case characters present in a given string | Java program for the above approach ; Function to count all the camelcase characters in the string S ; Stores the total count of camelcase characters ; Traverse the string S ; If ASCII value of character lies over the range [ 65 , 91 ] then increment the count ; Print the total count obtained ; Driver code
import java . io . * ; class GFG { static void countCamelCase ( String S ) { int count = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) >= 65 && S . charAt ( i ) <= 91 ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String S = " ckjkUUYII " ; countCamelCase ( S ) ; } }
Decode a given string by removing duplicate occurrences | Java program for the above approach ; Function to count the appearances of each character ; If the character is lower case ; If the character is uppercase ; If the character is a punctuation mark ; Function to decode the given encoded String ; Iterate the given String str ; Find the index of the next character to be printed ; Driver Code
import java . util . * ; class GFG { static int findRepitition ( char a ) { if ( a <= ' z ' && a >= ' a ' ) { return a - ' a ' ; } else if ( a <= ' Z ' && a >= ' A ' ) { return a - ' A ' ; } return 0 ; } static void decodeString ( String str ) { String output = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { output += ( str . charAt ( i ) ) ; i += findRepitition ( str . charAt ( i ) ) ; } System . out . print ( " Decrypted ▁ code ▁ is ▁ { " + output + " } " + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " abbbb ▁ acccdddd " ; decodeString ( str ) ; } }
Program to perform a letter frequency attack on a monoalphabetic substitution cipher | Java program for the above approach ; Function to decrypt a monoalphabetic substitution cipher using the letter frequency attack ; Stores final 5 possible deciphered plaintext ; Store the frequency of each letter in cipher text ; Stores the frequency of each letter in cipher text in descending order ; Store which alphabet is used already ; Traverse the String S ; Copy the frequency array ; Stores the String formed from concatanating the english letters in the decreasing frequency in the english language ; Sort the array in descending order ; Itearate over the range [ 0 , 5 ] ; Iterate over the range [ 0 , 26 ] ; Store the numerical equivalent of letter at ith index of array letter_frequency ; Calculate the probable shift used in monoalphabetic cipher ; Temporary String to generate one plaintext at a time ; Generate the probable ith plaintext String using the shift calculated above ; Insert whitespaces as it is ; Shift the kth letter of the cipher by x ; Add the kth calculated / shifted letter to temporary String ; Print the generated 5 possible plaintexts ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static void printString ( String S , int N ) { String [ ] plaintext = new String [ 5 ] ; int freq [ ] = new int [ 26 ] ; int freqSorted [ ] = new int [ 26 ] ; int Used [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) != ' ▁ ' ) { freq [ S . charAt ( i ) - ' A ' ] ++ ; } } for ( int i = 0 ; i < 26 ; i ++ ) { freqSorted [ i ] = freq [ i ] ; } String T = " ETAOINSHRDLCUMWFGYPBVKJXQZ " ; Arrays . sort ( freqSorted ) ; freqSorted = reverse ( freqSorted ) ; for ( int i = 0 ; i < 5 ; i ++ ) { int ch = - 1 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freqSorted [ i ] == freq [ j ] && Used [ j ] == 0 ) { Used [ j ] = 1 ; ch = j ; break ; } } if ( ch == - 1 ) break ; int x = T . charAt ( i ) - ' A ' ; x = x - ch ; String curr = " " ; for ( int k = 0 ; k < N ; k ++ ) { if ( S . charAt ( k ) == ' ▁ ' ) { curr += ( char ) ' ▁ ' ; continue ; } int y = S . charAt ( k ) - ' A ' ; y += x ; if ( y < 0 ) y += 26 ; if ( y > 25 ) y -= 26 ; curr += ( char ) ( ' A ' + y ) ; } plaintext [ i ] = curr ; } for ( int i = 0 ; i < 5 ; i ++ ) { System . out . print ( plaintext [ i ] + "NEW_LINE"); } } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { String S = " B ▁ TJNQMF ▁ NFTTBHF " ; int N = S . length ( ) ; printString ( S , N ) ; } }
Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | Java program for the above approach ; Function to check if the given String S can be made empty ; Stores the characters of the String S ; Traverse the given String ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver Code
import java . util . * ; class GFG { static String canMadeEmpty ( String s , int n ) { Stack < Character > St = new Stack < Character > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' c ' ) { if ( St . size ( ) >= 2 ) { char b = St . peek ( ) ; St . pop ( ) ; char a = St . peek ( ) ; St . pop ( ) ; if ( a != ' a ' b != ' b ' ) return " No " ; } else return " No " ; } else St . add ( s . charAt ( i ) ) ; } if ( St . size ( ) == 0 ) { return " Yes " ; } else { return " No " ; } } public static void main ( String [ ] args ) { String S = " aabcbc " ; int N = S . length ( ) ; System . out . print ( canMadeEmpty ( S , N ) ) ; } }
Maximum count of “ 010. . ” subsequences that can be removed from given Binary String | Java program for the above approach ; Function to count the maximum number of operations performed on the string ; Size of the string ; Stores the maximum number of operations performed ; Stores the count of ' X ' occurred so far ; Traverse the string ; Check if current char is ' X ' ; Check if the value of cnt is greater than 0 ; Decrement the value of cnt ; Increment the value of ans ; Print the value of ans ; Driver code
import java . io . * ; class GFG { public static void countOperations ( String S ) { int n = S . length ( ) ; int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == '0' ) { cnt ++ ; } else { if ( cnt > 0 ) { cnt -- ; ans ++ ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "110011010" ; countOperations ( S ) ; } }
Check if given String can be split only into subsequences ABC | Java program for the above approach ; Function to check if the given string can be splitted into subsequences " ABC " ; Stores the length of the string ; Stores the indices of ' A ' ; Stores the indices of ' B ' ; Stores the indices of ' C ' ; Traverse the string S ; If S [ i ] is equal to ' A ' ; Push the index i in A ; Else if S [ i ] is equal to ' B ' ; Push the index i in B ; Else if S [ i ] is equal to ' C ' ; Push the index i in C ; If N is not multiple of 3 or count of characters ' A ' , ' B ' and ' C ' are not equal ; Return " No " ; Iterate over the deque B ; If A is not empty and curr B is greater than A [ 0 ] ; Removes the front element of A ; Else return " No " ; Iterate over the deque B in reverse ; If C is not empty and last element of C is greater thab B [ i ] ; Removes the last element of C ; Else return " No " ; If none of the above cases satisfy return "Yes' ; Driver code ; Input ; Function call
import java . io . * ; import java . util . * ; class GFG { public static String check ( String S ) { int N = S . length ( ) ; Deque < Integer > A = new LinkedList < Integer > ( ) ; Deque < Integer > B = new LinkedList < Integer > ( ) ; Deque < Integer > C = new LinkedList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' A ' ) { A . addLast ( i ) ; } else if ( S . charAt ( i ) == ' B ' ) { B . addLast ( i ) ; } else { C . addLast ( i ) ; } } if ( N % 3 > 0 || A . size ( ) != B . size ( ) || A . size ( ) != C . size ( ) ) { return " No " ; } for ( Iterator itr = B . iterator ( ) ; itr . hasNext ( ) ; ) { Integer b = ( Integer ) itr . next ( ) ; if ( A . size ( ) > 0 && b > A . getFirst ( ) ) { A . pop ( ) ; } else { return " No " ; } } for ( Iterator itr = B . descendingIterator ( ) ; itr . hasNext ( ) ; ) { Integer b = ( Integer ) itr . next ( ) ; if ( C . size ( ) > 0 && b < C . getLast ( ) ) { C . pollLast ( ) ; } else { return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { String S = " ABABCC " ; System . out . println ( check ( S ) ) ; } }