codeparrot/xlcost-text-to-code huggingface dataset:
Print all possible strings that can be made by placing spaces | Java program to print all strings that can be made by placing spaces ; Function to print all subsequences ; Driver code
import java . util . * ; class GFG { static void printSubsequences ( String s ) { char [ ] str = s . toCharArray ( ) ; int n = str . length ; int opsize = ( int ) ( Math . pow ( 2 , n - 1 ) ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( str [ j ] ) ; if ( ( counter & ( 1 << j ) ) > 0 ) System . out . print ( " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { String str = " AB " ; printSubsequences ( str ) ; } }
Find n | Java program to print n - th permutation ; Utility for calculating factorial ; Function for nth permutation ; length of given string ; Count frequencies of all characters ; out string for output string ; Iterate till sum equals n ; We update both n and sum in this loop . ; Check for characters present in freq [ ] ; Remove character ; calculate sum after fixing a particular char ; if sum > n fix that char as present char and update sum and required nth after fixing char at that position ; if sum < n , add character back ; if sum == n means this char will provide its greatest permutation as nth permutation ; append string termination character and print result ; Driver program to test above method
public class PermuteString { final static int MAX_CHAR = 26 ; final static int MAX_FACT = 20 ; static long fact [ ] = new long [ MAX_FACT ] ; static void precomputeFactorirals ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static void nPermute ( String str , int n ) { precomputeFactorirals ( ) ; int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < len ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; String out = " " ; int sum = 10 ; int k = 0 ; while ( sum >= n ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; int xsum = ( int ) fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= fact [ freq [ j ] ] ; sum += xsum ; if ( sum >= n ) { out += ( char ) ( i + ' a ' ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { out += ( char ) ( i + ' a ' ) ; freq [ i ++ ] -- ; } System . out . println ( out ) ; } public static void main ( String [ ] args ) { int n = 2 ; String str = " geeksquiz " ; nPermute ( str , n ) ; } }
Minimum number of deletions so that no two consecutive are same | Java code to count minimum deletions required so that there are no consecutive characters left ; Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver code ; Function call to print answer
import java . util . * ; class GFG { static int countDeletions ( String s ) { int ans = 0 ; char [ ] str = s . toCharArray ( ) ; for ( int i = 0 ; i < str . length - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { String str = " AAABBB " ; System . out . println ( countDeletions ( str ) ) ; } }
Count words that appear exactly two times in an array of words | Java program to count all words with count exactly 2. ; Returns count of words with frequency exactly 2. ; Driver code
import java . util . HashMap ; import java . util . Map ; public class GFG { static int countWords ( String str [ ] , int n ) { HashMap < String , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( str [ i ] ) ) { int get = m . get ( str [ i ] ) ; m . put ( str [ i ] , get + 1 ) ; } else { m . put ( str [ i ] , 1 ) ; } } int res = 0 ; for ( Map . Entry < String , Integer > it : m . entrySet ( ) ) { if ( it . getValue ( ) == 2 ) res ++ ; } return res ; } public static void main ( String args [ ] ) { String s [ ] = { " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " } ; int n = s . length ; System . out . println ( countWords ( s , n ) ) ; } }
Check whether a given graph is Bipartite or not | Java program to find out whether a given graph is Bipartite or not . Using recursion . ; color this pos as c and all its neighbours as 1 - c ; start is vertex 0 ; ; two colors 1 and 0 ; Driver Code
class GFG { static final int V = 4 ; static boolean colorGraph ( int G [ ] [ ] , int color [ ] , int pos , int c ) { if ( color [ pos ] != - 1 && color [ pos ] != c ) return false ; color [ pos ] = c ; boolean ans = true ; for ( int i = 0 ; i < V ; i ++ ) { if ( G [ pos ] [ i ] == 1 ) { if ( color [ i ] == - 1 ) ans &= colorGraph ( G , color , i , 1 - c ) ; if ( color [ i ] != - 1 && color [ i ] != 1 - c ) return false ; } if ( ! ans ) return false ; } return true ; } static boolean isBipartite ( int G [ ] [ ] ) { int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = - 1 ; int pos = 0 ; return colorGraph ( G , color , pos , 1 ) ; } public static void main ( String [ ] args ) { int G [ ] [ ] = { { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; if ( isBipartite ( G ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Nth Even length Palindrome | Java program to find nth even length Palindrome ; Function to find nth even length Palindrome ; string r to store resultant palindrome . Initialize same as s ; In this loop string r stores reverse of string s after the string s in consecutive manner ; Driver code ; Function call
import java . io . * ; class GFG { static String evenlength ( String n ) { String res = n ; for ( int j = n . length ( ) - 1 ; j >= 0 ; -- j ) res += n . charAt ( j ) ; return res ; } public static void main ( String [ ] args ) { String n = "10" ; System . out . println ( evenlength ( n ) ) ; } }
Program to count occurrence of a given character in a string | package whatever do not write package name here ; base case ; ; checking if the first character of the given string is that character or not ; this will count the occurrence of given character in the string from index 1 to the last index of the string ; Driver code
import java . io . * ; class GFG { static int countinString ( char ch , String s ) { if ( s . length ( ) == 0 ) return 0 ; int count = 0 ; if ( s . charAt ( 0 ) == ch ) count ++ ; count += countinString ( ch , s . substring ( 1 ) ) ; return count ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; char c = ' e ' ; System . out . println ( countinString ( c , str ) ) ; } }
Determine if a string has all Unique Characters | Java program to illustrate string with unique characters using brute force technique ; If at any time we encounter 2 same characters , return false ; If no duplicate characters encountered , return true ; Driver Code
import java . util . * ; class GfG { boolean uniqueCharacters ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) for ( int j = i + 1 ; j < str . length ( ) ; j ++ ) if ( str . charAt ( i ) == str . charAt ( j ) ) return false ; return true ; } public static void main ( String args [ ] ) { GfG obj = new GfG ( ) ; String input = " GeeksforGeeks " ; if ( obj . uniqueCharacters ( input ) ) System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ all ▁ unique ▁ characters " ) ; else System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ duplicate ▁ characters " ) ; } }
Check if given string can be split into four distinct strings | Java program to check if we can break a string into four distinct strings . ; Return true if both strings are equal ; Return if the given string can be split or not . ; Brute Force ; Making 4 string from the given string ; Checking if they are distinct or not . ; Driver code
class GFG { public static boolean strcheck ( String s1 , String s2 ) { if ( s1 . compareTo ( s2 ) != 0 ) return false ; return true ; } public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { String s1 = " " , s2 = " " , s3 = " " , s4 = " " ; try { s1 = s . substring ( 0 , i ) ; s2 = s . substring ( i , j - i ) ; s3 = s . substring ( j , k - j ) ; s4 = s . substring ( k , s . length ( ) - k ) ; } catch ( StringIndexOutOfBoundsException e ) { } if ( strcheck ( s1 , s2 ) && strcheck ( s1 , s3 ) && strcheck ( s1 , s4 ) && strcheck ( s2 , s3 ) && strcheck ( s2 , s4 ) && strcheck ( s3 , s4 ) ) return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " aaabb " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Multiply Large Numbers represented as Strings | Java program to multiply two numbers represented as Strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multipliccharAtion of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result charAt current position . ; Carry for next itercharAtion ; Store result ; store carry in next cell ; To shift position to left after every multipliccharAtion of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; genercharAte the result String ; Driver code
class GFG { static String multiply ( String num1 , String num2 ) { int len1 = num1 . length ( ) ; int len2 = num2 . length ( ) ; if ( len1 == 0 len2 == 0 ) return "0" ; int result [ ] = new int [ len1 + len2 ] ; int i_n1 = 0 ; int i_n2 = 0 ; for ( int i = len1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n1 = num1 . charAt ( i ) - '0' ; i_n2 = 0 ; for ( int j = len2 - 1 ; j >= 0 ; j -- ) { int n2 = num2 . charAt ( j ) - '0' ; int sum = n1 * n2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } int i = result . length - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == - 1 ) return "0" ; String s = " " ; while ( i >= 0 ) s += ( result [ i -- ] ) ; return s ; } public static void main ( String [ ] args ) { String str1 = "1235421415454545454545454544" ; String str2 = "1714546546546545454544548544544545" ; if ( ( str1 . charAt ( 0 ) == ' - ' || str2 . charAt ( 0 ) == ' - ' ) && ( str1 . charAt ( 0 ) != ' - ' || str2 . charAt ( 0 ) != ' - ' ) ) System . out . print ( " - " ) ; if ( str1 . charAt ( 0 ) == ' - ' ) str1 = str1 . substring ( 1 ) ; if ( str2 . charAt ( 0 ) == ' - ' ) str2 = str2 . substring ( 1 ) ; System . out . println ( multiply ( str1 , str2 ) ) ; } }
Find an equal point in a string of brackets | Java program to find an index k which decides the number of opening brackets is equal to the number of closing brackets ; Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; check if there is no opening or closing brackets ; check if there is any index at which both brackets are equal ; Driver Method
public class GFG { static int findIndex ( String str ) { int len = str . length ( ) ; int open [ ] = new int [ len + 1 ] ; int close [ ] = new int [ len + 1 ] ; int index = - 1 ; open [ 0 ] = 0 ; close [ len ] = 0 ; if ( str . charAt ( 0 ) == ' ( ' ) open [ 1 ] = 1 ; if ( str . charAt ( len - 1 ) == ' ) ' ) close [ len - 1 ] = 1 ; for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) open [ i + 1 ] = open [ i ] + 1 ; else open [ i + 1 ] = open [ i ] ; } for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == ' ) ' ) close [ i ] = close [ i + 1 ] + 1 ; else close [ i ] = close [ i + 1 ] ; } if ( open [ len ] == 0 ) return len ; if ( close [ 0 ] == 0 ) return 0 ; for ( int i = 0 ; i <= len ; i ++ ) if ( open [ i ] == close [ i ] ) index = i ; return index ; } public static void main ( String [ ] args ) { String str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) " ; System . out . println ( findIndex ( str ) ) ; } }
Convert decimal fraction to binary number | Java program to convert fractional decimal to binary number ; Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Swap values of left and right ; Driver code
import java . util . * ; class GFG { static String decimalToBinary ( double num , int k_prec ) { String binary = " " ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + '0' ) ) ; Integral /= 2 ; } binary = reverse ( binary ) ; binary += ( ' . ' ) ; while ( k_prec -- > 0 ) { fractional *= 2 ; int fract_bit = ( int ) fractional ; if ( fract_bit == 1 ) { fractional -= fract_bit ; binary += ( char ) ( 1 + '0' ) ; } else { binary += ( char ) ( 0 + '0' ) ; } } return binary ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { double n = 4.47 ; int k = 3 ; System . out . println ( decimalToBinary ( n , k ) ) ; n = 6.986 ; k = 5 ; System . out . println ( decimalToBinary ( n , k ) ) ; } }
Difference of two large numbers | Java program to find difference of two large numbers . ; Returns true if str1 is smaller than str2 , else false . ; Calculate lengths of both string ; Function for finding difference of larger numbers ; Before proceeding further , make sure str1 is not smaller ; Take an empty string for storing result ; Calculate lengths of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute difference of current digits and carry ; subtract remaining digits of str1 [ ] ; if ( i > 0 sub > 0 ) remove preceding 0 's ; reverse resultant string ; Driver code ; Function call
class GFG { static boolean isSmaller ( String str1 , String str2 ) { int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; if ( n1 < n2 ) return true ; if ( n2 < n1 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) return true ; else if ( str1 . charAt ( i ) > str2 . charAt ( i ) ) return false ; } return false ; } static String findDiff ( String str1 , String str2 ) { if ( isSmaller ( str1 , str2 ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n1 - n2 ; int carry = 0 ; for ( int i = n2 - 1 ; i >= 0 ; i -- ) { int sub = ( ( ( int ) str1 . charAt ( i + diff ) - ( int ) '0' ) - ( ( int ) str2 . charAt ( i ) - ( int ) '0' ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str += String . valueOf ( sub ) ; } for ( int i = n1 - n2 - 1 ; i >= 0 ; i -- ) { if ( str1 . charAt ( i ) == '0' && carry > 0 ) { str += "9" ; continue ; } int sub = ( ( ( int ) str1 . charAt ( i ) - ( int ) '0' ) - carry ) ; str += String . valueOf ( sub ) ; carry = 0 ; } return new StringBuilder ( str ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { String str1 = "88" ; String str2 = "1079" ; System . out . println ( findDiff ( str1 , str2 ) ) ; } }
Efficiently check if a string has all unique characters without using any additional data structure | Driver code
import java . util . Arrays ; class GFG { static boolean unique ( String s ) { Arrays . sort ( s . toCharArray ( ) ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { if ( unique ( " abcdd " ) == true ) { System . out . println ( " String ▁ is ▁ Unique " ) ; } else { System . out . println ( " String ▁ is ▁ not ▁ Unique " ) ; } } }
Check if two strings are k | Optimized Java program to check if two strings are k anagram or not . ; Function to check if str1 and str2 are k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Store the occurrence of all characters in a hash_array ; Return true if count is less than or equal to k ; Driver code
public class GFG { static final int MAX_CHAR = 26 ; static boolean areKAnagrams ( String str1 , String str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int [ ] hash_str1 = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) hash_str1 [ str1 . charAt ( i ) - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash_str1 [ str2 . charAt ( i ) - ' a ' ] > 0 ) hash_str1 [ str2 . charAt ( i ) - ' a ' ] -- ; else count ++ ; if ( count > k ) return false ; } return true ; } public static void main ( String args [ ] ) { String str1 = " fodr " ; String str2 = " gork " ; int k = 2 ; if ( areKAnagrams ( str1 , str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Nth character in Concatenated Decimal String | Java program to get Nth character in concatenated Decimal String ; Utility method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; sum will store character escaped till now ; dist will store numbers escaped till now ; loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; restore variables to previous correct state ; get distance from last one digit less maximum number ; d will store dth digit of current number ; method will return dth numbered digit of ( dist + diff ) number ; Driver code
class GFG { static char getDigit ( int N , int d ) { String str = Integer . toString ( N ) ; return str . charAt ( d - 1 ) ; } static char getNthChar ( int N ) { int sum = 0 , nine = 9 ; int dist = 0 , len ; for ( len = 1 ; ; len ++ ) { sum += nine * len ; dist += nine ; if ( sum >= N ) { sum -= nine * len ; dist -= nine ; N -= sum ; break ; } nine *= 10 ; } int diff = ( int ) ( Math . ceil ( ( double ) ( N ) / ( double ) ( len ) ) ) ; int d = N % len ; if ( d == 0 ) d = len ; return getDigit ( dist + diff , d ) ; } public static void main ( String [ ] args ) { int N = 251 ; System . out . println ( getNthChar ( N ) ) ; } }
Minimum characters to be added at front to make string palindrome | Java program for getting minimum character to be added at front to make string palindrome ; function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front
class GFG { static boolean ispalindrome ( String s ) { int l = s . length ( ) ; for ( int i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s . charAt ( i ) != s . charAt ( j ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " BABABAA " ; int cnt = 0 ; int flag = 0 ; while ( s . length ( ) > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s = s . substring ( 0 , s . length ( ) - 1 ) ; s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; } } if ( flag == 1 ) { System . out . println ( cnt ) ; } } }
Count characters at same position as in English alphabet | Java program to find number of characters at same position as in English alphabets ; Traverse input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver code
class GFG { static int findCount ( String str ) { int result = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i == ( str . charAt ( i ) - ' a ' ) || i == ( str . charAt ( i ) - ' A ' ) ) { result ++ ; } } return result ; } public static void main ( String [ ] args ) { String str = " AbgdeF " ; System . out . print ( findCount ( str ) ) ; } }
Remove a character from a string to make it a palindrome | Java program to check whether it is possible to make string palindrome by removing one character ; Utility method to check if substring from low to high is palindrome or not . ; This method returns - 1 if it is not possible to make string a palindrome . It returns - 2 if string is already a palindrome . Otherwise it returns index of character whose removal can make the whole string palindrome . ; Initialize low and right by both the ends of the string ; loop until low and high cross each other ; If both characters are equal then move both pointer towards end ; * If removing str [ low ] makes the * whole string palindrome . We basically * check if substring str [ low + 1. . high ] * is palindrome or not . ; * If removing str [ high ] makes the whole string * palindrome . We basically check if substring * str [ low + 1. . high ] is palindrome or not . ; We reach here when complete string will be palindrome if complete string is palindrome then return mid character ; Driver Code
import java . util . * ; class GFG { static boolean isPalindrome ( String str , int low , int high ) { while ( low < high ) { if ( str . charAt ( low ) != str . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } static int possiblePalinByRemovingOneChar ( String str ) { int low = 0 , high = str . length ( ) - 1 ; while ( low < high ) { if ( str . charAt ( low ) == str . charAt ( high ) ) { low ++ ; high -- ; } else { if ( isPalindrome ( str , low + 1 , high ) ) return low ; if ( isPalindrome ( str , low , high - 1 ) ) return high ; return - 1 ; } } return - 2 ; } public static void main ( String [ ] args ) { String str = " abecbea " ; int idx = possiblePalinByRemovingOneChar ( str ) ; if ( idx == - 1 ) System . out . println ( " Not ▁ Possible " ) ; else if ( idx == - 2 ) System . out . println ( " Possible ▁ without ▁ " + " removing ▁ any ▁ character " ) ; else System . out . println ( " Possible ▁ by ▁ removing " + " ▁ character ▁ at ▁ index ▁ " + idx ) ; } }