codeparrot/xlcost-text-to-code huggingface dataset:
Generate a string of size N whose each substring of size M has exactly K distinct characters | Java program to generate a String of size N whose each subString of size M has exactly K distinct characters ; Function to generate the String ; Declare empty String ; counter for M ; counter for K ; Loop to generate String size of N ; Generating K distinct letters one by one ; After generating b distinct letters , append rest a - b letters as ' a ' ; Reset the counter value and repeat the process ; return final result String ; Driver code
import java . util . * ; class GFG { static String generateString ( int N , int M , int K ) { String s = " " ; int cnt1 = 0 ; int cnt2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt1 ++ ; cnt2 ++ ; if ( cnt1 <= M ) { if ( cnt2 <= K ) { s = s + ( char ) ( 96 + cnt1 ) ; } else { s = s + ' a ' ; } } else { cnt1 = 1 ; cnt2 = 1 ; s = s + ' a ' ; } } return s ; } public static void main ( String [ ] args ) { int N = 7 , M = 5 , K = 3 ; System . out . println ( generateString ( N , M , K ) ) ; } }
Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | Java program to print the final String after removing all the occurrences of "10" and "01" from the given binary String ; Function to print the final String after removing all the occurrences of "10" and "01" from the given binary String ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Variable left will store whether 0 ' s ▁ or ▁ 1' s is left in the final String ; Length of the String ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the String ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the final remaining String after removing all the occurrences ; Printing the final String ; Driver Code
import java . util . * ; class GFG { static void finalString ( String str ) { int x = 0 , y = 0 ; int left ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * Math . min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { System . out . print ( left ) ; } } public static void main ( String [ ] args ) { String str = "010110100100000" ; finalString ( str ) ; } }
Longest palindrome formed by concatenating and reordering strings of equal length | Java program to find the Longest palindrome that can be formed by concatenating and reordering given N Strings of equal length ; Function to print the longest palindrome ; Printing every String in left vector ; Printing the palindromic String in the middle ; Printing the reverse of the right vector to make the final output palindromic ; Function to find and print the longest palindrome that can be formed ; Inserting each String in the set ; Vectors to add the Strings in the left and right side ; To add the already present palindrome String in the middle of the solution ; Iterating through all the given Strings ; If the String is a palindrome it is added in the middle ; Checking if the reverse of the String is already present in the set ;  ; Driver code
import java . util . * ; class GFG { static void printPalindrome ( Vector < String > left , String mid , Vector < String > right ) { for ( String x : left ) System . out . print ( x ) ; System . out . print ( mid ) ; Collections . reverse ( right ) ; for ( String x : right ) System . out . print ( x ) ; System . out . println ( ) ; } static void findPalindrome ( Vector < String > S , int N , int M ) { HashSet < String > dict = new HashSet < String > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { dict . add ( S . get ( i ) ) ; } Vector < String > left = new Vector < String > ( ) , right = new Vector < String > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = S . get ( i ) ; t = reverse ( t ) ; if ( t == S . get ( i ) ) mid = t ; else if ( dict . contains ( t ) ) { left . add ( S . get ( i ) ) ; right . add ( t ) ; dict . remove ( S . get ( i ) ) ; dict . remove ( t ) ; } } printPalindrome ( left , mid , right ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " tab " , " one " , " bat " } ; Vector < String > S = new Vector < String > ( Arrays . asList ( arr ) ) ; int M = 3 ; int N = S . size ( ) ; findPalindrome ( S , N , M ) ; } }
Lexicographically smaller string by swapping at most one character pair | Java program check whether is it possible to make String A lexicographically smaller than String B ; Swap function ; Function that finds whether is it possible to make String A lexicographically smaller than String B ; Condition if String A is already smaller than B ; Sorting temp String ; Condition for first changed character of String A and temp ; Condition if String A is already sorted ; Finding first changed character from last of String A ; Swap the two characters ; Condition if String A is smaller than B ; Driver Code
import java . util . * ; class GFG { static String swap ( String str , int i , int j ) { char [ ] tempArr = str . toCharArray ( ) ; char temp = tempArr [ i ] ; tempArr [ i ] = tempArr [ j ] ; tempArr [ j ] = temp ; return String . valueOf ( tempArr ) ; } static boolean IsLexicographicallySmaller ( String A , String B ) { if ( A . compareTo ( B ) < 0 ) { return true ; } String temp = A ; char p [ ] = temp . toCharArray ( ) ; Arrays . sort ( p ) ; temp = String . valueOf ( p ) ; int index = - 1 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != temp . charAt ( i ) ) { index = i ; break ; } } if ( index == - 1 ) { return false ; } int j = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) == temp . charAt ( index ) ) j = i ; } A = swap ( A , index , j ) ; if ( A . compareTo ( B ) < 0 ) { return true ; } else { return false ; } } public static void main ( String args [ ] ) { String A = " AGAIN " ; String B = " ACTION " ; if ( IsLexicographicallySmaller ( A , B ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Length of longest palindromic sub | Java implementation to find the length of longest palindromic sub - String using Recursion ; Function to find maximum of the two variables ; Function to find the longest palindromic subString : Recursion ; Base condition when the start index is greater than end index ; Base condition when both the start and end index are equal ; Condition when corner characters are equal in the String ; Recursive call to find the longest Palindromic String by excluding the corner characters ; Recursive call to find the longest Palindromic String by including one corner character at a time ; Function to find the longest palindromic sub - String ; Utility function call ; Driver Code ; Function Call
class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int longestPalindromic ( String str , int i , int j , int count ) { if ( i > j ) return count ; if ( i == j ) return ( count + 1 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) { count = longestPalindromic ( str , i + 1 , j - 1 , count + 2 ) ; return max ( count , max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ) ; } return Math . max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ; } static int longest_palindromic_substr ( String str ) { return longestPalindromic ( str , 0 , str . length ( ) - 1 , 0 ) ; } public static void main ( String [ ] args ) { String str = " aaaabbaa " ; System . out . print ( longest_palindromic_substr ( str ) ) ; } }
Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | Java implementation to find the prefix of the s such that occurrence of each character is atmost the count of minimum frequency in the s ; Function to find the maximum possible prefix of the s ; Hash map to store the frequency of the characters in the s ; Iterate over the s to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the s ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix s and its length . ; Driver code ; s is initialize . ; str is passed in MaxPrefix function .
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void MaxPrefix ( String s ) { Map < Character , Integer > Dict = new HashMap < > ( ) ; for ( char i : s . toCharArray ( ) ) { Dict . put ( i , Dict . getOrDefault ( i , 0 ) + 1 ) ; } int minfrequency = Integer . MAX_VALUE ; for ( Integer x : Dict . values ( ) ) { minfrequency = Math . min ( minfrequency , x ) ; } int countminFrequency = 0 ; for ( Map . Entry < Character , Integer > x : Dict . entrySet ( ) ) { if ( x . getValue ( ) == minfrequency ) countminFrequency += 1 ; } Map < Character , Integer > mapper = new HashMap < > ( ) ; int indi = 0 ; for ( char i : s . toCharArray ( ) ) { mapper . put ( i , mapper . getOrDefault ( i , 0 ) + 1 ) ; if ( mapper . get ( i ) > countminFrequency ) break ; indi += 1 ; } System . out . println ( s . substring ( 0 , indi ) ) ; } public static void main ( String [ ] args ) { String str = " aabcdaab " ; MaxPrefix ( str ) ; } }
Count of Substrings that can be formed without using the given list of Characters | Java implementation of the above approach ; Function to find the Number of sub - Strings without using given character ; Mark the given characters in the freq array ; Count variable to store the count of the characters until a character from given L is encountered ; If a character from L is encountered , then the answer variable is incremented by the value obtained by using the mentioned formula and count is set to 0 ; For last remaining characters ; Driver code
import java . util . * ; class GFG { static int countSubString ( char [ ] S , char L [ ] , int n ) { int [ ] freq = new int [ 26 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ ( int ) ( L [ i ] - ' a ' ) ] = 1 ; } int count = 0 ; for ( int x : S ) { if ( freq [ ( int ) ( x - ' a ' ) ] > 0 ) { ans += ( count * count + count ) / 2 ; count = 0 ; } else count ++ ; } ans += ( count * count + count ) / 2 ; return ans ; } public static void main ( String [ ] args ) { String S = " abcpxyz " ; char L [ ] = { ' a ' , ' p ' , ' q ' } ; int n = L . length ; System . out . print ( countSubString ( S . toCharArray ( ) , L , n ) ) ; } }
Program to accept Strings starting with a Vowel | Java program to accept String starting with Vowel ; Function to check if first character is vowel ; Function to check ; Driver code
class GFG { static int checkIfStartsWithVowels ( char [ ] str ) { if ( ! ( str [ 0 ] == ' A ' str [ 0 ] == ' a ' str [ 0 ] == ' E ' str [ 0 ] == ' e ' str [ 0 ] == ' I ' str [ 0 ] == ' i ' str [ 0 ] == ' O ' str [ 0 ] == ' o ' str [ 0 ] == ' U ' str [ 0 ] == ' u ' ) ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithVowels ( str . toCharArray ( ) ) == 1 ) System . out . print ( "Not AcceptedNEW_LINE"); else System . out . print ( "AcceptedNEW_LINE"); } public static void main ( String [ ] args ) { String str = " animal " ; check ( str ) ; str = " zebra " ; check ( str ) ; } }
Find the Nth occurrence of a character in the given String | Java implementation to find the Nth occurrence of a character ; Function to find the Nth occurrence of a character ; Loop to find the Nth occurrence of the character ; Driver Code
import java . util . * ; class GFG { static int findNthOccur ( String str , char ch , int N ) { int occur = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ch ) { occur += 1 ; } if ( occur == N ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { String str = " geeks " ; char ch = ' e ' ; int N = 2 ; System . out . print ( findNthOccur ( str , ch , N ) ) ; } }
Longest equal substring with cost less than K | Java program to find the maximum length of equal subString within a given cost ; Function to find the maximum length ; Fill the prefix array with the difference of letters ; Update the maximum length ; Driver code
class GFG { static int solve ( String X , String Y , int N , int K ) { int [ ] count = new int [ N + 1 ] ; int sol = 0 ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count [ i ] = count [ i - 1 ] + Math . abs ( X . charAt ( i - 1 ) - Y . charAt ( i - 1 ) ) ; } int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { while ( ( count [ i ] - count [ j ] ) > K ) { j ++ ; } sol = Math . max ( sol , i - j ) ; } return sol ; } public static void main ( String [ ] args ) { int N = 4 ; String X = " abcd " , Y = " bcde " ; int K = 3 ; System . out . print ( solve ( X , Y , N , K ) + "NEW_LINE"); } }
Jaro and Jaro | Java implementation of above approach ; Function to calculate the Jaro Similarity of two strings ; If the strings are equal ; Length of two strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first string ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second string ; Return the Jaro Similarity ; Jaro Winkler Similarity ; If the jaro Similarity is above a threshold ; Find the length of common prefix ; If the characters match ; Else break ; Maximum of 4 characters are allowed in prefix ; Calculate jaro winkler Similarity ; Driver code ; Print Jaro - Winkler Similarity of two strings
class GFG { static double jaro_distance ( String s1 , String s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . length ( ) , len2 = s2 . length ( ) ; if ( len1 == 0 len2 == 0 ) return 0.0 ; int max_dist = ( int ) Math . floor ( Math . max ( len1 , len2 ) / 2 ) - 1 ; int match = 0 ; int hash_s1 [ ] = new int [ s1 . length ( ) ] ; int hash_s2 [ ] = new int [ s2 . length ( ) ] ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = Math . max ( 0 , i - max_dist ) ; j < Math . min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 . charAt ( i ) == s2 . charAt ( j ) && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] == 1 ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 . charAt ( i ) != s2 . charAt ( point ++ ) ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } static double jaro_Winkler ( String s1 , String s2 ) { double jaro_dist = jaro_distance ( s1 , s2 ) ; if ( jaro_dist > 0.7 ) { int prefix = 0 ; for ( int i = 0 ; i < Math . min ( s1 . length ( ) , s2 . length ( ) ) ; i ++ ) { if ( s1 . charAt ( i ) == s2 . charAt ( i ) ) prefix ++ ; else break ; } prefix = Math . min ( 4 , prefix ) ; jaro_dist += 0.1 * prefix * ( 1 - jaro_dist ) ; } return jaro_dist ; } public static void main ( String [ ] args ) { String s1 = " TRATE " , s2 = " TRACE " ; System . out . println ( " Jaro - Winkler ▁ Similarity ▁ = " + jaro_Winkler ( s1 , s2 ) ) ; } }
Check if a word is present in a sentence | Java implementation of the approach ; Function that returns true if the word is found ; To break the sentence in words ; To temporarily store each individual word ; Comparing the current word with the word to be searched ; Driver code
class GFG { static boolean isWordPresent ( String sentence , String word ) { String [ ] s = sentence . split ( " ▁ " ) ; for ( String temp : s ) { if ( temp . compareTo ( word ) == 0 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String s = " Geeks ▁ for ▁ Geeks " ; String word = " Geeks " ; if ( isWordPresent ( s , word ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if a word is present in a sentence | Java implementation of the approach ; Function that returns true if the word is found ; To convert the word in uppercase ; To convert the complete sentence in uppercase ; To break the sentence in words ; To store the individual words of the sentence ; Comparing the current word with the word to be searched ; Driver code
import java . util . * ; class GFG { static boolean isWordPresent ( String sentence , String word ) { word = transform ( word ) ; sentence = transform ( sentence ) ; String [ ] s = sentence . split ( " ▁ " ) ; for ( String temp : s ) { if ( temp . compareTo ( word ) == 0 ) { return true ; } } return false ; } static String transform ( String word ) { return word . toUpperCase ( ) ; } public static void main ( String [ ] args ) { String s = " Geeks ▁ for ▁ Geeks " ; String word = " geeks " ; if ( isWordPresent ( s , word ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Count of 1 | Java implementation of the above approach ; Function to return the count of required characters ; While there are characters left ; Single bit character ; Two - bit character ; Update the count ; Driver code
class GFG { static int countChars ( String str , int n ) { int i = 0 , cnt = 0 ; while ( i < n ) { if ( str . charAt ( i ) == '0' ) i += 1 ; else i += 2 ; cnt += 1 ; } return cnt ; } public static void main ( String [ ] args ) { String str = "11010" ; int n = str . length ( ) ; System . out . println ( countChars ( str , n ) ) ; } }
Print the frequency of each character in Alphabetical order | Java implementation of the approach ; Function to print the frequency of each of the characters of s in alphabetical order ; To store the frequency of the characters ; Update the frequency array ; Print the frequency in alphatecial order ; If the current alphabet doesn 't  appear in the string ; Driver code
class GFG { static int MAX = 26 ; static void compressString ( String s , int n ) { int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == 0 ) continue ; System . out . print ( ( char ) ( i + ' a ' ) + " " + freq [ i ] ) ; } } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; compressString ( s , n ) ; } }
Find the number obtained after concatenation of binary representation of M and N | Java implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array arr [ ] ; Funtion to convert the number represented as a binary array arr [ ] into its decimal equivalent ; Function to concatenate the binary numbers and return the decimal result ; Number of bits in both the numbers ; Convert the bits in both the integers to the arrays a [ ] and b [ ] ; c [ ] will be the binary array for the result ; Update the c [ ] array ; Return the decimal equivalent of the result ; Driver code
class GFG { static void decBinary ( int arr [ ] , int n ) { int k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } static int binaryDec ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } static int concat ( int m , int n ) { int k = ( int ) ( Math . log ( m ) / Math . log ( 2 ) ) + 1 ; int l = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; int a [ ] = new int [ k ] ; int b [ ] = new int [ l ] ; int c [ ] = new int [ k + l ] ; decBinary ( a , m ) ; decBinary ( b , n ) ; int in = 0 ; for ( int i = 0 ; i < k ; i ++ ) c [ in ++ ] = a [ i ] ; for ( int i = 0 ; i < l ; i ++ ) c [ in ++ ] = b [ i ] ; return ( binaryDec ( c , k + l ) ) ; } public static void main ( String [ ] args ) { int m = 4 , n = 5 ; System . out . println ( concat ( m , n ) ) ; } }
Find the number obtained after concatenation of binary representation of M and N | Java implementation of the approach ; Utility function to calculate binary length of a number . ; Function to concatenate the binary numbers and return the decimal result ; Find binary length of n ; left binary shift m and then add n ; Driver code
import java . util . * ; class GFG { public static int getBinaryLength ( int n ) { int length = 0 ; while ( n > 0 ) { length += 1 ; n /= 2 ; } return length ; } public static int concat ( int m , int n ) { int length = getBinaryLength ( n ) ; return ( m << length ) + n ; } public static void main ( String [ ] args ) throws Exception { int m = 4 , n = 5 ; System . out . println ( concat ( m , n ) ) ; } }
XOR two binary strings of unequal lengths | Java implementation of the approach ; Function to insert n 0 s in the beginning of the given string ; Function to return the XOR of the given strings ; Lengths of the given strings ; Make both the strings of equal lengths by inserting 0 s in the beginning ; Updated length ; To store the resultant XOR ; Driver code
class GFG { static String addZeros ( String str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { str = "0" + str ; } return str ; } static String getXOR ( String a , String b ) { int aLen = a . length ( ) ; int bLen = b . length ( ) ; if ( aLen > bLen ) { a = addZeros ( b , aLen - bLen ) ; } else if ( bLen > aLen ) { a = addZeros ( a , bLen - aLen ) ; } int len = Math . max ( aLen , bLen ) ; String res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( a . charAt ( i ) == b . charAt ( i ) ) res += "0" ; else res += "1" ; } return res ; } public static void main ( String [ ] args ) { String a = "11001" , b = "111111" ; System . out . println ( getXOR ( a , b ) ) ; } }
Generate Binary Strings of length N using Branch and Bound | Java Program to generate Binary Strings using Branch and Bound ; Creating a Node class ; Queue that maintains the list of live Nodes ; Utility function to generate binary strings of length n ; If list is full print combination ; Create a new vector for new combination ; iterate while length is not equal to n ; Driver code ; Initiate Generation Create a root Node ; Instantiate the Queue
import java . io . * ; import java . util . * ; class Node { int soln [ ] ; int level ; ArrayList < Node > child ; Node parent ; Node ( Node parent , int level , int N ) { this . parent = parent ; this . level = level ; this . soln = new int [ N ] ; } } class GFG { static int N ; public static Queue < Node > Q ; public static void generate ( Node n ) { if ( n . level == N ) { for ( int i = 0 ; i <= N - 1 ; i ++ ) { System . out . print ( n . soln [ i ] ) ; } System . out . println ( ) ; } else { n . child = new ArrayList < Node > ( ) ; int l = n . level ; for ( int i = 0 ; i <= 1 ; i ++ ) { Node x = new Node ( n , l + 1 , N ) ; for ( int k = 0 ; k < l ; k ++ ) { x . soln [ k ] = n . soln [ k ] ; } x . soln [ l ] = i ; n . child . add ( x ) ; Q . add ( x ) ; } } } public static void main ( String args [ ] ) { N = 3 ; Node root = new Node ( null , 0 , N ) ; Q = new LinkedList < Node > ( ) ; Q . add ( root ) ; while ( Q . size ( ) != 0 ) { Node E = Q . poll ( ) ; generate ( E ) ; } } }