codeparrot/xlcost-text-to-code huggingface dataset:
Frequency of smallest character in first sentence less than that of second sentence | Java program for the above approach ; Function to count the frequency of minimum character ; Sort the string s ; Return the count with smallest character ; Function to count number of frequency of smallest character of string arr1 [ ] is less than the string in arr2 [ ] ; To store the frequency of smallest character in each string of arr2 ; Traverse the arr2 [ ] ; Count the frequency of smallest character in string s ; Append the frequency to freq [ ] ; Sort the frequency array ; Traverse the array arr1 [ ] ; Count the frequency of smallest character in string s ; find the element greater than f ; Find the count such that arr1 [ i ] < arr2 [ j ] ; Print the count ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int countMinFreq ( String s ) { char [ ] tempArray = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; s = new String ( tempArray ) ; int x = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == s . charAt ( 0 ) ) x ++ ; return x ; } static void countLessThan ( List < String > arr1 , List < String > arr2 ) { List < Integer > freq = new ArrayList < Integer > ( ) ; for ( String s : arr2 ) { int f = countMinFreq ( s ) ; freq . add ( f ) ; } Collections . sort ( freq ) ; for ( String s : arr1 ) { int f = countMinFreq ( s ) ; int it = upper_bound ( freq , f ) ; int cnt = freq . size ( ) - it ; System . out . print ( cnt + " ▁ " ) ; } } static int upper_bound ( List < Integer > freq , int f ) { int low = 0 , high = freq . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( freq . get ( mid ) > f ) high = mid ; else low = mid + 1 ; } return ( freq . get ( low ) < f ) ? low ++ : low ; } public static void main ( String [ ] args ) { List < String > arr1 , arr2 ; arr1 = Arrays . asList ( new String [ ] { " yyy " , " zz " } ) ; arr2 = Arrays . asList ( new String [ ] { " x " , " xx " , " xxx " , " xxxx " } ) ; countLessThan ( arr1 , arr2 ) ; } }
Lexicographically all Shortest Palindromic Substrings from a given string | Java program to find Lexicographically all Shortest Palindromic Substrings from a given string ; Function to find all lexicographically shortest palindromic substring ; Array to keep track of alphabetic characters ; Iterate to print all lexicographically shortest substring ; Driver code
class Main { static void shortestPalindrome ( String s ) { int [ ] abcd = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) abcd [ s . charAt ( i ) - 97 ] = 1 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( abcd [ i ] == 1 ) { System . out . print ( ( char ) ( i + 97 ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; shortestPalindrome ( s ) ; } }
Remove duplicates from string keeping the order according to last occurrences | Java program to remove duplicate character from character array and print in sorted order ; Used as index in the modified String ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
import java . util . * ; class GFG { static String removeDuplicates ( String str ) { int n = str . length ( ) ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = i + 1 ; j < n ; j ++ ) if ( str . charAt ( i ) == str . charAt ( j ) ) break ; if ( j == n ) res = res + str . charAt ( i ) ; } return res ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( removeDuplicates ( str ) ) ; } }
Remove duplicates from string keeping the order according to last occurrences | Java program to remove duplicate character from character array and print in sorted order ; Used as index in the modified String ; Create an empty hash table ; Traverse through all characters from right to left ; If current character is not in ; Reverse the result String ; Driver code
import java . util . * ; class GFG { static String removeDuplicates ( String str ) { int n = str . length ( ) ; HashSet < Character > s = new HashSet < Character > ( ) ; String res = " " ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ! s . contains ( str . charAt ( i ) ) ) { res = res + str . charAt ( i ) ; s . add ( str . charAt ( i ) ) ; } } res = reverse ( res ) ; return res ; } 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 str = " geeksforgeeks " ; System . out . print ( removeDuplicates ( str ) ) ; } }
Count of numbers in a range that does not contain the digit M and which is divisible by M . | Java implementation to illustrate the program ; Function to count all the numbers which does not contain the digit ' M ' and is divisible by M ; Storing all the distinct digits of a number ; Checking if the two conditions are satisfied or not ; Driver code ; Lower Range ; Upper Range ; The digit
import java . util . * ; class GFG { static void contain ( int L , int U , int M ) { int count = 0 ; for ( int j = L ; j < U ; j ++ ) { HashSet < String > num = new HashSet < > ( ) ; String str = Integer . toString ( j ) ; num . add ( str ) ; if ( j % M == 0 && ! num . contains ( Integer . toString ( M ) ) ) { count += 1 ; } } System . out . println ( count - 2 ) ; } public static void main ( String [ ] args ) { int L = 106 ; int U = 200 ; int M = 7 ; contain ( L , U , M ) ; } }
Longest string which is prefix string of at least two strings | Java program to find longest String which is prefix String of at least two Strings ; Function to find Max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current String or not ; If atleast 2 String have that character ; Recursive call to i + 1 ; Driver code ; Initialising Strings ; push Strings into vectors .
import java . util . * ; class GFG { static int max1 = 0 ; static int MaxLength ( Vector < String > v , int i , int m ) { if ( i >= m ) { return m - 1 ; } for ( int k = 0 ; k < 26 ; k ++ ) { char c = ( char ) ( ' a ' + k ) ; Vector < String > v1 = new Vector < String > ( ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v . get ( j ) . charAt ( i ) == c ) { v1 . add ( v . get ( j ) ) ; } } if ( v1 . size ( ) >= 2 ) { max1 = Math . max ( max1 , MaxLength ( v1 , i + 1 , m ) ) ; } else { max1 = Math . max ( max1 , i - 1 ) ; } } return max1 ; } public static void main ( String [ ] args ) { String s1 , s2 , s3 , s4 , s5 ; s1 = " abcde " ; s2 = " abcsd " ; s3 = " bcsdf " ; s4 = " abcda " ; s5 = " abced " ; Vector < String > v = new Vector < String > ( ) ; v . add ( s1 ) ; v . add ( s2 ) ; v . add ( s3 ) ; v . add ( s4 ) ; v . add ( s5 ) ; int m = v . get ( 0 ) . length ( ) ; System . out . print ( MaxLength ( v , 0 , m ) + 1 ) ; } }
Periodic Binary String With Minimum Period and a Given Binary String as Subsequence . | Java implementation to find the periodic string with minimum period ; Function to find the periodic string with minimum period ; Print the string S if it consists of similar elements ; Find the required periodic string with period 2 ; Driver Code
class GFG { static void findPeriodicString ( String S ) { int l = 2 * S . length ( ) ; int count = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '1' ) count ++ ; } if ( count == S . length ( ) count == 0 ) System . out . println ( S ) ; else { char arr [ ] = new char [ l ] ; for ( int i = 0 ; i < l ; i += 2 ) { arr [ i ] = '1' ; arr [ i + 1 ] = '0' ; } for ( int i = 0 ; i < l ; i ++ ) System . out . print ( arr [ i ] ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { String S = "1111001" ; findPeriodicString ( S ) ; } }
Check if two strings after processing backspace character are equal or not | Java implementation to Check if two strings after processing backspace character are equal or not ; Function to compare the two strings ; The index of character in string which would be removed when backspace is encountered ; Checks if a backspace is encountered or not . In case the first character is # , no change in string takes place ; The character after the # is added after the character at position rem_ind1 ; Checks if a backspace is encountered or not ; Check if the value of rem_ind1 and rem_ind2 is same , if not then it means they have different length ; Check if resultant strings are empty ; Check if each character in the resultant string is same ; Driver code ; Initialise two strings
import java . util . * ; import java . lang . * ; class GFG { static boolean compare ( StringBuilder s , StringBuilder t ) { int ps , pt , i ; ps = - 1 ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' # ' && ps != - 1 ) ps -= 1 ; else if ( s . charAt ( i ) != ' # ' ) { s . setCharAt ( ps + 1 , s . charAt ( i ) ) ; ps += 1 ; } } pt = - 1 ; for ( i = 0 ; i < t . length ( ) ; i ++ ) { if ( t . charAt ( i ) == ' # ' && pt != - 1 ) pt -= 1 ; else if ( t . charAt ( i ) != ' # ' ) { t . setCharAt ( pt + 1 , t . charAt ( i ) ) ; pt += 1 ; } } if ( pt != ps ) return false ; else if ( ps == - 1 && pt == - 1 ) return true ; else { for ( i = 0 ; i <= pt ; i ++ ) { if ( s . charAt ( i ) != t . charAt ( i ) ) return false ; } return true ; } } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( " geee # e # ks " ) ; StringBuilder t = new StringBuilder ( " gee # # eeks " ) ; if ( compare ( s , t ) ) System . out . print ( " True " ) ; else System . out . print ( " False " ) ; } }
Count the minimum number of groups formed in a string | Java implementation to Count the minimum number of groups formed in a string by replacing consecutive characters with same single character ; Function to count the minimum number of groups formed in the given string s ; Initializing count as one since the string is not NULL ; Comparing adjacent characters ; Driver Code
class GFG { static void group_formed ( String S ) { int count = 1 ; for ( int i = 0 ; i < S . length ( ) - 1 ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( i + 1 ) ) count += 1 ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String S = " TTWWW " ; group_formed ( S ) ; } }
Find the Substring with maximum product | Java program to find the maximum product subString ; Function to return the value of a character ; Function to find the maximum product subString ; To store subStrings ; Check if current product is maximum possible or not ; If product is 0 ; Return the subString with maximum product ; Driver code ; Function call
class GFG { static int value ( char x ) { return ( int ) ( x - ' a ' ) ; } static String maximumProduct ( String str , int n ) { String answer = " " , curr = " " ; long maxProduct = 0 , product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product *= 1L * value ( str . charAt ( i ) ) ; curr += str . charAt ( i ) ; if ( product >= maxProduct ) { maxProduct = product ; answer = curr ; } if ( product == 0 ) { product = 1 ; curr = " " ; } } return answer ; } public static void main ( String [ ] args ) { String str = " sdtfakdhdahdzz " ; int n = str . length ( ) ; System . out . print ( maximumProduct ( str , n ) + "NEW_LINE"); } }
Sum of minimum and the maximum difference between two given Strings | Java program to find the sum of the minimum and the maximum difference between two given Strings ; Function to find the sum of the minimum and the maximum difference between two given Strings ; Variables to store the minimum difference and the maximum difference ; Iterate through the length of the String as both the given Strings are of the same length ; For the maximum difference , we can replace " + " in both the Strings with different char ; For the minimum difference , we can replace " + " in both the Strings with the same char ; Driver code
class GFG { static void solve ( char [ ] a , char [ ] b ) { int l = a . length ; int min = 0 , max = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] == ' + ' b [ i ] == ' + ' a [ i ] != b [ i ] ) max ++ ; if ( a [ i ] != ' + ' && b [ i ] != ' + ' && a [ i ] != b [ i ] ) min ++ ; } System . out . print ( min + max + "NEW_LINE"); } public static void main ( String [ ] args ) { String s1 = " a + c " , s2 = " + + b " ; solve ( s1 . toCharArray ( ) , s2 . toCharArray ( ) ) ; } }
Minimum letters to be removed to make all occurrences of a given letter continuous | Java implementation of the above approach ; Function to find the minimum number of deletions required to make the occurrences of the given character K continuous ; Find the first occurrence of the given letter ; Iterate from the first occurrence till the end of the sequence ; Find the index from where the occurrence of the character is not continuous ; Update the answer with the number of elements between non - consecutive occurrences of the given letter ; Update the count for all letters which are not equal to the given letter ; Return the count ; Driver code ; Calling the function ; Calling the function
import java . util . * ; class GFG { static int noOfDeletions ( String str , char k ) { int ans = 0 , cnt = 0 , pos = 0 ; while ( pos < str . length ( ) && str . charAt ( pos ) != k ) { pos ++ ; } int i = pos ; while ( i < str . length ( ) ) { while ( i < str . length ( ) && str . charAt ( i ) == k ) { i = i + 1 ; } ans = ans + cnt ; cnt = 0 ; while ( i < str . length ( ) && str . charAt ( i ) != k ) { i = i + 1 ; cnt = cnt + 1 ; } } return ans ; } public static void main ( String [ ] args ) { String str1 = " ababababa " ; char k1 = ' a ' ; System . out . print ( noOfDeletions ( str1 , k1 ) + "NEW_LINE"); String str2 = " kprkkoinkopt " ; char k2 = ' k ' ; System . out . print ( noOfDeletions ( str2 , k2 ) + "NEW_LINE"); } }
Check whether an array of strings can correspond to a particular number X | Java implementation to check whether array of Strings can correspond to a number X ; Function to find the maximum base possible for the number N ; Function to find the decimal equivalent of the number ; Condition to check if the number is convertible to another base ; Function to check that the array can correspond to a number X ; counter to count the numbers those are convertible to X ; Loop to iterate over the array ; Convert the current String to every base for checking whether it will correspond to X from any base ; Condition to check if every number of the array can be converted to X ; Driver Code ; The set of Strings in base from [ 2 , 36 ]
class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } static void checkCorrespond ( String [ ] str , int x ) { int counter = 0 ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j <= 36 ; j ++ ) { if ( toDeci ( str [ i ] , j ) == x ) { counter ++ ; break ; } } } if ( counter == n ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } public static void main ( String [ ] args ) { int x = 16 ; String [ ] str = { "10000" , "20" , "16" } ; checkCorrespond ( str , x ) ; } }
Count of same length Strings that exists lexicographically in between two given Strings | Java program to find the count of same length Strings that exists lexicographically in between two given Strings ; Function to find the count of strings less than given string lexicographically ; Find length of string s ; Looping over the string characters and finding strings less than that character ; Function to find the count of same length Strings that exists lexicographically in between two given Strings ; Count string less than S1 ; Count string less than S2 ; Total strings between S1 and S2 would be difference between the counts - 1 ; If S1 is lexicographically greater than S2 then return 0 , otherwise return the value of totalString ; Driver code
import java . util . * ; class GFG { static int LexicoLesserStrings ( String s ) { int count = 0 ; int len ; len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { count += ( s . charAt ( i ) - ' a ' ) * Math . pow ( 26 , len - i - 1 ) ; } return count ; } static int countString ( String S1 , String S2 ) { int countS1 , countS2 , totalString ; countS1 = LexicoLesserStrings ( S1 ) ; countS2 = LexicoLesserStrings ( S2 ) ; totalString = countS2 - countS1 - 1 ; return ( totalString < 0 ? 0 : totalString ) ; } public static void main ( String args [ ] ) { String S1 , S2 ; S1 = " cda " ; S2 = " cef " ; System . out . println ( countString ( S1 , S2 ) ) ; } }
Longest Subsequence of a String containing only vowels | Java program to find the longest subsequence containing only vowels ; Function to check whether a character is vowel or not ; Returns true if x is vowel ; Function to find the longest subsequence which contain all vowels ; Length of the String ; Iterating through the String ; Checking if the character is a vowel or not ; If it is a vowel , then add it to the final String ; Driver code
class GFG { static boolean isVowel ( char x ) { x = Character . toLowerCase ( x ) ; return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static String longestVowelSubsequence ( String str ) { String answer = " " ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) { answer += str . charAt ( i ) ; } } return answer ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( longestVowelSubsequence ( str ) + "NEW_LINE"); } }
Maximum repeated frequency of characters in a given string | Java implementation to find the maximum repeated frequency of characters in the given String ; Function to find the maximum repeated frequency of the characters in the given String ; Hash - Array to store the frequency of characters ; Loop to find the frequency of the characters ; Hash map to store the occurrence of frequencies of characters ; Loop to find the maximum Repeated frequency from hash - map ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findMaxFrequency ( String s ) { int arr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) arr [ s . charAt ( i ) - ' a ' ] ++ ; HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) if ( arr [ i ] != 0 ) { if ( hash . containsKey ( arr [ i ] ) ) { hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } else { hash . put ( arr [ i ] , 1 ) ; } } int max_count = 0 , res = - 1 ; for ( Map . Entry < Integer , Integer > i : hash . entrySet ( ) ) { if ( max_count < i . getValue ( ) ) { res = i . getKey ( ) ; max_count = i . getValue ( ) ; } } System . out . println ( " Frequency ▁ " + res + " ▁ is ▁ repeated ▁ " + max_count + " ▁ times " ) ; } public static void main ( String [ ] args ) { String s = " geeksgeeks " ; findMaxFrequency ( s ) ; } }
Generate string with Hamming Distance as half of the hamming distance between strings A and B | Java implementation of the above approach ; Function to find the required string ; Find the hamming distance between A and B ; If distance is odd , then resultant string is not possible ; Make the resultant string ; To store the final string ; Pick k characters from each string ; Pick K characters from string B ; Pick K characters from string A ; Append the res characters from string to the resultant string ; Print the resultant string ; Driver 's Code  ; Function to find the resultant string
class GFG { static void findString ( String A , String B ) { int dist = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) ) dist ++ ; } if ( ( dist & 1 ) == 1 ) { System . out . println ( " Not ▁ Possible " ) ; } else { String res = " " ; int K = ( int ) dist / 2 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) && K > 0 ) { res += B . charAt ( i ) ; K -- ; } else if ( A . charAt ( i ) != B . charAt ( i ) ) { res += A . charAt ( i ) ; } else { res += A . charAt ( i ) ; } } System . out . println ( res ) ; } } public static void main ( String [ ] args ) { String A = "1001010" ; String B = "0101010" ; findString ( A , B ) ; } }
Longest suffix such that occurrence of each character is less than N after deleting atmost K characters | Java implementation to find longest suffix of the String such that occurrence of each character is less than K ; Function to find the maximum length suffix in the String ; Length of the String ; Map to store the number of occurrence of character ; Loop to iterate String from the last character ; Condition to check if the occurrence of each character is less than given number ; Condition when character cannot be deleted ; Longest suffix ; Driver Code ; Function call
class GFG { static void maximumSuffix ( String s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ ] = new int [ 26 ] ; String suffix = " " ; while ( i > - 1 ) { int index = s . charAt ( i ) - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s . charAt ( i ) ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } suffix = reverse ( suffix ) ; System . out . print ( suffix ) ; } 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 str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; } }
XOR of two Binary Strings | Java Implementation to find the XOR of the two Binary Strings ; Function to find the XOR of the two Binary Strings ; Loop to iterate over the Binary Strings ; If the Character matches ; Driver Code
import java . io . * ; class GFG { static String xoring ( String a , String b , int n ) { String ans = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) == b . charAt ( i ) ) ans += "0" ; else ans += "1" ; } return ans ; } public static void main ( String [ ] args ) { String a = "1010" ; String b = "1101" ; int n = a . length ( ) ; String c = xoring ( a , b , n ) ; System . out . println ( c ) ; } }
Find Kth largest string from the permutations of the string with two characters | Java implementation of above approach ; Function to print the kth largest String ; loop to iterate through series ; total takes the position of second y ; i takes the position of first y ; calculating first y position ; calculating second y position from first y ; print all x before first y ; print first y ; print all x between first y and second y ; print second y ; print x which occur after second y ; Driver code
class GFG { static void kthString ( int n , int k ) { int total = 0 ; int i = 1 ; while ( total < k ) { total = total + n - i ; i ++ ; } int first_y_position = i - 1 ; int second_y_position = k - ( total - n + first_y_position ) ; for ( int j = 1 ; j < first_y_position ; j ++ ) System . out . print ( " x " ) ; System . out . print ( " y " ) ; int j = first_y_position + 1 ; while ( second_y_position > 1 ) { System . out . print ( " x " ) ; second_y_position -- ; j ++ ; } System . out . print ( " y " ) ; while ( j < n ) { System . out . print ( " x " ) ; j ++ ; } } public static void main ( String [ ] args ) { int n = 5 ; int k = 7 ; kthString ( n , k ) ; } }