codeparrot/xlcost-text-to-code huggingface dataset:
Print all words matching a pattern in CamelCase Notation Dictionary | Java program to print all words in the CamelCase dictionary that matches with a given pattern ; Alphabet size ( # of upper - Case characters ) ; A Trie node ; isLeaf is true if the node represents end of a word ; vector to store list of complete words in leaf node ; Function to insert word into the Trie ; consider only uppercase characters ; get current character position ; mark last node as leaf ; push word into vector associated with leaf node ; Function to print all children of Trie node root ; if current node is leaf ; recurse for all children of root node ; search for pattern in Trie and print all words matching that pattern ; Invalid pattern ; print all words matching that pattern ; Main function to print all words in the CamelCase dictionary that matches with a given pattern ; construct Trie root node ; Construct Trie from given dict ; search for pattern in Trie ; Driver function ; dictionary of words where each word follows CamelCase notation ; pattern consisting of uppercase characters only
import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; public class CamelCase { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; boolean isLeaf ; List < String > word ; public TrieNode ( ) { isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; word = new ArrayList < String > ( ) ; } } static TrieNode root ; static void insert ( String word ) { int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < word . length ( ) ; level ++ ) { if ( Character . isLowerCase ( word . charAt ( level ) ) ) continue ; index = word . charAt ( level ) - ' A ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isLeaf = true ; ( pCrawl . word ) . add ( word ) ; } static void printAllWords ( TrieNode root ) { if ( root . isLeaf ) { for ( String str : root . word ) System . out . println ( str ) ; } for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) { TrieNode child = root . children [ i ] ; if ( child != null ) printAllWords ( child ) ; } } static boolean search ( String pattern ) { int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < pattern . length ( ) ; level ++ ) { index = pattern . charAt ( level ) - ' A ' ; if ( pCrawl . children [ index ] == null ) return false ; pCrawl = pCrawl . children [ index ] ; } printAllWords ( pCrawl ) ; return true ; } static void findAllWords ( List < String > dict , String pattern ) { root = new TrieNode ( ) ; for ( String word : dict ) insert ( word ) ; if ( ! search ( pattern ) ) System . out . println ( " No ▁ match ▁ found " ) ; } public static void main ( String args [ ] ) { List < String > dict = Arrays . asList ( " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " ) ; String pattern = " HT " ; findAllWords ( dict , pattern ) ; } }
Generate all binary strings from given pattern | Recursive Java program to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; NOTE : Need to backtrack as string is passed by reference to the function ; driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class binStr { public static void print ( char str [ ] , int index ) { if ( index == str . length ) { System . out . println ( str ) ; return ; } if ( str [ index ] == ' ? ' ) { str [ index ] = '0' ; print ( str , index + 1 ) ; str [ index ] = '1' ; print ( str , index + 1 ) ; str [ index ] = ' ? ' ; } else print ( str , index + 1 ) ; } public static void main ( String [ ] args ) { String input = "1 ? ? 0?101" ; char [ ] str = input . toCharArray ( ) ; print ( str , 0 ) ; } }
Find the most frequent digit without using array / string | Finds maximum occurring digit without using any array / string ; Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Initialize result which is a digit ; Initialize count of result ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
import java . io . * ; class GFG { static int countOccurrences ( int x , int d ) { int count = 0 ; while ( x > 0 ) { if ( x % 10 == d ) count ++ ; x = x / 10 ; } return count ; } static int maxOccurring ( int x ) { if ( x < 0 ) x = - x ; int result = 0 ; int max_count = 1 ; for ( int d = 0 ; d <= 9 ; d ++ ) { int count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } public static void main ( String [ ] args ) { int x = 1223355 ; System . out . println ( " Max ▁ occurring ▁ digit ▁ is ▁ " + maxOccurring ( x ) ) ; } }
Remove recurring digits in a given number | Java program to remove recurring digits from a given number ; Removes recurring digits in num [ ] ; int j = 0 ; Index in modified string ; Traverse digits of given number one by one ; Copy the first occurrence of new digit ; Remove repeating occurrences of digit ; Driver code
class GFG { static String removeRecurringDigits ( char num [ ] ) { int len = num . length ; String s = " " ; for ( int i = 0 ; i < len ; i ++ ) { s += String . valueOf ( num [ i ] ) ; while ( i + 1 < len && num [ i ] == num [ i + 1 ] ) { i ++ ; } } return s ; } public static void main ( String [ ] args ) { char num [ ] = "1299888833" . toCharArray ( ) ; System . out . print ( " Modified ▁ number ▁ is ▁ " + removeRecurringDigits ( num ) ) ; } }
Find the maximum subarray XOR in a given array | A simple Java program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; to store xor of current subarray ; Pick ending points of subarrays starting with i ; Driver program to test above functions
class GFG { static int maxSubarrayXOR ( int arr [ ] , int n ) { int ans = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_xor = curr_xor ^ arr [ j ] ; ans = Math . max ( ans , curr_xor ) ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 1 , 2 , 12 } ; int n = arr . length ; System . out . println ( " Max ▁ subarray ▁ XOR ▁ is ▁ " + maxSubarrayXOR ( arr , n ) ) ; } }
Recursive Implementation of atoi ( ) | Recursive Java program to compute atoi ( ) ; Recursive function to compute atoi ( ) ; Base case ( Only one digit ) ; If more than 1 digits , recur for ( n - 1 ) , multiplu result with 10 and add last digit ; Driver code
class GFG { static int myAtoiRecursive ( String str , int n ) { if ( n == 1 ) { return str . charAt ( 0 ) - '0' ; } return ( 10 * myAtoiRecursive ( str , n - 1 ) + str . charAt ( n - 1 ) - '0' ) ; } public static void main ( String [ ] s ) { String str = "112" ; int n = str . length ( ) ; System . out . println ( myAtoiRecursive ( str , n ) ) ; } }
Print string of odd length in ' X ' format | Java program to print the given pattern ; Function to print the given string in respective pattern ; Print characters at corresponding places satisfying the two conditions ; Print blank space at rest of places ; Driver code
import java . io . * ; class GFG { static void printPattern ( String str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { if ( ( i == j ) || ( i + j == len - 1 ) ) System . out . print ( str . charAt ( j ) ) ; else System . out . print ( " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; printPattern ( str , len ) ; } }
Find the longest substring with k unique characters in a given string | Java program to find the longest substring with k unique characters in a given string ; This function calculates number of unique characters using a associative array count [ ] . Returns true if no . of characters are less than required else returns false . ; Return true if k is greater than or equal to val ; Finds the maximum substring with exactly k unique chars ; Associative array to store the count of characters ; Traverse the string , Fills the associative array count [ ] and count number of unique characters ; If there are not enough unique characters , show an error message . ; Otherwise take a window with first element in it . start and end variables . ; Also initialize values for result longest window ; Initialize associative array count [ ] with zero ; put the first character ; Start from the second character and add characters in window according to above explanation ; Add the character ' s [ i ] ' to current window ; If there are more than k unique characters in current window , remove from left side ; Update the max window size if required ; Driver Code
import java . util . Arrays ; class GFG { final static int MAX_CHARS = 26 ; static boolean isValid ( int count [ ] , int k ) { int val = 0 ; for ( int i = 0 ; i < MAX_CHARS ; i ++ ) { if ( count [ i ] > 0 ) { val ++ ; } } return ( k >= val ) ; } static void kUniques ( String s , int k ) { int u = 0 ; int n = s . length ( ) ; int count [ ] = new int [ MAX_CHARS ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ s . charAt ( i ) - ' a ' ] == 0 ) { u ++ ; } count [ s . charAt ( i ) - ' a ' ] ++ ; } if ( u < k ) { System . out . print ( " Not ▁ enough ▁ unique ▁ characters " ) ; return ; } int curr_start = 0 , curr_end = 0 ; int max_window_size = 1 ; int max_window_start = 0 ; Arrays . fill ( count , 0 ) ; count [ s . charAt ( 0 ) - ' a ' ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { count [ s . charAt ( i ) - ' a ' ] ++ ; curr_end ++ ; while ( ! isValid ( count , k ) ) { count [ s . charAt ( curr_start ) - ' a ' ] -- ; curr_start ++ ; } if ( curr_end - curr_start + 1 > max_window_size ) { max_window_size = curr_end - curr_start + 1 ; max_window_start = curr_start ; } } System . out . println ( " Max ▁ substring ▁ is ▁ : ▁ " + s . substring ( max_window_start , max_window_start + max_window_size ) + " ▁ with ▁ length ▁ " + max_window_size ) ; } static public void main ( String [ ] args ) { String s = " aabacbebebe " ; int k = 3 ; kUniques ( s , k ) ; } }
Function to find Number of customers who could not get a computer | JAVA program to find number of customers who couldn 't get a resource. ; n is number of computers in cafe . ' seq ' is given sequence of customer entry , exit events ; seen [ i ] = 0 , indicates that customer ' i ' is not in cafe seen [ 1 ] = 1 , indicates that customer ' i ' is in cafe but computer is not assigned yet . seen [ 2 ] = 2 , indicates that customer ' i ' is in cafe and has occupied a computer . ; Initialize result which is number of customers who could not get any computer . ; To keep track of occupied computers ; Traverse the input sequence ; Find index of current character in seen [ 0. . .25 ] ; If First occurrence of ' seq [ i ] ' ; set the current character as seen ; If number of occupied computers is less than n , then assign a computer to new customer ; Set the current character as occupying a computer ; Else this customer cannot get a computer , increment result ; If this is second occurrence of ' seq [ i ] ' ; Decrement occupied only if this customer was using a computer ; Driver program
class GFG { static int MAX_CHAR = 26 ; static int runCustomerSimulation ( int n , char [ ] seq ) { char [ ] seen = new char [ MAX_CHAR ] ; int res = 0 ; int occupied = 0 ; for ( int i = 0 ; i < seq . length ; i ++ ) { int ind = seq [ i ] - ' A ' ; if ( seen [ ind ] == 0 ) { seen [ ind ] = 1 ; if ( occupied < n ) { occupied ++ ; seen [ ind ] = 2 ; } else res ++ ; } else { if ( seen [ ind ] == 2 ) occupied -- ; seen [ ind ] = 0 ; } } return res ; } public static void main ( String [ ] args ) { System . out . println ( runCustomerSimulation ( 2 , " ABBAJJKZKZ " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 3 , " GACCBDDBAGEE " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 3 , " GACCBGDDBAEE " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 1 , " ABCBCA " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 1 , " ABCBCADEED " . toCharArray ( ) ) ) ; } }
Check if characters of a given string can be rearranged to form a palindrome | Java Implementation of the above approach ; bitvector to store the record of which character appear odd and even number of times ; Driver Code
import java . io . * ; class GFG { static boolean canFormPalindrome ( String a ) { int bitvector = 0 , mask = 0 ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { int x = a . charAt ( i ) - ' a ' ; mask = 1 << x ; bitvector = bitvector ^ mask ; } return ( bitvector & ( bitvector - 1 ) ) == 0 ; } public static void main ( String [ ] args ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Print all pairs of anagrams in a given array of strings | Java program to Print all pairs of anagrams in a given array of strings ; function to check whether two strings are anagram of each other ; Create two count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; This function prints all anagram pairs in a given array of strings ; Driver program to test to pront printDups
public class GFG { static final int NO_OF_CHARS = 256 ; static boolean areAnagram ( String str1 , String str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . length ( ) && i < str2 . length ( ) ; i ++ ) { count [ str1 . charAt ( i ) ] ++ ; count [ str2 . charAt ( i ) ] -- ; } if ( str1 . length ( ) != str2 . length ( ) ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } static void findAllAnagrams ( String arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( areAnagram ( arr [ i ] , arr [ j ] ) ) System . out . println ( arr [ i ] + " ▁ is ▁ anagram ▁ of ▁ " + arr [ j ] ) ; } public static void main ( String args [ ] ) { String arr [ ] = { " geeksquiz " , " geeksforgeeks " , " abcd " , " forgeeksgeeks " , " zuiqkeegs " } ; int n = arr . length ; findAllAnagrams ( arr , n ) ; } }
Program to print all palindromes in a given range | Java Program to print all palindromes in a given range ; A function to check if n is palindrome ; Find reverse of n ; If n and rev are same , then n is palindrome ; prints palindrome between min and max ; Driver Code
class GFG { static int isPalindrome ( int n ) { int rev = 0 ; for ( int i = n ; i > 0 ; i /= 10 ) rev = rev * 10 + i % 10 ; return ( n == rev ) ? 1 : 0 ; } static void countPal ( int min , int max ) { for ( int i = min ; i <= max ; i ++ ) if ( isPalindrome ( i ) == 1 ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { countPal ( 100 , 2000 ) ; } }
Length of the longest substring without repeating characters | Java program to find the length of the longest substring without repeating characters ; Result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int longestUniqueSubsttr ( String str ) { int n = str . length ( ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean [ ] visited = new boolean [ 256 ] ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str . charAt ( j ) ] == true ) break ; else { res = Math . max ( res , j - i + 1 ) ; visited [ str . charAt ( j ) ] = true ; } } visited [ str . charAt ( i ) ] = false ; } return res ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( " The ▁ input ▁ string ▁ is ▁ " + str ) ; int len = longestUniqueSubsttr ( str ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " + " non - repeating ▁ character ▁ " + " substring ▁ is ▁ " + len ) ; } }
Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver code
import java . util . * ; class GFG { static int longestUniqueSubsttr ( String s ) { HashMap < Character , Integer > seen = new HashMap < > ( ) ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . length ( ) ; end ++ ) { if ( seen . containsKey ( s . charAt ( end ) ) ) { start = Math . max ( start , seen . get ( s . charAt ( end ) ) + 1 ) ; } seen . put ( s . charAt ( end ) , end ) ; maximum_length = Math . max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( " The ▁ input ▁ String ▁ is ▁ " + s ) ; int length = longestUniqueSubsttr ( s ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is ▁ " + length ) ; } }
Find the smallest window in a string containing all characters of another string | Function ; Answer length of ans ; starting index of ans ; creating map ; References of Window ; Traversing the window ; Calculations ; Condition matching ; Sorting ans ; Sliding I Calculation for removing I ; Driver code
import java . util . * ; class GFG { static String Minimum_Window ( char [ ] s , char [ ] t ) { int m [ ] = new int [ 256 ] ; int ans = Integer . MAX_VALUE ; int start = 0 ; int count = 0 ; for ( int i = 0 ; i < t . length ; i ++ ) { if ( m [ t [ i ] ] == 0 ) count ++ ; m [ t [ i ] ] ++ ; } int i = 0 ; int j = 0 ; while ( j < s . length ) { m [ s [ j ] ] -- ; if ( m [ s [ j ] ] == 0 ) count -- ; if ( count == 0 ) { while ( count == 0 ) { if ( ans > j - i + 1 ) { ans = Math . min ( ans , j - i + 1 ) ; start = i ; } m [ s [ i ] ] ++ ; if ( m [ s [ i ] ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != Integer . MAX_VALUE ) return String . valueOf ( s ) . substring ( start , ans + start ) ; else return " - 1" ; } public static void main ( String [ ] args ) { String s = " ADOBECODEBANC " ; String t = " ABC " ; System . out . print ( " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : ▁ " ) ; System . out . print ( Minimum_Window ( s . toCharArray ( ) , t . toCharArray ( ) ) ) ; } }
Run Length Encoding | Java program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
public class RunLength_Encoding { public static void printRLE ( String str ) { int n = str . length ( ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) { count ++ } else { System . out . print ( str . charAt ( i ) ) ; System . out . print ( count ) ; count = 1 } System . out . print ( str . charAt ( i ) ) ; System . out . print ( count ) ; } } public static void main ( String [ ] args ) { String str = " wwwwaaadexxxxxxywww " ; printRLE ( str ) ; } }
Print list items containing all characters of a given word | Java program to print all strings that contain all characters of a word ; prints list items having all characters of word ; * Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length ( ) of given word ; * Check each item of list if has all characters of word ; * unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver code
class GFG { static final int NO_OF_CHARS = 256 ; static void print ( String [ ] list , String word , int list_size ) { int [ ] map = new int [ NO_OF_CHARS ] ; int i , j , count , word_size ; for ( i = 0 ; i < word . length ( ) ; i ++ ) map [ word . charAt ( i ) ] = 1 ; word_size = word . length ( ) ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; j < list [ i ] . length ( ) ; j ++ ) { if ( map [ list [ i ] . charAt ( j ) ] > 0 ) { count ++ ; map [ list [ i ] . charAt ( j ) ] = 0 ; } } if ( count == word_size ) System . out . println ( list [ i ] ) ; for ( j = 0 ; j < word . length ( ) ; j ++ ) map [ word . charAt ( j ) ] = 1 ; } } public static void main ( String [ ] args ) { String str = " sun " ; String [ ] list = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; } }
Given a string , find its first non | Java program to find first non - repeating character ; calculate count of characters in the passed string ; The method returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver method
class GFG { static final int NO_OF_CHARS = 256 ; static char count [ ] = new char [ NO_OF_CHARS ] ; static void getCharCountArray ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str . charAt ( i ) ] ++ ; } static int firstNonRepeating ( String str ) { getCharCountArray ( str ) ; int index = - 1 , i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( count [ str . charAt ( i ) ] == 1 ) { index = i ; break ; } } return index ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int index = firstNonRepeating ( str ) ; System . out . println ( index == - 1 ? " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ " + " is ▁ empty " : " First ▁ non - repeating ▁ character ▁ is ▁ " + str . charAt ( index ) ) ; } }
Divide a string in N equal parts | Java program to divide a string in n equal parts ; Method to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver Code ; length od string is 28 ; Print 4 equal parts of the string
class GFG { static void divideString ( String str , int n ) { int str_size = str . length ( ) ; int part_size ; if ( str_size % n != 0 ) { System . out . println ( " Invalid ▁ Input : ▁ String ▁ size " + " is ▁ not ▁ divisible ▁ by ▁ n " ) ; return ; } part_size = str_size / n ; for ( int i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) System . out . println ( ) ; System . out . print ( str . charAt ( i ) ) ; } } public static void main ( String [ ] args ) { String str = " a _ simple _ divide _ string _ quest " ; divideString ( str , 4 ) ; } }