codeparrot/xlcost-text-to-code huggingface dataset:
Minimum , maximum and average price values for all the items of given type | Java implementation of the approach ; To store an item ; To store the minimum and the maximum price for the item ; To store the total number of items of the current type and the total cost of buying them ; Initializing an element ; Function to find the minimum , the maximum and the average price for every item ; To store the distinct items ; For every item ; If the current item has aready been purchased earlier from a different shop ; Get the item ; Update its minimum and maximum price so far ; Increment the total count of the current item ; Add the current price to the sum ; The item has been purchased for the first time ; Print all the items with their minimum , maximum and average prices ; Driver code
import java . util . * ; class GFG { static class Item { int min , max ; int total , sum ; Item ( int price ) { min = price ; max = price ; total = 1 ; this . sum = price ; } } static void findPrices ( String item [ ] , int price [ ] , int n ) { HashMap < String , Item > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( item [ i ] ) ) { Item currItem = map . get ( item [ i ] ) ; currItem . min = Math . min ( currItem . min , price [ i ] ) ; currItem . max = Math . max ( currItem . max , price [ i ] ) ; currItem . total ++ ; currItem . sum += price [ i ] ; } else { Item currItem = new Item ( price [ i ] ) ; map . put ( item [ i ] , currItem ) ; } } System . out . println ( " Item ▁ Min ▁ Max ▁ Average " ) ; for ( Map . Entry < String , Item > ob : map . entrySet ( ) ) { String key = ob . getKey ( ) ; Item currItem = ob . getValue ( ) ; System . out . println ( key + " ▁ " + currItem . min + " ▁ " + currItem . max + " ▁ " + ( ( float ) currItem . sum / ( float ) currItem . total ) ) ; } } public static void main ( String args [ ] ) { String item [ ] = { " toy " , " pen " , " notebook " , " pen " } ; int n = item . length ; int price [ ] = { 2 , 1 , 3 , 2 } ; findPrices ( item , price , n ) ; } }
Queries to find the count of characters preceding the given location | Java implementation of the approach ; returns character at index pos - 1 ; Driver Code
class GFG { static int Count ( String s , int pos ) { int c = s . charAt ( pos - 1 ) ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s . charAt ( i ) == c ) counter = counter + 1 ; } return counter ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . length ( ) ; int query [ ] = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; System . out . println ( Count ( s , pos ) ) ; } } }
Queries to find the count of characters preceding the given location | Java implementation of the approach ; Driver Code
import java . util . * ; class GFG { static void Count ( int [ ] temp ) { int [ ] query = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { int pos = query [ i ] ; System . out . println ( temp [ pos - 1 ] ) ; } } static int [ ] processing ( String s , int len ) { int [ ] temp = new int [ len ] ; HashMap < Character , Integer > d = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! d . containsKey ( s . charAt ( i ) ) ) { d . put ( s . charAt ( i ) , i ) ; } else { temp [ i ] = temp [ d . get ( s . charAt ( i ) ) ] + 1 ; d . put ( s . charAt ( i ) , i ) ; } } return temp ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int n = s . length ( ) ; int [ ] temp = processing ( s , n ) ; Count ( temp ) ; } }
Count of matchsticks required to represent the given number | Java implementation of the approach ; stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code
import java . util . * ; class GFG { static int sticks [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str . charAt ( i ) - '0' ] ) ; } return cnt ; } public static void main ( String [ ] args ) { String str = "56" ; int n = str . length ( ) ; System . out . println ( countSticks ( str , n ) ) ; } }
Print characters in decreasing order of frequency | Java implementation of the approach ; Function to print the characters of the given String in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code
import java . util . * ; class GFG { static void printChar ( char [ ] arr , int len ) { HashMap < Character , Integer > occ = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . containsKey ( arr [ i ] ) ) { occ . put ( arr [ i ] , occ . get ( arr [ i ] ) + 1 ) ; } else { occ . put ( arr [ i ] , 1 ) ; } int size = occ . size ( ) ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = 0 ; for ( Map . Entry < Character , Integer > it : occ . entrySet ( ) ) { if ( it . getValue ( ) > currentMax || ( it . getValue ( ) == currentMax && it . getKey ( ) > arg_max ) ) { arg_max = it . getKey ( ) ; currentMax = it . getValue ( ) ; } } System . out . print ( arg_max + " ▁ - ▁ " + currentMax + "NEW_LINE"); occ . remove ( arg_max ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; printChar ( str . toCharArray ( ) , len ) ; } }
Modulo of a large Binary String | Java implementation of the approach ; Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code
import java . util . * ; class GFG { static int getMod ( String str , int n , int k ) { int pwrTwo [ ] = new int [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; int i = 0 , j = n - 1 ; while ( i < n ) { if ( str . charAt ( j ) == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } public static void main ( String [ ] args ) { String str = "1101" ; int n = str . length ( ) ; int k = 45 ; System . out . print ( getMod ( str , n , k ) ) ; } }
Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1 's | Java implementation of the approach ; Function to return the count of all possible valid strings ; Fill 0 's in the dp array ; Base cases ; dp [ i ] [ j ] = number of possible strings such that '1' just appeared consecutively j times upto the ith index ; Taking previously calculated value ; Taking all possible cases that can appear at the Nth position ; Driver code
class GFG { final static int MOD = 1000000007 ; static long countStrings ( int N ) { int i , j ; int dp [ ] [ ] = new int [ N + 1 ] [ 3 ] ; for ( i = 0 ; i < N + 1 ; i ++ ) { for ( j = 9 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } int ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( countStrings ( N ) ) ; } }
Find the maximum possible Binary Number from given string | Java implementation of the approach ; Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver Code
import java . util . * ; class GFG { static String maxNumber ( String str , int n ) { int [ ] freq = new int [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' z ' ) freq [ 0 ] ++ ; else if ( str . charAt ( i ) == ' n ' ) freq [ 1 ] ++ ; } String num = " " ; for ( int i = 0 ; i < freq [ 1 ] ; i ++ ) num += '1' ; for ( int i = 0 ; i < freq [ 0 ] ; i ++ ) num += '0' ; return num ; } public static void main ( String [ ] args ) { String str = " roenenzooe " ; int n = str . length ( ) ; System . out . println ( maxNumber ( str , n ) ) ; } }
Contiguous unique substrings with the given length L | Java implementation of above approach ; Function to print the unique sub - String of length n ; set to store the Strings ; if the size of the String is equal to 1 then insert ; inserting unique sub - String of length L ; Printing the set of Strings ; Driver Code ; Function calling
import java . util . * ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { String ans = " " ; for ( int j = i ; j < ( int ) s . length ( ) ; j ++ ) { ans += s . charAt ( j ) ; if ( ans . length ( ) == n ) { st . add ( ans ) ; break ; } } } for ( String it : st ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }
Find the occurrence of the given binary pattern in the binary representation of the array elements | Java implementation of the approach ; Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code
import java . util . * ; class GFG { static String decToBinary ( int n ) { int [ ] binaryNum = new int [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } String binary = " " ; for ( int j = i - 1 ; j >= 0 ; j -- ) { binary += String . valueOf ( binaryNum [ j ] ) ; } return binary ; } static int countFreq ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int res = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt . charAt ( i + j ) != pat . charAt ( j ) ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } static void findOccurrence ( int arr [ ] , int n , String pattern ) { for ( int i = 0 ; i < n ; i ++ ) { String binary = decToBinary ( arr [ i ] ) ; System . out . print ( countFreq ( pattern , binary ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 106 , 7 , 8 } ; String pattern = "10" ; int n = arr . length ; findOccurrence ( arr , n , pattern ) ; } }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Java implementation of the approach ; Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code
import java . util . Vector ; class GFG { static boolean canBeBalanced ( String s , int n ) { if ( n % 2 == 1 ) return false ; String k = " ( " ; k += s + " ) " ; Vector < String > d = new Vector < > ( ) ; for ( int i = 0 ; i < k . length ( ) ; i ++ ) { if ( k . charAt ( i ) == ' ( ' ) d . add ( " ( " ) ; else { if ( d . size ( ) != 0 ) d . remove ( d . size ( ) - 1 ) ; else return false ; } } if ( d . isEmpty ( ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ) ( ( ) " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the winner of the game | Java implementation of the approach ; Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
import java . util . Arrays ; class GFG { static void find_winner ( String str , int n ) { String str1 = " " , str2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { str1 += str . charAt ( i ) ; } else { str2 += str . charAt ( i ) ; } } char a [ ] = str1 . toCharArray ( ) ; Arrays . sort ( a ) ; char b [ ] = str2 . toCharArray ( ) ; Arrays . sort ( b ) ; str1 = new String ( a ) ; str2 = new String ( b ) ; if ( str1 . compareTo ( str2 ) < 0 ) System . out . print ( " A " ) ; else if ( str1 . compareTo ( str2 ) > 0 ) System . out . print ( " B " ) ; else System . out . print ( " Tie " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; find_winner ( str , n ) ; } }
Count of characters in str1 such that after deleting anyone of them str1 becomes str2 | Java implementation of the approach ; Function to return the count of required indices ; Solution doesn 't exist ; Find the length of the longest common prefix of strings ; Find the length of the longest common suffix of strings ; If solution does not exist ; Return the count of indices ; Driver code
class GFG { static int Find_Index ( String str1 , String str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; int l = 0 ; int r = 0 ; if ( n != m + 1 ) { return - 1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( str1 . charAt ( i ) == str2 . charAt ( i ) ) { l += 1 ; } else { break ; } } int i = n - 1 ; int j = m - 1 ; while ( i >= 0 && j >= 0 && str1 . charAt ( i ) == str2 . charAt ( j ) ) { r += 1 ; i -= 1 ; j -= 1 ; } if ( l + r < m ) { return - 1 ; } else { i = Math . max ( n - r , 1 ) ; j = Math . min ( l + 1 , n ) ; return ( j - i + 1 ) ; } } public static void main ( String [ ] args ) { String str1 = " aaa " , str2 = " aa " ; System . out . println ( Find_Index ( str1 , str2 ) ) ; } }
Expand the string according to the given conditions | Java implementation of the approach ; Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
public class GFG { static void expandString ( String strin ) { String temp = " " ; int j ; for ( int i = 0 ; i < strin . length ( ) ; i ++ ) { if ( strin . charAt ( i ) >= 0 ) { int num = strin . charAt ( i ) - '0' ; if ( strin . charAt ( i + 1 ) == ' ( ' ) { for ( j = i + 1 ; strin . charAt ( j ) != ' ) ' ; j ++ ) { if ( ( strin . charAt ( j ) >= ' a ' && strin . charAt ( j ) <= ' z ' ) || ( strin . charAt ( j ) >= ' A ' && strin . charAt ( j ) <= ' Z ' ) ) { temp += strin . charAt ( j ) ; } } for ( int k = 1 ; k <= num ; k ++ ) { System . out . print ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ( ) ) { i = j ; } } } } } public static void main ( String args [ ] ) { String strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; } }
Largest substring of str2 which is a prefix of str1 | Java implementation of the approach ; Function to return the largest substring in str2 which is a prefix of str1 ; To store the index in str2 which matches the prefix in str1 ; While there are characters left in str1 ; If the prefix is not found in str2 ; Remove the last character ; Prefix found ; No substring found in str2 that matches the prefix of str1 ; Driver code
import java . util . * ; class GFG { static String findPrefix ( String str1 , String str2 ) { boolean pos = false ; while ( str1 . length ( ) > 0 ) { if ( ! str2 . contains ( str1 ) ) str1 = str1 . substring ( 0 , str1 . length ( ) - 1 ) ; else { pos = str2 . contains ( str1 ) ; break ; } } if ( pos == false ) return " - 1" ; return str1 ; } public static void main ( String [ ] args ) { String str1 = " geeksfor " ; String str2 = " forgeeks " ; System . out . println ( findPrefix ( str1 , str2 ) ) ; } }
Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2 | Java implementation of the approach ; Function to return the lexicographically smallest required string ; Initially empty string ; min heap of characters ; Length of the string ; K cannot be greater than the size of the string ; First push the first K characters into the priority_queue ; While there are characters to append ; Append the top of priority_queue to X ; Remove the top element ; Push only if i is less than the size of string ; Return the generated string ; Driver Code
import java . util . PriorityQueue ; class GFG { static String getSmallestStr ( String S , int K ) { String X = " " ; PriorityQueue < Character > pq = new PriorityQueue < > ( ) ; int i , n = S . length ( ) ; K = Math . min ( K , n ) ; for ( i = 0 ; i < K ; i ++ ) pq . add ( S . charAt ( i ) ) ; while ( ! pq . isEmpty ( ) ) { X += pq . peek ( ) ; pq . remove ( ) ; if ( i < S . length ( ) ) pq . add ( S . charAt ( i ) ) ; i ++ ; } return X ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int K = 5 ; System . out . println ( getSmallestStr ( S , K ) ) ; } }
Round the given number to nearest multiple of 10 | Set | Java implementation of the approach ; Function to round the given number to the nearest multiple of 10 ; If string is empty ; If the last digit is less then or equal to 5 then it can be rounded to the nearest ( previous ) multiple of 10 by just replacing the last digit with 0 ; Set the last digit to 0 ; Print the updated number ; The number hast to be rounded to the next multiple of 10 ; To store the carry ; Replace the last digit with 0 ; Starting from the second last digit , add 1 to digits while there is carry ; While there are digits to consider and there is carry to add ; Get the current digit ; Add the carry ; If the digit exceeds 9 then the carry will be generated ; Else there will be no carry ; Update the current digit ; Get to the previous digit ; If the carry is still 1 then it must be inserted at the beginning of the string ; Prin the rest of the number ; Driver code
class GFG { static void roundToNearest ( StringBuilder str , int n ) { if ( str . toString ( ) == " " ) return ; if ( str . charAt ( n - 1 ) - '0' <= 5 ) { str . setCharAt ( n - 1 , '0' ) ; System . out . print ( str . substring ( 0 , n ) ) ; } else { int carry = 0 ; str . setCharAt ( n - 1 , '0' ) ; int i = n - 2 ; carry = 1 ; while ( i >= 0 && carry == 1 ) { int currentDigit = str . charAt ( i ) - '0' ; currentDigit += carry ; if ( currentDigit > 9 ) { carry = 1 ; currentDigit = 0 ; } else carry = 0 ; str . setCharAt ( i , ( char ) ( currentDigit + '0' ) ) ; i -- ; } if ( carry == 1 ) System . out . print ( carry ) ; System . out . print ( str . substring ( 0 , n ) ) ; } } public static void main ( String [ ] args ) { StringBuilder str = new StringBuilder ( "99999999999999993" ) ; int n = str . length ( ) ; roundToNearest ( str , n ) ; } }
Repeat substrings of the given String required number of times | Java implementation of the approach ; Function that returns true if the passed character is a digit ; Function to return the next index of a non - digit character in the string starting at the index i ( returns - 1 if no such index is found ) ; If the character at index i is a digit then skip to the next character ; If no such index was found ; Function to append str the given number of times to the StringBuilder ; Function to return the string after performing the given operations ; To build the resultant string ; Index of the first non - digit character in the string ; While there are substrings that do not consist of digits ; Find the ending of the substring ; Starting index of the number ; If no digit appears after the current substring ; Find the index at which the current number ends ; Parse the number from the substring ; Repeat the current substring required number of times ; Find the next non - digit character index ; Return the resultant string ; Driver code
class GFG { static boolean isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static int nextNonDigit ( String str , int i ) { while ( i < str . length ( ) && isDigit ( str . charAt ( i ) ) ) { i ++ ; } if ( i >= str . length ( ) ) return - 1 ; return i ; } static void appendRepeated ( StringBuilder sb , String str , int times ) { for ( int i = 0 ; i < times ; i ++ ) sb . append ( str ) ; } static String findString ( String str , int n ) { StringBuilder sb = new StringBuilder ( " " ) ; int startStr = nextNonDigit ( str , 0 ) ; while ( startStr != - 1 ) { int endStr = startStr ; while ( ( endStr + 1 ) < n && ! isDigit ( str . charAt ( endStr + 1 ) ) ) { endStr ++ ; } int startNum = endStr + 1 ; if ( startNum == - 1 ) break ; int endNum = startNum ; while ( ( endNum + 1 ) < n && isDigit ( str . charAt ( endNum + 1 ) ) ) { endNum ++ ; } int num = Integer . parseInt ( str . substring ( startNum , endNum + 1 ) ) ; appendRepeated ( sb , str . substring ( startStr , endStr + 1 ) , num ) ; startStr = nextNonDigit ( str , endStr + 1 ) ; } return sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " g1ee1ks1for1g1e2ks1" ; int n = str . length ( ) ; System . out . println ( findString ( str , n ) ) ; } }
Recursive program to replace all occurrences of pi with 3.14 in a given string | Java program for above approach ; A simple recursive approach to replace all pi in a given function with "3.14" . Firstly function is declared we don 't  need any helper function one  function is enough ; Base case if s is empty or length of s is 1 return the s ; If the 0 th and 1 st element of s are p and i we have to handle them for rest we have to call recursion it will give the result ; Smalloutput is a variable used to store recursion result ; And we have to add the recursion result with the first part we handled and return the answer ; If not then we have to call recursion from 1 st to all elements then add recursion result to 1 st element and return it ; Driver code ; Function call
class GFG { public static String replacePi ( String s ) { if ( s . length ( ) == 0 || s . length ( ) == 1 ) return s ; if ( s . charAt ( 0 ) == ' p ' && s . charAt ( 1 ) == ' i ' ) { String smallOutput = replacePi ( s . substring ( 2 ) ) ; return "3.14" + smallOutput ; } else { String smallOutput = replacePi ( s . substring ( 1 ) ) ; return s . charAt ( 0 ) + smallOutput ; } } public static void main ( String [ ] args ) { String s = " pipppiiipi " ; String result = replacePi ( s ) ; System . out . println ( result ) ; } }