codeparrot/xlcost-text-to-code huggingface dataset:
Split a string into maximum number of unique substrings | Java program for the above approach ; Function to find the maximum count of unique substrings by splitting a string into maximum number of unique substrings ; Utility function to find maximum count of unique substrings by splitting the string ; Stores maximum count of unique substring by splitting the string into substrings ; Iterate over the characters of the string ; Stores prefix substring ; Check if the current substring already exists ; Insert tmp into set ; Recursively call for remaining characters of string ; Remove from the set ; Return answer ; Driver Code
import java . io . * ; import java . util . * ; class Solution { public int maxUniqueSplit ( String S ) { return maxUnique ( S , new HashSet < String > ( ) ) ; } public int maxUnique ( String S , Set < String > set ) { int max = 0 ; for ( int i = 1 ; i <= S . length ( ) ; i ++ ) { String tmp = S . substring ( 0 , i ) ; if ( ! set . contains ( tmp ) ) { set . add ( tmp ) ; max = Math . max ( max , maxUnique ( S . substring ( i ) , set ) + 1 ) ; set . remove ( tmp ) ; } } return max ; } } class GFG { public static void main ( String [ ] args ) { Solution st = new Solution ( ) ; String S = " ababccc " ; int res = st . maxUniqueSplit ( S ) ; System . out . println ( res ) ; } }
Minimize length of a given string by removing subsequences forming valid parenthesis | Java program of the above approach ; Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Driver code ; Given string ; Size of the string ; Function Call
import java . util . * ; public class GFG { static void removeValidBracketSequences ( String str , int N ) { Vector < Integer > A = new Vector < Integer > ( ) ; Vector < Integer > B = new Vector < Integer > ( ) ; Vector < Integer > C = new Vector < Integer > ( ) ; boolean [ ] vis = new boolean [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { A . add ( i ) ; } else if ( str . charAt ( i ) == ' { ' ) { B . add ( i ) ; } else if ( str . charAt ( i ) == ' [ ' ) { C . add ( i ) ; } else if ( str . charAt ( i ) == ' ) ' && ( A . size ( ) > 0 ) ) { vis [ A . get ( A . size ( ) - 1 ) ] = false ; A . remove ( A . size ( ) - 1 ) ; vis [ i ] = false ; } else if ( str . charAt ( i ) == ' } ' && ( B . size ( ) > 0 ) ) { vis [ B . get ( B . size ( ) - 1 ) ] = false ; B . remove ( B . size ( ) - 1 ) ; vis [ i ] = false ; } else if ( str . charAt ( i ) == ' ] ' && ( C . size ( ) > 0 ) ) { vis [ C . get ( C . size ( ) - 1 ) ] = false ; C . remove ( C . size ( ) - 1 ) ; vis [ i ] = false ; } } for ( int i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) System . out . print ( str . charAt ( i ) ) ; } } public static void main ( String [ ] args ) { String str = " ( ( ) { ( ) ( { } ) " ; int N = str . length ( ) ; removeValidBracketSequences ( str , N ) ; } }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Java program for the above approach ; Function to update frequency of a character in Fenwick tree ; Update frequency of ( idx + ' a ' ) ; Update i ; Function to find the frequency of a character ( idx + ' a ' ) in range [ 1 , i ] ; Stores frequency of character , ( idx + ' a ' ) in range [ 1 , i ] ; Update ans ; Update i ; Function to find difference between maximum and minimum frequency of a character in given range ; BIT [ i ] [ j ] : Stores frequency of ( i + ' a ' ) If j is a power of 2 , then it stores the frequency ( i + ' a ' ) of from [ 1 , j ] ; Stores length of String ; Iterate over the characters of the String ; Update the frequency of s [ i ] in fenwick tree ; Stores count of queries ; Iterate over all the queries ; Stores maximum frequency of a character in range [ l , r ] ; Stores minimum frequency of a character in range [ l , r ] ; Iterate over all possible characters ; Stores frequency of ( j + ' a ' ) in range [ 1 , r ] ; Stores frequency of ( j + ' a ' ) in range [ 1 , l - 1 ] ; Update mx ; If a character ( i + ' a ' ) present in range [ l , r ] ; Update mn ; Print the difference between max and min freq ; Driver Code ; Given String ; Given queries ; Function Call
import java . util . * ; class GFG { static void update ( int BIT [ ] [ ] , int idx , int i , int val ) { while ( i < 10005 ) { BIT [ idx ] [ i ] += val ; i = i + ( i & ( - i ) ) ; } } static int query ( int BIT [ ] [ ] , int idx , int i ) { int ans = 0 ; while ( i > 0 ) { ans += BIT [ idx ] [ i ] ; i = i - ( i & ( - i ) ) ; } return ans ; } static void maxDiffFreq ( String s , int [ ] [ ] queries ) { int [ ] [ ] BIT = new int [ 26 ] [ 10005 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { update ( BIT , s . charAt ( i ) - ' a ' , i + 1 , 1 ) ; } int Q = queries . length ; for ( int i = 0 ; i < Q ; ++ i ) { int mx = 0 ; int mn = Integer . MAX_VALUE ; int l = queries [ i ] [ 0 ] ; int r = queries [ i ] [ 1 ] ; for ( int j = 0 ; j < 26 ; j ++ ) { int p = query ( BIT , j , r ) ; int q = query ( BIT , j , l - 1 ) ; mx = Math . max ( mx , p - q ) ; if ( p > 0 ) { mn = Math . min ( mn , p - q ) ; } } System . out . print ( mx - mn + "NEW_LINE"); } } public static void main ( String [ ] args ) { String S = " abaabac " ; int [ ] [ ] queries = { { 2 , 6 } , { 1 , 7 } } ; maxDiffFreq ( S , queries ) ; } }
Make all strings from a given array equal by replacing minimum number of characters | Java program to implement the above approach ; Function to find the minimum count of operations required to make all Strings equal by replacing characters of Strings ; Stores minimum count of operations required to make all Strings equal ; Stores length of the String ; hash [ i ] [ j ] : Stores frequency of character i present at j - th index of all Strings ; Traverse the array arr [ ] ; Iterate over characters of current String ; Update frequency of arr [ i ] [ j ] ; Traverse hash [ ] [ ] array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
import java . util . * ; class GFG { static int minOperation ( String arr [ ] , int N ) { int cntMinOP = 0 ; int M = arr [ 0 ] . length ( ) ; int [ ] [ ] hash = new int [ 256 ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] . charAt ( j ) ] [ j ] ++ ; } } for ( int i = 0 ; i < M ; i ++ ) { int Sum = 0 ; int Max = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j ] [ i ] ; Max = Math . max ( Max , hash [ j ] [ i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } public static void main ( String [ ] args ) { String arr [ ] = { " abcd " , " bcde " , " cdef " } ; int N = arr . length ; System . out . print ( minOperation ( arr , N ) + "NEW_LINE"); } }
Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | Java program for the above approach ; Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ; Store the starting index of the substring ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void countStrings ( String s , int k ) { int ans = 1 ; int len = s . length ( ) ; int flag = 0 ; int start_ind = 0 ; for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( s . charAt ( i ) - '0' + s . charAt ( i + 1 ) - '0' == k && flag == 0 ) { flag = 1 ; start_ind = i ; } if ( flag == 1 && s . charAt ( i ) - '0' + s . charAt ( i + 1 ) - '0' != k ) { flag = 0 ; if ( ( i - start_ind + 1 ) % 2 != 0 ) ans *= ( i - start_ind + 1 - 1 ) / 2 + 1 ; } } if ( flag == 1 && ( len - start_ind ) % 2 != 0 ) ans *= ( len - start_ind ) / 2 + 1 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "313" ; int K = 4 ; countStrings ( S , K ) ; } }
Smallest number whose product with N has sum of digits equal to that of N | Java program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
class GFG { static void find_num ( String n ) { int ans = 0 ; char [ ] digitsOfN = n . toCharArray ( ) ; int sumOfDigitsN = 0 ; for ( char c : digitsOfN ) { sumOfDigitsN += Integer . parseInt ( Character . toString ( c ) ) ; } for ( int x = 11 ; x > 0 ; x ++ ) { int newNum = x * Integer . parseInt ( n ) ; int tempSumDigits = 0 ; char [ ] temp = Integer . toString ( newNum ) . toCharArray ( ) ; for ( char c : temp ) { tempSumDigits += Integer . parseInt ( Character . toString ( c ) ) ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String N = "3029" ; find_num ( N ) ; } }
Print all distinct strings from a given array | Java program to implement the above approach ; Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver code ; Stores length of the array
import java . io . * ; import java . util . * ; class GFG { static void findDisStr ( List < String > arr , int N ) { Set < String > DistString = new HashSet < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! DistString . contains ( arr . get ( i ) ) ) { DistString . add ( arr . get ( i ) ) ; } } for ( String string : DistString ) { System . out . print ( string + " ▁ " ) ; } } public static void main ( String [ ] args ) { List < String > arr = Arrays . asList ( new String [ ] { " Geeks " , " For " , " Geeks " , " Code " , " Coder " } ) ; int N = arr . size ( ) ; findDisStr ( arr , N ) ; } }
Reverse substrings of given string according to specified array indices | Java program for the above approach ; Function to perform the reversal operation on the given String ; Size of String ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated String ; Swap char of a string ; Driver Code ; Given String str ; Given array of reversing index ; Function Call
import java . util . * ; class GFG { static String modifyString ( int A [ ] , String str , int K ) { int N = str . length ( ) ; int count [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) { count [ A [ i ] ] ++ ; } for ( int i = 1 ; i <= N / 2 ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; if ( ( count [ i ] & 1 ) > 0 ) { str = swap ( str , i - 1 , N - i ) ; } } return str ; } static String swap ( String str , int i , int j ) { char ch [ ] = str . toCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . valueOf ( ch ) ; } public static void main ( String [ ] args ) { String str = " abcdef " ; int arr [ ] = { 1 , 2 , 3 } ; int K = arr . length ; System . out . print ( modifyString ( arr , str , K ) ) ; } }
Convert given Float value to equivalent Fraction | Java program for the above approach ; Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given String str ; Function Call
import java . util . * ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findFraction ( String s ) { String be_deci = " " , af_deci = " " , reccu = " " ; boolean x = true , y = false , z = false ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) == ' . ' ) { x = false ; y = true ; continue ; } if ( s . charAt ( i ) == ' ( ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s . charAt ( i ) ; if ( y ) af_deci += s . charAt ( i ) ; if ( z ) { for ( ; i < s . length ( ) && s . charAt ( i ) != ' ) ' ; ++ i ) reccu += s . charAt ( i ) ; break ; } } int num_be_deci = Integer . valueOf ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . length ( ) != 0 ) num_af_deci = Integer . valueOf ( af_deci ) ; int numr = num_be_deci * ( int ) Math . pow ( 10 , af_deci . length ( ) ) + num_af_deci ; int deno = ( int ) Math . pow ( 10 , af_deci . length ( ) ) ; if ( reccu . length ( ) == 0 ) { int gd = __gcd ( numr , deno ) ; System . out . print ( numr / gd + " ▁ / ▁ " + deno / gd ) ; } else { int reccu_num = Integer . valueOf ( reccu ) ; int numr1 = numr * ( int ) Math . pow ( 10 , reccu . length ( ) ) + reccu_num ; int deno1 = deno * ( int ) Math . pow ( 10 , reccu . length ( ) ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; System . out . print ( res_numr / gd + " ▁ / ▁ " + res_deno / gd ) ; } } public static void main ( String [ ] args ) { String str = "23.98(231 ) " ; findFraction ( str ) ; } }
Lexicographically largest string possible for a given cost of appending characters | Java Program to implement the above approach ; Function to find the lexicographically largest String possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest String ; Backtrack if solution not found ; Find the lexicographically largest String excluding the current character ; Function to print the lexicographically largest String generated ; Function call ; Stores the String ; Print the lexicographically largest String formed ; Driver code ; Cost of adding each alphabet ; Cost of generating the String
import java . util . * ; class GFG { static boolean lexi_largest_String ( int a [ ] , int i , int sum , Vector < Integer > v ) { if ( sum < 0 ) return false ; if ( sum == 0 ) return true ; if ( i < 0 ) return false ; v . add ( i ) ; if ( lexi_largest_String ( a , i , sum - a [ i ] , v ) ) return true ; v . remove ( v . size ( ) - 1 ) ; return lexi_largest_String ( a , i - 1 , sum , v ) ; } static void generateString ( int a [ ] , int sum ) { Vector < Integer > v = new Vector < Integer > ( ) ; lexi_largest_String ( a , 25 , sum , v ) ; String s = " " ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) s += ( char ) ( v . get ( j ) + ' a ' ) ; System . out . print ( s + "NEW_LINE"); } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 } ; int sum = 236 ; generateString ( a , sum ) ; } }
Count of distinct permutations of every possible length of given string | Java implementation of the above approach ; Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given String ; Function to find the total number of combinations possible ; Driver Code
class GFG { static int fact ( int a ) { int i , f = 1 ; for ( i = 2 ; i <= a ; i ++ ) f = f * i ; return f ; } static int permute ( int n , int r ) { int ans = 0 ; ans = ( fact ( n ) / fact ( n - r ) ) ; return ans ; } static int findPermutations ( int n ) { int sum = 0 , P ; for ( int r = 1 ; r <= n ; r ++ ) { P = permute ( n , r ) ; sum = sum + P ; } return sum ; } public static void main ( String [ ] args ) { String str = " xz " ; int result , n ; n = str . length ( ) ; System . out . print ( findPermutations ( n ) ) ; } }
Cycles of length n in an undirected and connected graph | Java program to calculate cycles of length n in a given graph ; Number of vertices ; mark the vertex vert as visited ; if the path of length ( n - 1 ) is found ; mark vert as un - visited to make it usable again ; Check if vertex vert end with vertex start ; For searching every possible path of length ( n - 1 ) ; DFS for searching path by decreasing length by 1 ; marking vert as unvisited to make it usable again ; Count cycles of length N in an undirected and connected graph . ; all vertex are marked un - visited initially . ; Searching for cycle by using v - n + 1 vertices ; ith vertex is marked as visited and will not be visited again ; driver code
public class Main { public static final int V = 5 ; static int count = 0 ; static void DFS ( int graph [ ] [ ] , boolean marked [ ] , int n , int vert , int start ) { marked [ vert ] = true ; if ( n == 0 ) { marked [ vert ] = false ; if ( graph [ vert ] [ start ] == 1 ) { count ++ ; return ; } else return ; } for ( int i = 0 ; i < V ; i ++ ) if ( ! marked [ i ] && graph [ vert ] [ i ] == 1 ) DFS ( graph , marked , n - 1 , i , start ) ; marked [ vert ] = false ; } static int countCycles ( int graph [ ] [ ] , int n ) { boolean marked [ ] = new boolean [ V ] ; for ( int i = 0 ; i < V - ( n - 1 ) ; i ++ ) { DFS ( graph , marked , n - 1 , i , i ) ; marked [ i ] = true ; } return count / 2 ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } } ; int n = 4 ; System . out . println ( " Total ▁ cycles ▁ of ▁ length ▁ " + n + " ▁ are ▁ " + countCycles ( graph , n ) ) ; } }
String hashing using Polynomial rolling hash function | Java implementation of the Polynomial Rolling Hash Function ; Function to calculate the hash of a String ; P and M ; Loop to calculate the hash value by iterating over the elements of String ; Driver Code ; Given Strings
class GFG { static long polynomialRollingHash ( String str ) { int p = 31 ; int m = ( int ) ( 1e9 + 9 ) ; long power_of_p = 1 ; long hash_val = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { hash_val = ( hash_val + ( str . charAt ( i ) - ' a ' + 1 ) * power_of_p ) % m ; power_of_p = ( power_of_p * p ) % m ; } return hash_val ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " geeks " ; System . out . print ( " Hash ▁ of ▁ ' " + str1 + " ' ▁ = ▁ " + polynomialRollingHash ( str1 ) ) ; } }
Find first non | Java implementation to find the first non - repeating element of the string using Linked List ; Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function Call
import java . util . LinkedList ; public class FirstNonRepeatingElement { static void firstNonRepElement ( String str ) { LinkedList < Character > list = new LinkedList < Character > ( ) ; list . add ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( list . contains ( str . charAt ( i ) ) ) list . remove ( list . indexOf ( str . charAt ( i ) ) ) ; else list . add ( str . charAt ( i ) ) ; } System . out . println ( list . get ( 0 ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; firstNonRepElement ( str ) ; } }
DFA that begins with ' a ' but does not contain substring ' aab ' | Java code for the above DFA ; Function for state A transition ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state B ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for transition state C ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state D ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state Q transition ; In dead state it shows String not accepted ; Driver code ; Take String input ; Call stateA to check the input
import java . util . * ; class GFG { static void stateA ( String n ) { if ( n . charAt ( 0 ) == ' a ' ) { stateB ( n . substring ( 1 ) ) ; } else { stateQ ( n ) ; } } static void stateB ( String n ) { if ( n . length ( ) == 0 ) { System . out . print ( " Accepted " ) ; } else { if ( n . charAt ( 0 ) == ' a ' ) stateC ( n . substring ( 1 ) ) ; else stateD ( n . substring ( 1 ) ) ; } } static void stateC ( String n ) { if ( n . length ( ) == 0 ) System . out . print ( " Accepted " ) ; else { if ( n . charAt ( 0 ) == ' a ' ) stateC ( n . substring ( 1 ) ) ; else stateQ ( n ) ; } } static void stateD ( String n ) { if ( n . length ( ) == 0 ) System . out . print ( " Accepted " ) ; else { if ( n . charAt ( 0 ) == ' a ' ) { stateB ( n . substring ( 1 ) ) ; } else { stateD ( n . substring ( 1 ) ) ; } } } static void stateQ ( String n ) { System . out . print ( " Not ▁ Accepted " ) ; } public static void main ( String [ ] args ) { String n = " aaaba " ; stateA ( n ) ; } }