codeparrot/xlcost-text-to-code huggingface dataset:
Check if the given string is vowel prime | Java implementation of the approach ; Function that returns true if c is a vowel ; Function that returns true if all the vowels in the given string are only at prime indices ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; For every character of the given string ; If current character is vowel and the index is not prime ; Driver code
import java . util . * ; class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean isVowelPrime ( String str , int n ) { boolean [ ] prime = new boolean [ n ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < n ; i += p ) prime [ i ] = false ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) && ! prime [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; if ( isVowelPrime ( str , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Swap all occurrences of two characters to get lexicographically smallest string | Java implementation of the approach ; Function to return the lexicographically smallest string after swapping all the occurrences of any two characters ; To store the first index of every character of str ; Store the first occurring index every character ; If current character is appearing for the first time in str ; Starting from the leftmost character ; For every character smaller than str [ i ] ; If there is a character in str which is smaller than str [ i ] and appears after it ; If the required character pair is found ; If swapping is possible ; Characters to be swapped ; For every character ; Replace every ch1 with ch2 and every ch2 with ch1 ; Driver code
import java . util . * ; class GFG { static int MAX = 26 ; static String smallestStr ( char [ ] str , int n ) { int i , j = 0 ; int [ ] chk = new int [ MAX ] ; for ( i = 0 ; i < MAX ; i ++ ) chk [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( chk [ str [ i ] - ' a ' ] == - 1 ) chk [ str [ i ] - ' a ' ] = i ; } for ( i = 0 ; i < n ; i ++ ) { boolean flag = false ; for ( j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { if ( chk [ j ] > chk [ str [ i ] - ' a ' ] ) { flag = true ; break ; } } if ( flag ) break ; } if ( i < n ) { char ch1 = str [ i ] ; char ch2 = ( char ) ( j + ' a ' ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch1 ) str [ i ] = ch2 ; else if ( str [ i ] == ch2 ) str [ i ] = ch1 ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " ccad " ; int n = str . length ( ) ; System . out . println ( smallestStr ( str . toCharArray ( ) , n ) ) ; } }
Longest sub string of 0 's in a binary string which is repeated K times | Java program to find the Longest continuous sequence of '0' after repeating Given string K time ; Function to find the longest substring of 0 's ; To store size of the string ; To store the required answer ; Find the longest substring of 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
class GFG { static int longest_substring ( String s , int k ) { int n = s . length ( ) ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( i < n && s . charAt ( i ) == '0' ) { x ++ ; i ++ ; } ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 ans != n ) return ans ; else return ( ans / 2 ) * k ; } public static void main ( String [ ] args ) { String s = "010001000" ; int k = 4 ; System . out . println ( longest_substring ( s , k ) ) ; } }
Find the number of occurrences of a character upto preceding position | Java program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Return the required count ; Driver code ; Function call
class GFG { static int Occurrence ( String s , int position ) { int count = 0 ; for ( int i = 0 ; i < position - 1 ; i ++ ) if ( s . charAt ( i ) == s . charAt ( position - 1 ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; System . out . println ( Occurrence ( s , p ) ) ; } }
Find the number of occurrences of a character upto preceding position | Java program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
import java . util . * ; class GFG { static int countOccurrence ( String s , int position ) { int [ ] alpha = new int [ 26 ] ; int [ ] b = new int [ s . length ( ) ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { b [ i ] = alpha [ ( int ) s . charAt ( i ) - 97 ] ; alpha [ ( int ) s . charAt ( i ) - 97 ] ++ ; } return b [ position - 1 ] ; } public static void main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; System . out . println ( countOccurrence ( s , p ) ) ; } }
Map every character of one string to another such that all occurrences are mapped to the same character | Java implementation of the approach ; Function that returns true if the mapping is possible ; Both the strings are of un - equal lengths ; To store the frequencies of the characters in both the string ; Update frequencies of the characters ; For every character of s1 ; If current character is not present in s1 ; Find a character in s2 that has frequency equal to the current character 's  frequency in s1 ; If such character is found ; Set the frequency to - 1 so that it doesn 't get picked again ; Set found to true ; If there is no character in s2 that could be mapped to the current character in s1 ; Driver code
class GFG { static int MAX = 26 ; public static boolean canBeMapped ( String s1 , int l1 , String s2 , int l2 ) { if ( l1 != l2 ) return false ; int [ ] freq1 = new int [ MAX ] ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) freq1 [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ s2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; boolean found = false ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " axx " ; String s2 = " cbc " ; int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Distinct strings such that they contains given strings as sub | Java implementation of the approach ; Set to store strings and avoid duplicates ; Recursive function to generate the required strings ; If current string is part of the result ; Insert it into the set ; If character from str1 can be chosen ; If character from str2 can be chosen ; Function to print the generated strings from the set ; Driver code
import java . util . HashSet ; class GFG { static HashSet < String > stringSet = new HashSet < > ( ) ; public static void find_permutation ( String str1 , String str2 , int len1 , int len2 , int i , int j , String res ) { if ( res . length ( ) == len1 + len2 ) { stringSet . add ( res ) ; return ; } if ( i < len1 ) find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 . charAt ( i ) ) ; if ( j < len2 ) find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 . charAt ( j ) ) ; } public static void print_set ( ) { for ( String s : stringSet ) System . out . println ( s ) ; } public static void main ( String [ ] args ) { String str1 = " aa " , str2 = " ab " ; int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; find_permutation ( str1 , str2 , len1 , len2 , 0 , 0 , " " ) ; print_set ( ) ; } }
Generate all possible strings such that char at index i is either str1 [ i ] or str2 [ i ] | Java implementation of the approach ; Recursive function to generate the required strings ; If length of the current string is equal to the length of the given strings then the current string is part of the result ; Choosing the current character from the string a ; Choosing the current character from the string b ; Driver code ; Third argument is an empty string that we will be appended in the recursion calls Fourth arguments is the length of the resultant string so far
class GFG { public static void generateStr ( String a , String b , String s , int count , int len ) { if ( count == len ) { System . out . println ( s ) ; return ; } generateStr ( a . substring ( 1 ) , b . substring ( 1 ) , s + a . charAt ( 0 ) , count + 1 , len ) ; generateStr ( a . substring ( 1 ) , b . substring ( 1 ) , s + b . charAt ( 0 ) , count + 1 , len ) ; } public static void main ( String [ ] args ) { String a = " abc " , b = " def " ; int n = a . length ( ) ; generateStr ( a , b , " " , 0 , n ) ; } }
Count of non | Java implementation of the approach ; Function to return the count of required non - overlapping sub - strings ; To store the required count ; If "010" matches the sub - string starting at current index i ; If "101" matches the sub - string starting at current index i ; Driver code
class GFG { static int countSubStr ( char [ ] s , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 2 { if ( s [ i ] == '0' && s [ i + 1 ] == '1' && s [ i + 2 ] == '0' ) { count ++ ; i += 3 ; } else if ( s [ i ] == '1' && s [ i + 1 ] == '0' && s [ i + 2 ] == '1' ) { count ++ ; i += 3 ; } else { i ++ ; } } return count ; } public static void main ( String [ ] args ) { char [ ] s = "10101010101" . toCharArray ( ) ; int n = s . length ; System . out . println ( countSubStr ( s , n ) ) ; } }
Find distinct characters in distinct substrings of a string | Java implementation of the approach ; Function to return the count of distinct characters in all the distinct sub - strings of the given string ; To store all the sub - strings ; To store the current sub - string ; To store the characters of the current sub - string ; If current sub - string hasn 't  been stored before ; Insert it into the set ; Update the count of distinct characters ; Driver code
import java . util . HashSet ; class geeks { public static int countTotalDistinct ( String str ) { int cnt = 0 ; HashSet < String > items = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { String temp = " " ; HashSet < Character > ans = new HashSet < > ( ) ; for ( int j = i ; j < str . length ( ) ; ++ j ) { temp = temp + str . charAt ( j ) ; ans . add ( str . charAt ( j ) ) ; if ( ! items . contains ( temp ) ) { items . add ( temp ) ; cnt += ans . size ( ) ; } } } return cnt ; } public static void main ( String [ ] args ) { String str = " ABCA " ; System . out . println ( countTotalDistinct ( str ) ) ; } }
Partition the string in two parts such that both parts have at least k different characters | Java implementation of the approach ; Function to find the partition of the string such that both parts have at least k different characters ; Length of the string ; To check if the current character is already found ; Count number of different characters in the left part ; If current character is not already found , increase cnt by 1 ; If count becomes equal to k , we 've  got the first part, therefore,  store current index and break the loop ; Increment i by 1 ; Clear the map ; Assign cnt as 0 ; If the current character is not already found , increase cnt by 1 ; If cnt becomes equal to k , the second part also have k different characters so break it ; If the second part has less than k different characters , then print " Not ▁ Possible " ; Otherwise print both parts ; Driver code ; Function call
import java . util . * ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . length ; Map < Character , Boolean > has = new HashMap < > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } i ++ ; has . clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { System . out . println ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; while ( i < n ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . toCharArray ( ) , k ) ; } }
Count substrings that contain all vowels | SET 2 | Java implementation of the approach ; Function that returns true if c is a vowel ; Function to return the count of sub - strings that contain every vowel at least once and no consonant ; Map is used to store count of each vowel ; Start index is set to 0 initially ; If substring till now have all vowels atleast once increment start index until there are all vowels present between ( start , i ) and add n - i each time ; Function to extract all maximum length sub - strings in s that contain only vowels and then calls the countSubstringsUtil ( ) to find the count of valid sub - strings in that string ; If current character is a vowel then append it to the temp string ; The sub - string containing all vowels ends here ; If there was a valid sub - string ; Reset temp string ; For the last valid sub - string ; Driver code
import java . util . * ; class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static int countSubstringsUtil ( char [ ] s ) { int count = 0 ; Map < Character , Integer > mp = new HashMap < > ( ) ; int n = s . length ; int start = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } while ( mp . containsKey ( ' a ' ) && mp . containsKey ( ' e ' ) && mp . containsKey ( ' i ' ) && mp . containsKey ( ' o ' ) && mp . containsKey ( ' u ' ) && mp . get ( ' a ' ) > 0 && mp . get ( ' e ' ) > 0 && mp . get ( ' i ' ) > 0 && mp . get ( ' o ' ) > 0 && mp . get ( ' u ' ) > 0 ) { count += n - i ; mp . put ( s [ start ] , mp . get ( s [ start ] ) - 1 ) ; start ++ ; } } return count ; } static int countSubstrings ( String s ) { int count = 0 ; String temp = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) { temp += s . charAt ( i ) ; } else { if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp . toCharArray ( ) ) ; temp = " " ; } } if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp . toCharArray ( ) ) ; return count ; } public static void main ( String [ ] args ) { String s = " aeouisddaaeeiouua " ; System . out . println ( countSubstrings ( s ) ) ; } }
Remove first adjacent pairs of similar characters until possible | Java implementation of the above approach ; Function to remove adjacent duplicates ; Iterate for every character in the string ; If ans string is empty or its last character does not match with the current character then append this character to the string ; Matches with the previous one ; Return the answer ; Driver Code
class GFG { static String removeDuplicates ( String S ) { String ans = " " ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( ans . isEmpty ( ) || ans . charAt ( ans . length ( ) - 1 ) != S . charAt ( i ) ) ans += S . charAt ( i ) ; else if ( ans . charAt ( ans . length ( ) - 1 ) == S . charAt ( i ) ) ans = ans . substring ( 0 , ans . length ( ) - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " keexxllx " ; System . out . println ( removeDuplicates ( str ) ) ; } }
Reverse individual words with O ( 1 ) extra space | Java implementation of the approach ; Function to resturn the string after reversing the individual words ; Pointer to the first character of the first word ; If the current word has ended ; Pointer to the last character of the current word ; Reverse the current word ; Pointer to the first character of the next word ; Driver code
class GFG { static String swap ( String str , int i , int j ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setCharAt ( i , str . charAt ( j ) ) ; sb . setCharAt ( j , str . charAt ( i ) ) ; return sb . toString ( ) ; } static String reverseWords ( String str ) { int start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' || i == str . length ( ) - 1 ) { int end ; if ( i == str . length ( ) - 1 ) end = i ; else end = i - 1 ; while ( start < end ) { str = swap ( str , start , end ) ; start ++ ; end -- ; } start = i + 1 ; } } return str ; } public static void main ( String args [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; System . out . println ( reverseWords ( str ) ) ; } }
Reverse the Words of a String using Stack | Java implementation of the approach ; Function to reverse the words of the given sentence ; Create an empty character array stack ; Push words into the stack ; Get the words in reverse order ; Driver code
import java . util . Arrays ; import java . util . Stack ; class GFG { static void reverse ( String k ) { Stack < String > s = new Stack < > ( ) ; String [ ] token = k . split ( " ▁ " ) ; for ( int i = 0 ; i < token . length ; i ++ ) { s . push ( token [ i ] ) ; } while ( ! s . empty ( ) ) { System . out . print ( s . peek ( ) + " ▁ " ) ; s . pop ( ) ; } } public static void main ( String [ ] args ) { String k = " geeks ▁ for ▁ geeks " ; reverse ( k ) ; } }
Count of substrings which contains a given character K times | Java program to count the number of substrings which contains the character C exactly K times ; Function to count the number of substrings which contains the character C exactly K times ; left and right counters for characters on both sides of substring window ; left and right pointer on both sides of substring window ; initialize the frequency ; result and length of string ; initialize the left pointer ; initialize the right pointer ; traverse all the window substrings ; counting the characters on leftSide of substring window ; counting the characters on rightSide of substring window ; Add the possible substrings on both sides to result ; Setting the frequency for next substring window ; reset the left , right counters ; Driver code
class GFG { static int countSubString ( String s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . length ( ) ; while ( s . charAt ( left ) != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s . charAt ( right ) == c ) { freq ++ ; } right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s . charAt ( left ) != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s . charAt ( right ) != c ) { if ( s . charAt ( right ) == c ) { freq ++ ; } right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } public static void main ( String args [ ] ) { String s = "3123231" ; char c = '3' ; int k = 2 ; System . out . println ( countSubString ( s , c , k ) ) ; } }
Count of sub | Java Implementation of above approach ; Function to return the total required sub - sequences ; Find ways for all values of x ; x + 1 ; Removing all unnecessary digits ; Prefix Sum Array for X + 1 digit ; Sum of squares ; Previous sum of all possible pairs ; To find sum of multiplication of all possible pairs ; To prevent overcounting ; Adding ways for all possible x ; Driver code
import java . io . * ; class GFG { static int solve ( String test , int MOD ) { int size = test . length ( ) ; int total = 0 ; for ( int i = 0 ; i <= 8 ; i ++ ) { int x = i ; int y = i + 1 ; String newtest = " " ; for ( int j = 0 ; j < size ; j ++ ) { if ( test . charAt ( j ) == x + 48 || test . charAt ( j ) == y + 48 ) { newtest += test . charAt ( j ) ; } } if ( newtest . length ( ) > 0 ) { int size1 = newtest . length ( ) ; int [ ] prefix = new int [ size1 ] ; for ( int j = 0 ; j < size1 ; j ++ ) { prefix [ j ] = 0 ; if ( newtest . charAt ( j ) == y + 48 ) { prefix [ j ] ++ ; } } for ( int j = 1 ; j < size1 ; j ++ ) { prefix [ j ] += prefix [ j - 1 ] ; } int count = 0 ; int firstcount = 0 ; int ss = 0 ; int prev = 0 ; for ( int j = 0 ; j < size1 ; j ++ ) { if ( newtest . charAt ( j ) == x + 48 ) { count ++ ; firstcount ++ ; } else { ss += count * count ; int pairsum = ( firstcount * firstcount - ss ) / 2 ; int temp = pairsum ; pairsum -= prev ; prev = temp ; int secondway = prefix [ size1 - 1 ] ; if ( j != 0 ) secondway -= prefix [ j - 1 ] ; int answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) ; answer /= 4 ; answer += ( pairsum * secondway * ( secondway - 1 ) ) / 2 ; total += answer ; count = 0 ; } } } } return total ; } public static void main ( String [ ] args ) { String test = "13134422" ; int MOD = 1000000007 ; System . out . println ( solve ( test , MOD ) ) ; } }
Find if it is possible to make a binary string which contanins given number of "0" , "1" , "01" and "10" as sub sequences | Java implementation of the approach ; Function that returns true if it is possible to make a binary string consisting of l 0 ' s , ▁ m ▁ 1' s , x "01" sub - sequences and y "10" sub - sequences ; Driver code
class sol { static boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void main ( String args [ ] ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Count pairs of characters in a string whose ASCII value difference is K | Java implementation of the approach ; Function to return the count of required pairs of characters ; Length of the string ; To store the frequency of each character ; Update the frequency of each character ; To store the required count of pairs ; If ascii value difference is zero ; If there exists similar characters more than once ; If there exits characters with ASCII value difference as k ; Return the required count ; Driver code
import java . util . Arrays ; class GFG { static int MAX = 26 ; static int countPairs ( char [ ] str , int k ) { int n = str . length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int cnt = 0 ; if ( k == 0 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 1 ) { cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 ) ; } } } else { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 0 && i + k < MAX && freq [ i + k ] > 0 ) { cnt += ( freq [ i ] * freq [ i + k ] ) ; } } ; } return cnt ; } public static void main ( String [ ] args ) { String str = " abcdab " ; int k = 0 ; System . out . println ( countPairs ( str . toCharArray ( ) , k ) ) ; } }