codeparrot/xlcost-text-to-code huggingface dataset:
Count binary strings with twice zeros in first half | Java program for finding number of binary strings number of '0' in first half is double the number of '0' in second half of string ; pre define some constant ; global values for pre computation ; function to print number of required string ; calculate answer using proposed algorithm ; main function
class GFG { static final long mod = 1000000007 ; static final long max = 1001 ; static long nCr [ ] [ ] = new long [ 1003 ] [ 1003 ] ; static void preComputeCoeff ( ) { for ( int i = 0 ; i < max ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = ( nCr [ i - 1 ] [ j - 1 ] + nCr [ i - 1 ] [ j ] ) % mod ; } } } static long computeStringCount ( int N ) { int n = N / 2 ; long ans = 0 ; for ( int i = 2 ; i <= n ; i += 2 ) ans = ( ans + ( ( nCr [ n ] [ i ] * nCr [ n ] [ i / 2 ] ) % mod ) ) % mod ; return ans ; } public static void main ( String [ ] args ) { preComputeCoeff ( ) ; int N = 3 ; System . out . println ( computeStringCount ( N ) ) ; } }
Count of ' GFG ' Subsequences in the given string | Java Program to find the " GFG " subsequence in the given string ; Print the count of " GFG " subsequence in the string ; Traversing the given string ; If the character is ' G ' , increment the count of ' G ' , increase the result and update the array . ; If the character is ' F ' , increment the count of ' F ' and update the array . ; Ignore other character . ; Driver code
public class GFG { static int max = 100 ; static void countSubsequence ( String s , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s . charAt ( i ) ) { case ' G ' : cntG ++ ; result += C ; break ; case ' F ' : cntF ++ ; C += cntG ; break ; default : continue ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { String s = " GFGFG " ; int n = s . length ( ) ; countSubsequence ( s , n ) ; } }
Lexicographical Maximum substring of string | Java program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; Driver code
class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( mx . compareTo ( str . substring ( i ) ) <= 0 ) { mx = str . substring ( i ) ; } } return mx ; } public static void main ( String [ ] args ) { String str = " ababaa " ; System . out . println ( LexicographicalMaxString ( str ) ) ; } }
Lexicographical Maximum substring of string | Java program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code
import java . io . * ; import java . util . * ; class GFG { static String LexicographicalMaxString ( String str ) { char maxchar = ' a ' ; ArrayList < Integer > index = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) >= maxchar ) { maxchar = str . charAt ( i ) ; index . add ( i ) ; } } String maxstring = " " ; for ( int i = 0 ; i < index . size ( ) ; i ++ ) { if ( str . substring ( index . get ( i ) , str . length ( ) ) . compareTo ( maxstring ) > 0 ) { maxstring = str . substring ( index . get ( i ) , str . length ( ) ) ; } } return maxstring ; } public static void main ( String [ ] args ) { String str = " acbacbc " ; System . out . println ( LexicographicalMaxString ( str ) ) ; } }
Print longest palindrome word in a sentence | Java program to print longest palindrome word in a sentence and its length ; Function to check if a word is palindrome ; making the check case case insensitive ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
public class GFG { static boolean checkPalin ( String word ) { int n = word . length ( ) ; word = word . toLowerCase ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . charAt ( i ) != word . charAt ( n - 1 ) ) return false ; return true ; } static String longestPalin ( String str ) { str = str + " ▁ " ; String longestword = " " , word = " " ; int length , length1 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { length = word . length ( ) ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } public static void main ( String args [ ] ) { String s = new String ( " My ▁ name ▁ is ▁ ava ▁ " + " and ▁ i ▁ love ▁ Geeksforgeeks " ) ; if ( longestPalin ( s ) == " " ) System . out . println ( " No ▁ Palindrome " + " ▁ Word " ) ; else System . out . println ( longestPalin ( s ) ) ; } }
Number of common base strings for two strings | Java program to count common base strings of s1 and s2 . ; function for finding common divisor ; Checking if ' base ' is base String of ' s1' ; Checking if ' base ' is base String of ' s2' ; Driver code
class GFG { static boolean isCommonBase ( String base , String s1 , String s2 ) { for ( int j = 0 ; j < s1 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s1 . charAt ( j ) ) { return false ; } } for ( int j = 0 ; j < s2 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s2 . charAt ( j ) ) { return false ; } } return true ; } static int countCommonBases ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; int count = 0 ; for ( int i = 1 ; i <= Math . min ( n1 , n2 ) ; i ++ ) { String base = s1 . substring ( 0 , i ) ; if ( isCommonBase ( base , s1 , s2 ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s1 = " pqrspqrs " ; String s2 = " pqrspqrspqrspqrs " ; System . out . println ( countCommonBases ( s1 , s2 ) ) ; } }
Removing elements between the two zeros | Java program to delete elements between zeros ; Function to find the string after operation ; use for loop to remove the character between two zeros ; Checking for character Between two zeros ; deleting the character At specific position ; updating the length of the string ; Driver code
import java . util . * ; public class GFG { static String findstring ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( s . charAt ( i - 1 ) == '0' && s . charAt ( i + 1 ) == '0' ) ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; i -- ; if ( i > 0 && s . charAt ( i - 1 ) == '0' ) i -- ; n = s . length ( ) ; } } return s ; } public static void main ( String [ ] args ) { String s = "100100" ; System . out . println ( findstring ( s ) ) ; } }
Perfect Square String | Java program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ASCII value of the string ; Find floating point value of square root of x . ; If square root is an integer ; Driver code
import java . io . * ; class GFG { static boolean isPerfectSquareString ( String str ) { int sum = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) sum += ( int ) str . charAt ( i ) ; long squareRoot = ( long ) Math . sqrt ( sum ) ; return ( ( squareRoot - Math . floor ( squareRoot ) ) == 0 ) ; } public static void main ( String [ ] args ) { String str = " d " ; if ( isPerfectSquareString ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count substrings with each character occurring at most k times | Java program to count number of substrings in which each character has count less than or equal to k . ; function to find number of substring in which each character has count less than or equal to k . ; initialize left and right pointer to 0 ; an array to keep track of count of each alphabet ; decrement the count ; increment left pointer ; Driver code
class GFG { static int find_sub ( String s , int k ) { int len = s . length ( ) ; int lp = 0 , rp = 0 ; int ans = 0 ; int [ ] hash_char = new int [ 26 ] ; for ( ; rp < len ; rp ++ ) { hash_char [ s . charAt ( rp ) - ' a ' ] ++ ; while ( hash_char [ s . charAt ( rp ) - ' a ' ] > k ) { hash_char [ s . charAt ( lp ) - ' a ' ] -- ; lp ++ ; } ans += rp - lp + 1 ; } return ans ; } public static void main ( String [ ] args ) { String S = " aaabb " ; int k = 2 ; System . out . println ( find_sub ( S , k ) ) ; } }
Remove consecutive vowels from string | Java program for printing sentence without repetitive vowels ; function which returns True or False for occurrence of a vowel ; this compares vowel with character ' c ' ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean is_vow ( char c ) { return ( c == ' a ' ) || ( c == ' e ' ) || ( c == ' i ' ) || ( c == ' o ' ) || ( c == ' u ' ) ; } static void removeVowels ( String str ) { System . out . print ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) if ( ( ! is_vow ( str . charAt ( i - 1 ) ) ) || ( ! is_vow ( str . charAt ( i ) ) ) ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks " ; removeVowels ( str ) ; } }
Find the Number which contain the digit d | Java program to print the number which contain the digit d from 0 to n ; function to display the values ; Converting d to character ; Loop to check each digit one by one . ; initialize the string ; checking for digit ; Driver code
public class GFG { static void printNumbers ( int n , int d ) { String st = " " + d ; char ch = st . charAt ( 0 ) ; for ( int i = 0 ; i <= n ; i ++ ) { st = " " ; st = st + i ; if ( i == d || st . indexOf ( ch ) >= 0 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 100 , d = 5 ; printNumbers ( n , d ) ; } }
Transform a string such that it has abcd . . z as a subsequence | Java Program to transform the given string to contain the required subsequence ; function to transform string with string passed as reference ; initializing the variable ch to ' a ' ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; Driver Code
import java . io . * ; public class GFG { static boolean transformString ( StringBuilder s ) { char ch = ' a ' ; if ( s . length ( ) < 26 ) return false ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( int ) ch > ( int ) ' z ' ) break ; if ( s . charAt ( i ) <= ch ) { s . setCharAt ( i , ch ) ; ch = ( char ) ( ( int ) ch + 1 ) ; } } if ( ch <= ' z ' ) return false ; return true ; } public static void main ( String args [ ] ) { StringBuilder str = new StringBuilder ( " aaaaaaaaaaaaaaaaaaaaaaaaaa " ) ; if ( transformString ( str ) ) System . out . println ( str . toString ( ) ) ; else System . out . println ( " Not ▁ Possible " ) ; } }
Number of pairs with Pandigital Concatenation | Java program to find all Pandigital concatenations of two strings . ; Checks if a given string is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code
import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > v = new ArrayList < String > ( ) ; static int isPanDigital ( String s ) { int digits [ ] = new int [ 10 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) digits [ s . charAt ( i ) - ( int ) '0' ] = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( digits [ i ] == 0 ) return 0 ; return 1 ; } static int countPandigitalPairs ( ) { int pairs = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) if ( isPanDigital ( v . get ( i ) + v . get ( j ) ) == 1 ) pairs ++ ; return pairs ; } public static void main ( String args [ ] ) { v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( ) ) ; } }
Number of pairs with Pandigital Concatenation | Java program to count PanDigital pairs ; Stores digits present in String v [ i ] atleast once . We use a set as we only need digits which exist only once ( irrespective of reputation ) ; Calculate the mask by considering all digits existing atleast once ; Increment the frequency of this mask ; Returns number of pairs of Strings resulting in Pandigital Concatenations ; All possible Strings lie between 1 and 1023 so we iterate over every possible mask ; If the concatenation results in mask of Pandigital Concatenation , calculate all pairs formed with Masks i and j ; Since every pair is considers twice , we get rid of half of these ; Find frequencies of all masks in given vector of Strings ; Return all possiblg concatenations . ; Driver code
import java . util . * ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( Vector < String > v , HashMap < Integer , Integer > freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; HashSet < Integer > digits = new HashSet < > ( ) ; for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) digits . add ( v . get ( i ) . charAt ( j ) - '0' ) ; for ( int it : digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . containsKey ( mask ) ) { freq . put ( mask , freq . get ( mask ) + 1 ) ; } else { freq . put ( mask , 1 ) ; } } } static int pandigitalConcatenations ( HashMap < Integer , Integer > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . containsKey ( j ) && freq . containsKey ( i ) ) { if ( i == j ) ans += ( freq . get ( i ) * ( freq . get ( i ) - 1 ) ) ; else ans += ( freq . get ( i ) * freq . get ( j ) ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( Vector < String > v ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( v ) + "NEW_LINE"); } }
Print the arranged positions of characters to make palindrome | JAVA program to print original positions of characters in a String after rearranging and forming a palindrome ; Maximum number of characters ; Insert all positions of every character in the given String . ; find the number of odd elements . Takes O ( n ) ; A palindrome cannot contain more than 1 odd characters ; Print positions in first half of palindrome ; Consider one instance odd character ; Print positions in second half of palindrome ; Driver code
import java . util . * ; class GFG { static int MAX = 256 ; static void printPalindromePos ( String str ) { Vector < Integer > [ ] pos = new Vector [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) pos [ i ] = new Vector < Integer > ( ) ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) pos [ str . charAt ( i ) ] . add ( i + 1 ) ; int oddCount = 0 ; char oddChar = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pos [ i ] . size ( ) % 2 != 0 ) { oddCount ++ ; oddChar = ( char ) i ; } } if ( oddCount > 1 ) System . out . print ( " NO ▁ PALINDROME " ) ; for ( int i = 0 ; i < MAX ; i ++ ) { int mid = pos [ i ] . size ( ) / 2 ; for ( int j = 0 ; j < mid ; j ++ ) System . out . print ( pos [ i ] . get ( j ) + " ▁ " ) ; } if ( oddCount > 0 ) { int last = pos [ oddChar ] . size ( ) - 1 ; System . out . print ( pos [ oddChar ] . get ( last ) + " ▁ " ) ; pos [ oddChar ] . remove ( pos [ oddChar ] . size ( ) - 1 ) ; } for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int count = pos [ i ] . size ( ) ; for ( int j = count / 2 ; j < count ; j ++ ) System . out . print ( pos [ i ] . get ( j ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String s = " geeksgk " ; printPalindromePos ( s ) ; } }
Minimum changes to a string to make all substrings distinct | JAVA program to count number of changes to make all substrings distinct . ; Returns minimum changes to str so that no substring is repeated . ; If length is more than maximum allowed characters , we cannot get the required string . ; Variable to store count of distinct characters ; To store counts of different characters ; Answer is , n - number of distinct char ; Driver function
import java . lang . * ; import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; public static int minChanges ( String str ) { int n = str . length ( ) ; if ( n > MAX_CHAR ) return - 1 ; int dist_count = 0 ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ str . charAt ( i ) - ' a ' ] == 0 ) dist_count ++ ; count [ str . charAt ( i ) - ' a ' ] ++ ; } return ( n - dist_count ) ; } public static void main ( String [ ] args ) { String str = " aebaecedabbee " ; System . out . println ( minChanges ( str ) ) ; } }
Reverse each word in a linked list node | Java program to reverse each word in a linked list ; Linked list Node ure ; Function to create newNode in a linked list ; reverse each node data ; iterate each node and call reverse_word for each node data ; printing linked list ; Driver program
class GFG { static class Node { String c ; Node next ; } ; static Node newNode ( String c ) { Node temp = new Node ( ) ; temp . c = c ; temp . next = null ; return temp ; } ; static String reverse_word ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) s = str . charAt ( i ) + s ; return s ; } static Node reverse ( Node head ) { Node ptr = head ; while ( ptr != null ) { ptr . c = reverse_word ( ptr . c ) ; ptr = ptr . next ; } return head ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . c + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = newNode ( " Geeksforgeeks " ) ; head . next = newNode ( " a " ) ; head . next . next = newNode ( " computer " ) ; head . next . next . next = newNode ( " science " ) ; head . next . next . next . next = newNode ( " portal " ) ; head . next . next . next . next . next = newNode ( " for " ) ; head . next . next . next . next . next . next = newNode ( " geeks " ) ; System . out . print ( "List before reverse: NEW_LINE"); printList ( head ) ; head = reverse ( head ) ; System . out . print ( " List after reverse : "); printList ( head ) ; } }
Number of strings of length N with no palindromic sub string | Java program to count number of strings of size m such that no substring is palindrome . ; Return the count of strings with no palindromic substring . ; Driven Program
import java . io . * ; class GFG { static int numofstring ( int n , int m ) { if ( n == 1 ) return m ; if ( n == 2 ) return m * ( m - 1 ) ; return m * ( m - 1 ) * ( int ) Math . pow ( m - 2 , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . println ( numofstring ( n , m ) ) ; } }
Count special palindromes in a String | Java program to count special Palindromic substring ; Function to count special Palindromic susbstring ; store count of special Palindromic substring ; it will store the count of continues same char ; traverse string character from left to right ; store same character count ; count smiler character ; Case : 1 so total number of substring that we can generate are : K * ( K + 1 ) / 2 here K is sameCharCount ; store current same char count in sameChar [ ] array ; increment i ; Case 2 : Count all odd length Special Palindromic substring ; if current character is equal to previous one then we assign Previous same character count to current one ; case 2 : odd length ; subtract all single length substring ; Driver code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public static int CountSpecialPalindrome ( String str ) { int n = str . length ( ) ; int result = 0 ; int [ ] sameChar = new int [ n ] ; for ( int v = 0 ; v < n ; v ++ ) sameChar [ v ] = 0 ; int i = 0 ; while ( i < n ) { int sameCharCount = 1 ; int j = i + 1 ; while ( j < n && str . charAt ( i ) == str . charAt ( j ) ) { sameCharCount ++ ; j ++ ; } result += ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; sameChar [ i ] = sameCharCount ; i = j ; } for ( int j = 1 ; j < n ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) ) sameChar [ j ] = sameChar [ j - 1 ] ; if ( j > 0 && j < ( n - 1 ) && ( str . charAt ( j - 1 ) == str . charAt ( j + 1 ) && str . charAt ( j ) != str . charAt ( j - 1 ) ) ) result += Math . min ( sameChar [ j - 1 ] , sameChar [ j + 1 ] ) ; } return result - n ; } public static void main ( String args [ ] ) { String str = " abccba " ; System . out . print ( CountSpecialPalindrome ( str ) ) ; } }
Print Bracket Number | Java implementation to print the bracket number ; function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printBracketNumber ( String exp , int n ) { int left_bnum = 1 ; Stack < Integer > right_bnum = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp . charAt ( i ) == ' ( ' ) { System . out . print ( left_bnum + " ▁ " ) ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp . charAt ( i ) == ' ) ' ) { System . out . print ( right_bnum . peek ( ) + " ▁ " ) ; right_bnum . pop ( ) ; } } } public static void main ( String args [ ] ) { String exp = " ( a + ( b * c ) ) + ( d / e ) " ; int n = exp . length ( ) ; printBracketNumber ( exp , n ) ; } }
Find if a string starts and ends with another given string | Java program to find if a given corner string is present at corners . ; If length of corner string is more , it cannot be present at corners . ; Return true if corner string is present at both corners of given string . ; Driver Code
import java . io . * ; class GFG { static boolean isCornerPresent ( String str , String corner ) { int n = str . length ( ) ; int cl = corner . length ( ) ; if ( n < cl ) return false ; return ( str . substring ( 0 , cl ) . equals ( corner ) && str . substring ( n - cl , n ) . equals ( corner ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String corner = " geeks " ; if ( isCornerPresent ( str , corner ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }