codeparrot/xlcost-text-to-code huggingface dataset:
Maximize length of the String by concatenating characters from an Array of Strings | Java program to implement the above approach ; Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static boolean check ( String s ) { HashSet < Character > a = new HashSet < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( a . contains ( s . charAt ( i ) ) ) { return false ; } a . add ( s . charAt ( i ) ) ; } return true ; } static ArrayList < String > helper ( ArrayList < String > arr , int ind ) { ArrayList < String > fin = new ArrayList < > ( ) ; fin . add ( " " ) ; if ( ind == arr . size ( ) ) return fin ; ArrayList < String > tmp = helper ( arr , ind + 1 ) ; ArrayList < String > ret = new ArrayList < > ( tmp ) ; for ( int i = 0 ; i < tmp . size ( ) ; i ++ ) { String test = tmp . get ( i ) + arr . get ( ind ) ; if ( check ( test ) ) ret . add ( test ) ; } return ret ; } static int maxLength ( ArrayList < String > arr ) { ArrayList < String > tmp = helper ( arr , 0 ) ; int len = 0 ; for ( int i = 0 ; i < tmp . size ( ) ; i ++ ) { len = len > tmp . get ( i ) . length ( ) ? len : tmp . get ( i ) . length ( ) ; } return len ; } public static void main ( String [ ] args ) { ArrayList < String > s = new ArrayList < > ( ) ; s . add ( " abcdefgh " ) ; System . out . println ( maxLength ( s ) ) ; } }
Perform range sum queries on string as per given condition | Java program for the above approach ; Function to perform range sum queries on String as per the given condition ; Initialize N by String size ; Create array A [ ] for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Driver Code ; Given String ; Given Queries ; Function call
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void Range_sum_query ( String S , Vector < pair > Query ) { int N = S . length ( ) ; int [ ] A = new int [ N ] ; A [ 0 ] = S . charAt ( 0 ) - ' a ' + 1 ; for ( int i = 1 ; i < N ; i ++ ) { A [ i ] = S . charAt ( i ) - ' a ' + 1 ; A [ i ] = A [ i ] + A [ i - 1 ] ; } for ( int i = 0 ; i < Query . size ( ) ; i ++ ) { if ( Query . get ( i ) . first == 1 ) { System . out . print ( A [ ( Query . get ( i ) . second ) - 1 ] + "NEW_LINE"); } else { System . out . print ( A [ ( Query . get ( i ) . second ) - 1 ] - A [ ( Query . get ( i ) . first ) - 2 ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { String S = " abcd " ; Vector < pair > Query = new Vector < pair > ( ) ; Query . add ( new pair ( 2 , 4 ) ) ; Query . add ( new pair ( 1 , 3 ) ) ; Range_sum_query ( S , Query ) ; } }
Generate all possible permutations of a Number divisible by N | Java program to implement the above approach ; Function to Swap two characters ; Function to generate all permutations and print the ones that are divisible by the N ; Convert String to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
class GFG { static void swap_ ( char [ ] a , int l , int i ) { char temp ; temp = a [ l ] ; a [ l ] = a [ i ] ; a [ i ] = temp ; } static void permute ( char [ ] str , int l , int r , int n ) { int i ; if ( l == r ) { int j = Integer . valueOf ( String . valueOf ( str ) ) ; if ( j % n == 0 ) System . out . print ( String . valueOf ( str ) + "NEW_LINE"); return ; } for ( i = l ; i < r ; i ++ ) { swap_ ( str , l , i ) ; permute ( str , l + 1 , r , n ) ; swap_ ( str , l , i ) ; } } public static void main ( String [ ] args ) { String str = "125" ; int n = 5 ; int len = str . length ( ) ; if ( len > 0 ) permute ( str . toCharArray ( ) , 0 , len , n ) ; } }
Check if binary representations of 0 to N are present as substrings in given binary string | Java program for the above approach ; Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the String as a subString or not ; To store the count of number exists as a subString ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the String s is a subString of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0' ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given number ; Function call
import java . util . * ; class GFG { static String decimalToBinary ( int N ) { String ans = " " ; while ( N > 0 ) { if ( N % 2 == 1 ) { ans = '1' + ans ; } else { ans = '0' + ans ; } N /= 2 ; } return ans ; } static String checkBinaryString ( String str , int N ) { int [ ] map = new int [ N + 10 ] ; int cnt = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( map [ i ] == 0 ) { int t = i ; String s = decimalToBinary ( t ) ; if ( str . contains ( s ) ) { while ( t > 0 && map [ t ] == 0 ) { map [ t ] = 1 ; cnt ++ ; t >>= 1 ; } } } } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == '0' ) { cnt ++ ; break ; } } if ( cnt == N + 1 ) return " True " ; else return " False " ; } public static void main ( String [ ] args ) { String str = "0110" ; int N = 3 ; System . out . print ( checkBinaryString ( str , N ) ) ; } }
Find the word with most anagrams in a given sentence | Java Program to find the word with most anagrams in a sentence ; Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
import java . util . * ; class GFG { private static String largestAnagramGrp ( String arr [ ] ) { int prime [ ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 } ; int max = - 1 ; long maxpdt = - 1 ; HashMap < Long , String > W = new HashMap < > ( ) ; HashMap < Long , Integer > P = new HashMap < > ( ) ; for ( String temp : arr ) { char c [ ] = temp . toCharArray ( ) ; long pdt = 1 ; for ( char t : c ) { pdt *= prime [ t - ' a ' ] ; } if ( P . containsKey ( pdt ) ) { P . put ( pdt , P . get ( pdt ) + 1 ) ; } else { W . put ( pdt , temp ) ; P . put ( pdt , 1 ) ; } } for ( Map . Entry < Long , Integer > e : P . entrySet ( ) ) { if ( max < e . getValue ( ) ) { max = e . getValue ( ) ; maxpdt = e . getKey ( ) ; } } return W . get ( maxpdt ) ; } public static void main ( String args [ ] ) { String S = " please ▁ be ▁ silent ▁ and ▁ listen " + " ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; String arr [ ] = S . split ( " [ ▁ ] + " ) ; System . out . println ( largestAnagramGrp ( arr ) ) ; } }
Replace every vowels with lexicographically next vowel in a String | Java program to convert all the vowels in in the String to the next vowel ; Function to replace every vowel with next vowel lexicographically ; storing the vowels in the map with custom numbers showing their index ; Iterate over the String ; If the current character is a vowel Find the index in Hash and Replace it with next vowel from Hash ; Driver code
import java . util . * ; class GFG { static String print_next_vovel_String ( char [ ] str ) { HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; m . put ( ' a ' , 0 ) ; m . put ( ' e ' , 1 ) ; m . put ( ' i ' , 2 ) ; m . put ( ' o ' , 3 ) ; m . put ( ' u ' , 4 ) ; char arr [ ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; int N = str . length ; for ( int i = 0 ; i < N ; i ++ ) { char c = str [ i ] ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { int index = m . get ( c ) + 1 ; int newindex = index % 5 ; str [ i ] = arr [ newindex ] ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( print_next_vovel_String ( str . toCharArray ( ) ) ) ; } }
Check if string is palindrome after removing all consecutive duplicates | Java program for the above approach ; Function to check if a String is palindrome or not ; Length of the String ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as str is palindromic ; Function to check if String str is palindromic after removing every consecutive characters from the str ; Length of the String str ; Create an empty compressed String ; The first character will always be included in the final String ; Check all the characters of the String ; If the current character is not same as its previous one , then insert it in the final String ; Check if the compressed String is a palindrome ; Driver Code ; Given String ; Function call
import java . util . * ; class GFG { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( len - i - 1 ) ) return false ; } return true ; } static boolean isCompressablePalindrome ( String str ) { int len = str . length ( ) ; String compressed = " " ; compressed = String . valueOf ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( i - 1 ) ) compressed += str . charAt ( i ) ; } return isPalindrome ( compressed ) ; } public static void main ( String [ ] args ) { String str = " abbcbbbaaa " ; if ( isCompressablePalindrome ( str ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
Count of substrings consisting only of vowels | Java program to Count all substrings in a string which contains only vowels ; Function to check vowel or not ; Function to find largest string which satisfy condition ; Check if current character is vowel ; Increment count ; Count all possible substrings of calculated length ; Reset the length ; Add remaining possible substrings consisting of vowels occupying last indices of the string ; Driver Program
import java . io . * ; public class GFG { static boolean isvowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static int CountTotal ( String str ) { int ans = 0 ; int n = str . length ( ) ; char [ ] s = str . toCharArray ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isvowel ( s [ i ] ) ) cnt += 1 ; else { ans += ( cnt * ( cnt + 1 ) / 2 ) ; cnt = 0 ; } } if ( cnt != 0 ) ans += ( cnt * ( cnt + 1 ) / 2 ) ; return ans ; } static public void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( CountTotal ( s ) ) ; } }
Number formed by flipping all bits to the left of rightmost set bit | Java program to find the integer formed after flipping all bits to the left of the rightmost set bit ; Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code
import java . util . * ; class GFG { static int totCount ; static int firstCount ; static void getTotCount ( int num ) { totCount = 1 ; firstCount = 1 ; int temp = 1 ; while ( ( num & temp ) == 0 ) { temp = temp << 1 ; totCount += 1 ; } firstCount = totCount ; temp = num >> totCount ; while ( temp != 0 ) { totCount += 1 ; temp = temp >> 1 ; } } static int flipBitsFromRightMostSetBit ( int num ) { getTotCount ( num ) ; int num1 = num ^ ( ( 1 << totCount ) - 1 ) ; num1 = num1 ^ ( ( 1 << firstCount ) - 1 ) ; return num1 ; } public static void main ( String [ ] args ) { int n = 120 ; System . out . println ( flipBitsFromRightMostSetBit ( n ) ) ; } }
Longest substring of vowels with no two adjacent alphabets same | Java implementation of the above approach ; Function to check a character is vowel or not ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; Stores length of current valid subString ; If curr and prev character are not same , include it ; If same as prev one , start new subString from here ; Store max in maxLen ; Driver code
public class GFG { static boolean isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static int findMaxLen ( String s ) { int maxLen = 0 ; int cur ; if ( isVowel ( s . charAt ( 0 ) ) ) maxLen = 1 ; cur = maxLen ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) cur += 1 ; else cur = 1 ; } else { cur = 0 ; } maxLen = Math . max ( cur , maxLen ) ; } return maxLen ; } public static void main ( String [ ] args ) { String Str = " aeoibsddaeiouudb " ; System . out . println ( findMaxLen ( Str ) ) ; } }
Count of non | Java program to count non - palindromic strings of length M using N distinct characters ; Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver code
import java . util . * ; class GFG { static long power ( long base , long pow ) { long res = 1 ; while ( pow > 0 ) { if ( ( pow & 1 ) == 1 ) res = ( res * base ) ; base = ( base * base ) ; pow >>= 1 ; } return res ; } static long countNonPalindromicString ( long n , long m ) { long total = power ( n , m ) ; long palindrome = power ( n , m / 2 + m % 2 ) ; long count = total - palindrome ; return count ; } public static void main ( String [ ] args ) { int n = 3 , m = 5 ; System . out . println ( countNonPalindromicString ( n , m ) ) ; } }
Count of K | Java program for the above approach ; To store the frequency array ; Function to check palindromic of of any subString using frequency array ; Initialise the odd count ; Traversing frequency array to compute the count of characters having odd frequency ; Returns true if odd count is atmost 1 ; Function to count the total number subString whose any permutations are palindromic ; Computing the frequency of first K character of the String ; To store the count of palindromic permutations ; Checking for the current window if it has any palindromic permutation ; Start and end point of window ; Decrementing count of first element of the window ; Incrementing count of next element of the window ; Checking current window character frequency count ; Return the final count ; Driver Code ; Given String str ; Window of size K ; Function Call
import java . util . * ; class GFG { static int [ ] freq = new int [ 26 ] ; static boolean checkPalindrome ( ) { int oddCnt = 0 ; for ( int x : freq ) { if ( x % 2 == 1 ) oddCnt ++ ; } return oddCnt <= 1 ; } static int countPalindromePermutation ( char [ ] s , int k ) { for ( int i = 0 ; i < k ; i ++ ) { freq [ s [ i ] - 97 ] ++ ; } int ans = 0 ; if ( checkPalindrome ( ) ) { ans ++ ; } int i = 0 , j = k ; while ( j < s . length ) { freq [ s [ i ++ ] - 97 ] -- ; freq [ s [ j ++ ] - 97 ] ++ ; if ( checkPalindrome ( ) ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String str = " abbaca " ; int K = 3 ; System . out . print ( countPalindromePermutation ( str . toCharArray ( ) , K ) + "NEW_LINE"); } }
Minimum flips required to form given binary string where every flip changes all bits to its right as well | Java program for the above approach ; Function to return the count of minimum flips required ; If curr occurs in the final string ; Switch curr to '0' if '1' or vice - versa ; Driver Code
import java . util . Arrays ; public class GFG { public static int minFlips ( String target ) { char curr = '1' ; int count = 0 ; for ( int i = 0 ; i < target . length ( ) ; i ++ ) { if ( target . charAt ( i ) == curr ) { count ++ ; curr = ( char ) ( 48 + ( curr + 1 ) % 2 ) ; } } return count ; } public static void main ( String args [ ] ) { String S = "011000" ; System . out . println ( minFlips ( S ) ) ; } }
Check if a number ends with another number or not | Java program for the above approach ; Function to check if B is a suffix of A or not ; Find the number of digit in B ; Subtract B from A ; Returns true , if B is a suffix of A ; Driver code ; Given numbers ; Function call ; If B is a suffix of A , then print " Yes "
import java . util . * ; class GFG { static boolean checkSuffix ( int A , int B ) { int digit_B = ( int ) ( Math . log10 ( B ) + 1 ) ; A -= B ; return ( A % ( int ) ( Math . pow ( 10 , digit_B ) ) > 0 ) ; } public static void main ( String [ ] args ) { int A = 12345 , B = 45 ; boolean result = checkSuffix ( A , B ) ; if ( ! result ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Minimum length of substring whose rotation generates a palindromic substring | Java Program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int count_min_length ( String s ) { int [ ] hash = new int [ 26 ] ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < 26 ; i ++ ) hash [ i ] = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( hash [ s . charAt ( i ) - ' a ' ] == - 1 ) hash [ s . charAt ( i ) - ' a ' ] = i ; else { if ( hash [ s . charAt ( i ) - ' a ' ] == i - 1 || hash [ s . charAt ( i ) - ' a ' ] == i - 2 ) return 0 ; ans = Math . min ( ans , i - hash [ s . charAt ( i ) - ' a ' ] - 1 ) ; hash [ s . charAt ( i ) - ' a ' ] = i ; } } if ( ans == Integer . MAX_VALUE ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String str = " abcdeba " ; System . out . println ( count_min_length ( str ) ) ; } }
Program to remove HTML tags from a given String | Java program for the above approach ; Function to remove the HTML tags from the given tags ; Use replaceAll function in regex to erase every tags enclosed in < > ; Print string after removing tags ; Driver Code ; Given String ; Function call to print the HTML string after removing tags
class GFG { static void RemoveHTMLTags ( String str ) { str = str . replaceAll ( " \\ < . * ? \\ > " , " " ) ; System . out . println ( str ) ; } public static void main ( String [ ] args ) { String str ; str = " < div > < b > Geeks ▁ for ▁ Geeks < / b > < / div > " ; RemoveHTMLTags ( str ) ; } }
Score of Parentheses using Tree | Java program to find the score of parentheses using Tree ; Customized tree class or struct , contains all required methods . ; Function to add a child into the list of children ; Function to change the parent pointer to the node passed ; Function to return the parent of the current node ; Function to compute the score recursively . ; Base case ; Adds scores of all children ; Function to create the tree structure ; Creating a node for every " ( ) " ; If we find " ( " we add a node as a child ; On finding " ) " which confirms that a pair is closed , we go back to the parent ; Driver code ; Generating the tree ; Computing the score
import java . util . * ; public class Main { static class TreeNode { public TreeNode parent = null ; public Vector < TreeNode > children = new Vector < TreeNode > ( ) ; public void addChild ( TreeNode node ) { children . add ( node ) ; } public void setParent ( TreeNode node ) { parent = node ; } public TreeNode getParent ( ) { return parent ; } public int computeScore ( ) { if ( children . size ( ) == 0 ) return 1 ; int res = 0 ; for ( TreeNode curr : children ) res += curr . computeScore ( ) ; if ( parent == null ) return res ; else return 2 * res ; } } static TreeNode computeTree ( String s ) { TreeNode current = new TreeNode ( ) ; TreeNode root = current ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) { TreeNode child = new TreeNode ( ) ; child . setParent ( current ) ; current . addChild ( child ) ; current = child ; } else { current = current . getParent ( ) ; } } return root ; } public static void main ( String [ ] args ) { String s = " ( ( ) ( ( ) ) ) " ; TreeNode root = computeTree ( s ) ; System . out . print ( root . computeScore ( ) ) ; } }
Move all occurrence of letter ' x ' from the string s to the end using Recursion | Java program for the above approach ; Recursive program to bring ' x ' to the end ; When the string is completed from reverse direction end of recursion ; If the character x is found ; Transverse the whole string ; Swap the x so that it moves to the last ; call to the smaller problem now ; Driver code ; Size of a ; Call to rec
import java . util . * ; class Main { public static void rec ( char a [ ] , int i ) { if ( i == 0 ) { System . out . println ( a ) ; return ; } if ( a [ i ] == ' x ' ) { int j = i ; while ( a [ j ] != ' \0' && a [ j + 1 ] != ' \0' ) { char temp = a [ j ] ; a [ j ] = a [ j + 1 ] ; a [ j + 1 ] = temp ; j ++ ; } } rec ( a , i - 1 ) ; } public static void main ( String [ ] args ) { char a [ ] = { ' g ' , ' e ' , ' e ' , ' k ' , ' x ' , ' s ' , ' x ' , ' x ' , ' k ' , ' s ' , ' \0' } ; int n = 10 ; rec ( a , n - 1 ) ; } }
Find the largest Alphabetic character present in the string | Java program to Find the Largest Alphabetic Character present in the string of both uppercase and lowercase English characters ; Function to find the Largest Alphabetic Character ; Array for keeping track of both uppercase and lowercase english alphabets ; Iterate from right side of array to get the largest index character ; Check for the character if both its uppercase and lowercase exist or not ; Return - 1 if no such character whose uppercase and lowercase present in string str ; Driver code
public class Main { public static String largestCharacter ( String str ) { boolean [ ] uppercase = new boolean [ 26 ] ; boolean [ ] lowercase = new boolean [ 26 ] ; char [ ] arr = str . toCharArray ( ) ; for ( char c : arr ) { if ( Character . isLowerCase ( c ) ) lowercase = true ; if ( Character . isUpperCase ( c ) ) uppercase = true ; } for ( int i = 25 ; i >= 0 ; i -- ) { if ( uppercase [ i ] && lowercase [ i ] ) return ( char ) ( i + ' A ' ) + " " ; } return " - 1" ; } public static void main ( String [ ] args ) { String str = " admeDCAB " ; System . out . println ( largestCharacter ( str ) ) ; } }