codeparrot/xlcost-text-to-code huggingface dataset:
Largest and Smallest N | Java program to find the largest and smallest N - digit numbers in Octal Number System ; Function to return the largest N - digit number in Octal Number System ; Append '7' N times ; Function to return the smallest N - digit number in Octal Number System ; Append '0' ( N - 1 ) times to 1 ; Function to print the largest and smallest N - digit Octal number ; Driver code ; Function Call
class GFG { static String findLargest ( int N ) { String largest = strings ( N , '7' ) ; return largest ; } static String findSmallest ( int N ) { String smallest = "1" + strings ( ( N - 1 ) , '0' ) ; return smallest ; } private static String strings ( int N , char c ) { String temp = " " ; for ( int i = 0 ; i < N ; i ++ ) { temp += c ; } return temp ; } static void printLargestSmallest ( int N ) { System . out . print ( " Largest : ▁ " + findLargest ( N ) + "NEW_LINE"); System . out . print ( " Smallest : ▁ " + findSmallest ( N ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; printLargestSmallest ( N ) ; } }
Count of substrings whose Decimal equivalent is greater than or equal to K | Java implementation to count the subStrings whose decimal equivalent is greater than or equal to K ; Function to count number of subString whose decimal equivalent is greater than or equal to K ; Left pointer of the subString ; Right pointer of the subString ; Loop to maintain the last occurrence of the 1 in the String ; Variable to count the subString ; Loop to maintain the every possible end index of the subString ; Loop to find the subString whose decimal equivalent is greater than or equal to K ; Condition to check no of bits is out of bound ; Driver Code
import java . util . * ; class GFG { static long countSubstr ( String s , int k ) { int n = s . length ( ) ; int l = n - 1 ; int r = n - 1 ; int [ ] arr = new int [ n ] ; int last_indexof1 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { arr [ i ] = i ; last_indexof1 = i ; } else { arr [ i ] = last_indexof1 ; } } long no_of_substr = 0 ; for ( r = n - 1 ; r >= 0 ; r -- ) { l = r ; while ( l >= 0 && ( r - l + 1 ) <= 64 && Integer . valueOf ( s . substring ( l , r + 1 ) , 2 ) < k ) { l -- ; } if ( r - l + 1 <= 64 ) no_of_substr += l + 1 ; else { no_of_substr += arr [ l + 1 ] + 1 ; } } return no_of_substr ; } public static void main ( String [ ] args ) { String s = "11100" ; int k = 3 ; System . out . println ( countSubstr ( s , k ) ) ; } }
Perfect Cube String | Java program to find if String is a perfect cube or not . ; Finding ASCII values of each character and finding its sum ; Find the cube root of sum ; Check if sum is a perfect cube ; Driver code
import java . util . * ; class GFG { static boolean isPerfectCubeString ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) sum += ( int ) str . charAt ( i ) ; double cr = Math . round ( Math . cbrt ( sum ) ) ; return ( cr * cr * cr == sum ) ; } public static void main ( String [ ] args ) { String str = " ll " ; if ( isPerfectCubeString ( str ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Program to find the XOR of ASCII values of characters in a string | Java program to find XOR of ASCII value of characters in String ; Function to find the XOR of ASCII value of characters in String ; store value of first character ; Traverse String to find the XOR ; Return the XOR ; Driver code
class GFG { static int XorAscii ( String str , int len ) { int ans = ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < len ; i ++ ) { ans = ( ans ^ ( ( str . charAt ( i ) ) ) ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . print ( XorAscii ( str , len ) + "NEW_LINE"); str = " GfG " ; len = str . length ( ) ; System . out . print ( XorAscii ( str , len ) ) ; } }
CamelCase Pattern Matching | Java to find CamelCase Pattern matching ; Function that prints the camel case pattern matching ; Map to store the hashing of each words with every uppercase letter found ; Traverse the words array that contains all the String ; Initialise str as empty ; length of String words [ i ] ; For every uppercase letter found map that uppercase to original words ; Traverse the map for pattern matching ; If pattern matches then print the corresponding mapped words ; If word not found print " No ▁ match ▁ found " ; Driver 's Code ; Pattern to be found ; Function call to find the words that match to the given pattern
import java . util . * ; class GFG { static void CamelCase ( ArrayList < String > words , String pattern ) { Map < String , List < String > > map = new HashMap < String , List < String > > ( ) ; for ( int i = 0 ; i < words . size ( ) ; i ++ ) { String str = " " ; int l = words . get ( i ) . length ( ) ; for ( int j = 0 ; j < l ; j ++ ) { if ( words . get ( i ) . charAt ( j ) >= ' A ' && words . get ( i ) . charAt ( j ) <= ' Z ' ) { str += words . get ( i ) . charAt ( j ) ; map . put ( str , list ( map . get ( str ) , words . get ( i ) ) ) ; } } } boolean wordFound = false ; for ( Map . Entry < String , List < String > > it : map . entrySet ( ) ) { if ( it . getKey ( ) . equals ( pattern ) ) { wordFound = true ; for ( String s : it . getValue ( ) ) System . out . print ( s + "NEW_LINE"); } } if ( ! wordFound ) { System . out . print ( " No ▁ match ▁ found " ) ; } } private static List < String > list ( List < String > list , String str ) { List < String > temp = new ArrayList < String > ( ) ; if ( list != null ) temp . addAll ( list ) ; temp . add ( str ) ; return temp ; } public static void main ( String [ ] args ) { String arr [ ] = { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; ArrayList < String > words = new ArrayList < String > ( Arrays . asList ( arr ) ) ; String pattern = " HT " ; CamelCase ( words , pattern ) ; } }
Encryption and Decryption of String according to given technique | Java implementation for Custom Encryption and Decryption of String ; Function to encrypt the String ; Matrix to generate the Encrypted String ; Fill the matrix row - wise ; Loop to generate encrypted String ; Function to decrypt the String ; Matrix to generate the Encrypted String ; Fill the matrix column - wise ; Loop to generate decrypted String ; Driver Code ; Encryption of String ; Decryption of String
class GFG { static String encryption ( char [ ] s ) { int l = s . length ; int b = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int a = ( int ) Math . floor ( Math . sqrt ( l ) ) ; String encrypted = " " ; if ( b * a < l ) { if ( Math . min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } char [ ] [ ] arr = new char [ a ] [ b ] ; int k = 0 ; for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i ] [ j ] ; } } return encrypted ; } static String decryption ( char [ ] s ) { int l = s . length ; int b = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int a = ( int ) Math . floor ( Math . sqrt ( l ) ) ; String decrypted = " " ; char [ ] [ ] arr = new char [ a ] [ b ] ; int k = 0 ; for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i ] [ j ] ; } } return decrypted ; } public static void main ( String [ ] args ) { String s = " Geeks ▁ For ▁ Geeks " ; String encrypted ; String decrypted ; encrypted = encryption ( s . toCharArray ( ) ) ; System . out . print ( encrypted + "NEW_LINE"); decrypted = decryption ( encrypted . toCharArray ( ) ) ; System . out . print ( decrypted ) ; } }
Program to accept String starting with Capital letter | Java program to accept String starting with Capital letter ; Function to check if first character is Capital ; Function to check ; Driver function
class GFG { static int checkIfStartsWithCapital ( String str ) { if ( str . charAt ( 0 ) >= ' A ' && str . charAt ( 0 ) <= ' Z ' ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithCapital ( str ) == 1 ) System . out . println ( " Accepted " ) ; else System . out . println ( " Not ▁ Accepted " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; check ( str ) ; str = " geeksforgeeks " ; check ( str ) ; } }
Program to accept a Strings which contains all the Vowels | Java implementation to check that a String contains all vowels ; Function to to check that a String contains all vowels ; Hash Array of size 5 such that the index 0 , 1 , 2 , 3 and 4 represent the vowels a , e , i , o and u ; Loop the String to mark the vowels which are present ; Loop to check if there is any vowel which is not present in the String ; Function to to check that a String contains all vowels ; Driver Code
class GFG { static int checkIfAllVowels ( String str ) { int [ ] hash = new int [ 5 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' A ' || str . charAt ( i ) == ' a ' ) hash [ 0 ] = 1 ; else if ( str . charAt ( i ) == ' E ' || str . charAt ( i ) == ' e ' ) hash [ 1 ] = 1 ; else if ( str . charAt ( i ) == ' I ' || str . charAt ( i ) == ' i ' ) hash [ 2 ] = 1 ; else if ( str . charAt ( i ) == ' O ' || str . charAt ( i ) == ' o ' ) hash [ 3 ] = 1 ; else if ( str . charAt ( i ) == ' U ' || str . charAt ( i ) == ' u ' ) hash [ 4 ] = 1 ; } for ( int i = 0 ; i < 5 ; i ++ ) { if ( hash [ i ] == 0 ) { return 1 ; } } return 0 ; } static void checkIfAllVowelsArePresent ( String str ) { if ( checkIfAllVowels ( str ) == 1 ) System . out . print ( "Not AcceptedNEW_LINE"); else System . out . print ( "AcceptedNEW_LINE"); } public static void main ( String [ ] args ) { String str = " aeioubc " ; checkIfAllVowelsArePresent ( str ) ; } }
Smallest odd number with even sum of digits from the given number N | Java program to find the smallest odd number with even sum of digits from the given number N ; Function to find the smallest odd number whose sum of digits is even from the given string ; Converting the given string to a list of digits ; An empty array to store the digits ; For loop to iterate through each digit ; If the given digit is odd then the digit is appended to the array b ; Sorting the list of digits ; If the size of the list is greater than 1 then a 2 digit smallest odd number is returned . Since the sum of two odd digits is always even ; Else , - 1 is returned ; Driver code
import java . util . * ; class GFG { public static int smallest ( String s ) { int [ ] a = new int [ s . length ( ) ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ i ] = s . charAt ( i ) - '0' ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 != 0 ) b . add ( a [ i ] ) ; } Collections . sort ( b ) ; if ( b . size ( ) > 1 ) return ( b . get ( 0 ) ) * 10 + ( b . get ( 1 ) ) ; return - 1 ; } public static void main ( String [ ] args ) { System . out . print ( smallest ( "15470" ) ) ; } }
Number formed by deleting digits such that sum of the digits becomes even and the number odd | Java implementation to convert a number into odd number such that digit - sum is odd ; Function to convert a number into odd number such that digit - sum is odd ; Loop to find any first two odd number such that their sum is even and number is odd ; Print the result ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class Main { static void converthenumber ( int n ) { String s = Integer . toString ( n ) ; String res = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' || s . charAt ( i ) == '9' ) res += s . charAt ( i ) ; if ( res . length ( ) == 2 ) break ; } if ( res . length ( ) == 2 ) System . out . println ( res ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 18720 ; converthenumber ( n ) ; } }
Longest palindromic String formed using concatenation of given strings in any order | Java implementation to find the longest palindromic String formed using concatenation of given Strings in any order ; Function to find the longest palindromic from given array of Strings ; Loop to find the pair of Strings which are reverse of each other ; Loop to find if any palindromic String is still left in the array ; Update the answer with all Strings of pair1 ; Update the answer with palindromic String s1 ; Update the answer with all Strings of pair2 ; Driver Code
class GFG { static void longestPalindrome ( String a [ ] , int n ) { String [ ] pair1 = new String [ n ] ; String [ ] pair2 = new String [ n ] ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; s = reverse ( s ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] != " " && a [ j ] != " " ) { if ( s . equals ( a [ j ] ) ) { pair1 [ r ] = a [ i ] ; pair2 [ r ++ ] = a [ j ] ; a [ i ] = " " ; a [ j ] = " " ; break ; } } } } String s1 = " " ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; a [ i ] = reverse ( a [ i ] ) ; if ( a [ i ] != " " ) { if ( a [ i ] . equals ( s ) ) { s1 = a [ i ] ; break ; } } } String ans = " " ; for ( int i = 0 ; i < r ; i ++ ) { ans = ans + pair1 [ i ] ; } if ( s1 != " " ) { ans = ans + s1 ; } for ( int j = r - 1 ; j >= 0 ; j -- ) { ans = ans + pair2 [ j ] ; } System . out . print ( ans + "NEW_LINE"); } 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 [ ] a1 = { " aba " , " aba " } ; int n1 = a1 . length ; longestPalindrome ( a1 , n1 ) ; String [ ] a2 = { " abc " , " dba " , " kop " , " abd " , " cba " } ; int n2 = a2 . length ; longestPalindrome ( a2 , n2 ) ; } }
Program to print the given digit in words | Java implementation of the above approach ; Function to return the word of the corresponding digit ; Switch block to check for each digit c ; For digit 0 ; For digit 1 ; For digit 2 ; For digit 3 ; For digit 4 ; For digit 5 ; For digit 6 ; For digit 7 ; For digit 8 ; For digit 9 ; Function to iterate through every digit in the given number ; Finding each digit of the number ; Print the digit in words ; Driver code
class GFG { static void printValue ( char digit ) { switch ( digit ) { case '0' : System . out . print ( " Zero ▁ " ) ; break ; case '1' : System . out . print ( " One ▁ " ) ; break ; case '2' : System . out . print ( " Two ▁ " ) ; break ; case '3' : System . out . print ( " Three ▁ " ) ; break ; case '4' : System . out . print ( " Four ▁ " ) ; break ; case '5' : System . out . print ( " Five ▁ " ) ; break ; case '6' : System . out . print ( " Six ▁ " ) ; break ; case '7' : System . out . print ( " Seven ▁ " ) ; break ; case '8' : System . out . print ( " Eight ▁ " ) ; break ; case '9' : System . out . print ( " Nine ▁ " ) ; break ; } } static void printWord ( String N ) { int i , length = N . length ( ) ; for ( i = 0 ; i < length ; i ++ ) { printValue ( N . charAt ( i ) ) ; } } public static void main ( String [ ] args ) { String N = "123" ; printWord ( N ) ; } }
Sum of all LCP of maximum length by selecting any two Strings at a time | Java program to find Sum of all LCP of maximum length by selecting any two Strings at a time ; Using map to store the pointers of children nodes for dynamic implementation , for making the program space efiicient ; Counts the number of times the node is visited while making the trie ; Initially visited value for all nodes is zero ; Head node of the trie is initialize as ' \0' , after this all Strings add ; Function to insert the Strings in the trie ; Inserting character - by - character ; If the node of ch is not present in map make a new node and add in map ; Recursive function to calculate the answer argument is passed by reference ; To store changed visited values from children of this node i . e . number of nodes visited by its children ; Updating the visited variable , telling number of nodes that have already been visited by its children ; If node . visited > 1 , means more than one String has prefix up till this node common in them ; Number of String pair with current node common in them ; Updating visited variable of current node ; Returning the total number of nodes already visited that needs to be updated to previous node ; Function to run the dfs function for the first time and give the answer variable ; Stores the final answer as sum of all depths ; Driver code
import java . util . * ; class GFG { static class TrieNode { char val ; HashMap < Character , TrieNode > children ; int visited ; TrieNode ( char x ) { val = x ; visited = 0 ; children = new HashMap < > ( ) ; } } static class Trie { TrieNode head ; int ans ; Trie ( ) { head = new TrieNode ( ' \0' ) ; ans = 0 ; } void addWord ( String s ) { TrieNode temp = head ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( temp . children . get ( ch ) == null ) { temp . children . put ( ch , new TrieNode ( ch ) ) ; } temp = temp . children . get ( ch ) ; temp . visited ++ ; } } int dfs ( TrieNode node , int depth ) { int vis = 0 ; Iterator hmIterator = node . children . entrySet ( ) . iterator ( ) ; while ( hmIterator . hasNext ( ) ) { Map . Entry child = ( Map . Entry ) hmIterator . next ( ) ; vis += dfs ( ( TrieNode ) child . getValue ( ) , depth + 1 ) ; } node . visited -= vis ; int String_pair = 0 ; if ( node . visited > 1 ) { String_pair = ( node . visited / 2 ) ; ans += ( depth * String_pair ) ; node . visited -= ( 2 * String_pair ) ; } return ( 2 * String_pair + vis ) ; } int dfshelper ( ) { ans = 0 ; dfs ( head , 0 ) ; return ans ; } } public static void main ( String args [ ] ) { Trie T = new Trie ( ) ; String str [ ] = { " babab " , " ababb " , " abbab " , " aaaaa " , " babaa " , " babbb " } ; int n = 6 ; for ( int i = 0 ; i < n ; i ++ ) { T . addWord ( str [ i ] ) ; } int ans = T . dfshelper ( ) ; System . out . println ( ans ) ; } }
Print all possible combinations of words from Dictionary using Trie | Java implementation of the approach ; Trie node ; isEndOfWord is true if node represents the end of the word ; Returns new trie node ; If not present , inserts key into trie If the key is prefix of trie node , marks the node as leaf node ; Mark node as leaf ; Returns true if the key is present in the trie ; Result stores the current prefix with spaces between words ; Process all prefixes one by one ; Extract subString from 0 to i in prefix ; If trie conatins this prefix then check for the remaining String . Otherwise ignore this prefix ; If no more elements are there then print ; Add this element to the previous prefix ; If ( result == word ) then return If you don 't want to print last word ; Driver code
class GFG { static int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; boolean isEndOfWord ; public TrieNode ( ) { super ( ) ; } } ; static TrieNode getNode ( ) { TrieNode pNode = new TrieNode ( ) ; pNode . isEndOfWord = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode . children [ i ] = null ; return pNode ; } static void insert ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = getNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isEndOfWord = true ; } static boolean search ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) return false ; pCrawl = pCrawl . children [ index ] ; } return ( pCrawl != null && pCrawl . isEndOfWord ) ; } static void wordBreakAll ( TrieNode root , String word , int n , String result ) { for ( int i = 1 ; i <= n ; i ++ ) { String prefix = word . substring ( 0 , i ) ; if ( search ( root , prefix ) ) { if ( i == n ) { result += prefix ; System . out . print ( " TABSYMBOL " + result + "NEW_LINE"); return ; } wordBreakAll ( root , word . substring ( i , n ) , n - i , result + prefix + " ▁ " ) ; } } } public static void main ( String [ ] args ) { new TrieNode ( ) ; TrieNode root = getNode ( ) ; String dictionary [ ] = { " sam " , " sung " , " samsung " } ; int n = dictionary . length ; for ( int i = 0 ; i < n ; i ++ ) { insert ( root , dictionary [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( dictionary [ i ] + ": NEW_LINE"); wordBreakAll ( root , dictionary [ i ] , dictionary [ i ] . length ( ) , " " ) ; } } }
Jaro and Jaro | Java implementation of above approach ; Function to calculate the Jaro Similarity of two Strings ; If the Strings are equal ; Length of two Strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first String ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second String ; Return the Jaro Similarity ; Driver code ; Print jaro Similarity of two Strings
class GFG { static double jaro_distance ( String s1 , String s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . length ( ) , len2 = s2 . length ( ) ; int max_dist = ( int ) ( Math . floor ( Math . max ( len1 , len2 ) / 2 ) - 1 ) ; int match = 0 ; int hash_s1 [ ] = new int [ s1 . length ( ) ] ; int hash_s2 [ ] = new int [ s2 . length ( ) ] ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = Math . max ( 0 , i - max_dist ) ; j < Math . min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 . charAt ( i ) == s2 . charAt ( j ) && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] == 1 ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 . charAt ( i ) != s2 . charAt ( point ++ ) ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } public static void main ( String [ ] args ) { String s1 = " CRATE " , s2 = " TRACE " ; System . out . print ( jaro_distance ( s1 , s2 ) + "NEW_LINE"); } }