codeparrot/xlcost-text-to-code huggingface dataset:
Check if summation of two words is equal to target word | Java program for the above approach ; Function to check weather summation of two words equal to target word ; Store the length of each String ; Reverse the Strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , Math . max ( L , Math . max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static String isSumEqual ( String A , String B , String C ) { int L = A . length ( ) ; int M = B . length ( ) ; int N = A . length ( ) ; A = reverse ( A ) ; B = reverse ( B ) ; C = reverse ( C ) ; int rem = 0 ; for ( int i = 0 ; i < Math . max ( L , Math . max ( M , N ) ) ; i ++ ) { int curr = rem ; if ( i < L ) curr += A . charAt ( i ) - ' a ' ; if ( i < M ) curr += B . charAt ( i ) - ' a ' ; rem = curr / 10 ; curr %= 10 ; if ( i < N && curr != C . charAt ( i ) - ' a ' ) { return " No " ; } } if ( rem > 0 ) return " No " ; else return " Yes " ; } 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 A = " acb " , B = " cba " , C = " cdb " ; System . out . print ( isSumEqual ( A , B , C ) ) ; } }
Minimize hamming distance in Binary String by setting only one K size substring bits | Java program for the above approach ; Function to find minimum Hamming Distance after atmost one operation ; Store the size of the string ; Store the prefix sum of 1 s ; Create Prefix Sum array ; Initialize cnt as number of ones in string S ; Store the required result ; Traverse the string , S ; Store the number of 1 s in the substring S [ i , i + K - 1 ] ; Update the answer ; Return the result ; Driver Code ; Given Input ; Function Call
public class GFG { static int minimumHammingDistance ( String S , int K ) { int n = S . length ( ) ; int [ ] pref = new int [ n ] ; pref [ 0 ] = S . charAt ( 0 ) - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + ( S . charAt ( i ) - '0' ) ; int cnt = pref [ n - 1 ] ; int ans = cnt ; for ( int i = 0 ; i < n - K ; i ++ ) { int value = pref [ i + K - 1 ] - ( i - 1 >= 0 ? pref [ i - 1 ] : 0 ) ; ans = Math . min ( ans , cnt - value + ( K - value ) ) ; } return ans ; } public static void main ( String args [ ] ) { String s = "101" ; int K = 2 ; System . out . println ( minimumHammingDistance ( s , K ) ) ; } }
Check if a permutation of S2 can be obtained by adding or removing characters from S1 | Java program for the above approach ; Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code
public class GFG { private static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; else if ( n == 2 ) return true ; else if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } private static void checkPermutation ( String s1 , String s2 ) { int freq [ ] = new int [ 26 ] ; for ( char ch : s1 . toCharArray ( ) ) { freq [ ch - ' a ' ] -- ; } for ( char ch : s2 . toCharArray ( ) ) { freq [ ch - ' a ' ] ++ ; } boolean isAllChangesPrime = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) { continue ; } else if ( ! isPrime ( Math . abs ( freq [ i ] ) ) ) { isAllChangesPrime = false ; break ; } } if ( isAllChangesPrime ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S1 = " gekforgk " ; String S2 = " geeksforgeeks " ; checkPermutation ( S1 , S2 ) ; } }
Count ways to split a string into two subsets that are reverse of each other | Java program for the above approach ; Function to find the total number of ways to partitiaon the String into two subset satisfying the conditions ; Stores the resultant number of ways of splitting ; Iterate over the range [ 0 , 2 ^ N ] ; Traverse the String S ; If ith bit is set , then append the character S [ i ] to X ; Otherwise , append the character S [ i ] to Y ; Reverse the second String ; If X is equal to Y ; Return the total number of ways ; Driver Code
import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { static int countWays ( String S , int N ) { int ans = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { String X = " " , Y = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( mask >> i & 1 ) == 1 ) { X += S . charAt ( i ) ; } else { Y += S . charAt ( i ) ; } } Y = new StringBuilder ( Y ) . reverse ( ) . toString ( ) ; if ( X . equals ( Y ) ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String S = " mippiisssisssiipsspiim " ; int N = S . length ( ) ; System . out . println ( countWays ( S , N ) ) ; } }
Check if String formed by first and last X characters of a String is a Palindrome | Java program for the above approach ; Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { public static void isEqualSubstring ( String str , int x ) { int n = str . length ( ) ; int i = 0 ; while ( i < n && i < x ) { if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) { System . out . println ( " false " ) ; return ; } i ++ ; } System . out . println ( " true " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; int x = 3 ; isEqualSubstring ( str , x ) ; } }
Minimum number of replacement done of substring "01" with "110" to remove it completely | Java program for the above approach ; Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code
import java . io . * ; class GFG { public static void minimumOperations ( String S , int N ) { int ans = 0 ; int cntOne = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '0' ) { ans += cntOne ; cntOne *= 2 ; } else cntOne ++ ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "001" ; int N = S . length ( ) ; minimumOperations ( S , N ) ; } }
Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string | Java program for the above approach ; Function to find the number of ways to obtain different Bitwise OR ; Stores the count of pairs t00 , t10 , t01 , t11 ; Traverse the characters of the string S1 and S2 ; Count the pair ( 0 , 0 ) ; Count the pair ( 1 , 0 ) ; Count the pair ( 1 , 1 ) ; Count the pair ( 0 , 1 ) ; Number of ways to calculate the different bitwise OR ; Print the result ; Driver Code
import java . util . * ; class GFG { static void differentBitwiseOR ( String s1 , String s2 ) { int n = s1 . length ( ) ; int t00 = 0 , t10 = 0 , t01 = 0 , t11 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '0' ) { t00 ++ ; } if ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) { t10 ++ ; } if ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '1' ) { t11 ++ ; } if ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) { t01 ++ ; } } int ans = t00 * t10 + t01 * t10 + t00 * t11 ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S1 = "01001" ; String S2 = "11011" ; differentBitwiseOR ( S1 , S2 ) ; } }
Minimum number of removals required such that no subsequence of length 2 occurs more than once | Java program for the above approach ; Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver code
import java . io . * ; class GFG { static void RemoveCharacters ( String s ) { String ans = " " ; int [ ] c = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; int pos = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( c [ ( int ) s . charAt ( i ) - 97 ] == 0 ) { c [ ( int ) s . charAt ( i ) - 97 ] = 1 ; pos = i ; ans += s . charAt ( i ) ; } } for ( int i = pos + 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( 0 ) ) { ans += s . charAt ( i ) ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " abcaadbcd " ; RemoveCharacters ( S ) ; } }
Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | Java program for the above approach ; Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Driver code ; Given Input ; Function call
import java . util . * ; class GFG { static void changeCharacters ( String S , int N , int K ) { int flag = 1 ; int count = 0 ; Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . empty ( ) ) { if ( S . charAt ( i ) == '0' ) { flag = 0 ; break ; } count = 0 ; st . push ( S . charAt ( i ) ) ; } else { if ( S . charAt ( i ) == '0' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String args [ ] ) { String S = "100100" ; int N = S . length ( ) ; int K = 2 ; changeCharacters ( S , N , K ) ; } }
Check if all prefixes of a number is divisible by remaining count of digits | Java program for the above approach ; Function to check if all prefixes of a number is divisible by remaining count of digits or not ; Traverse and check divisibility for each updated number ; Update the original number ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { public static boolean prefixDivisble ( int n ) { int i = 1 ; while ( n > 0 ) { if ( n % i != 0 ) return false ; n = n / 10 ; i ++ ; } return true ; } public static void main ( String [ ] args ) { int n = 52248 ; if ( prefixDivisble ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings | Java program for the above approach ; Function to calculate minimum cost to convert all the characters of S to '1' ; Stores the result ; Stores the number of groups that have 0 as characters ; Traverse the string S ; If current character is '0' ; If count is greater than 0 ; Set the count to 0 ; If the last few consecutive characters are '0' ; If string contains all characters as '1' ; Minimum Cost ; Driver Code ; Given Input ; Function Call
class GFG { static void MinimumCost ( String S , int A , int B ) { int count = 0 ; int group = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '0' ) { count += 1 ; } else { if ( count > 0 ) { group += 1 ; } count = 0 ; } } if ( count > 0 ) group += 1 ; if ( group == 0 ) { System . out . println ( 0 ) ; } else { System . out . print ( Math . min ( A , B ) * ( group - 1 ) + B ) ; } } public static void main ( String args [ ] ) { int A = 1 ; int B = 5 ; String S = "01100" ; MinimumCost ( S , A , B ) ; } }
Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | Java program for the above approach ; Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
import java . io . * ; class GFG { static boolean isPrime ( int num ) { boolean flag = false ; if ( num > 1 ) { for ( int i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } static int order ( int x ) { int n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } static boolean isArmstrong ( int x ) { int n = order ( x ) ; int temp = x ; int sum1 = 0 ; while ( temp != 0 ) { int r = temp % 10 ; sum1 = sum1 + ( int ) ( Math . pow ( r , n ) ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } static int count_armstrong ( String [ ] li ) { int c = 0 ; for ( String ele : li ) { int val = 0 ; for ( char che : ele . toCharArray ( ) ) val += che ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } static int count_prime ( String [ ] li ) { int c = 0 ; for ( String ele : li ) { int val = 0 ; for ( char che : ele . toCharArray ( ) ) val += che ; if ( isPrime ( val ) ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { String [ ] arr = { " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " } ; System . out . println ( " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : ▁ " + count_armstrong ( arr ) ) ; System . out . println ( " Number ▁ of ▁ Prime ▁ Strings ▁ are : ▁ " + count_prime ( arr ) ) ; } }
Minimize cost of flipping or swaps to make a Binary String balanced | Java program for the above approach ; Function to find the minimum cost to convert the given string into balanced string ; Stores count of 1 ' s ▁ and ▁ 0' s in the string ; Traverse the string ; Increment count1 ; Increment count 0 ; Stores absolute difference of counts of 0 ' s ▁ and ▁ 1' s ; If string consists of only 0 ' s ▁ and ▁ 1' s ; Print minimum cost ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findMinimumCost ( String s , int N ) { int count_1 = 0 , count_0 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) count_1 ++ ; else count_0 ++ ; } int k = Math . abs ( count_0 - count_1 ) ; if ( count_1 == N count_0 == N ) System . out . println ( - 1 ) ; else System . out . println ( k / 2 ) ; } public static void main ( String [ ] args ) { String S = "110110" ; int N = S . length ( ) ; findMinimumCost ( S , N ) ; } }
Minimum time required to complete all tasks with alteration of their order allowed | Java program for the above approach ; Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver code
import java . util . HashMap ; import java . util . Map ; class GFG { static void findMinimumTime ( String S , int N , int K ) { if ( N == 0 ) { System . out . println ( 0 ) ; return ; } int maxfreq = Integer . MIN_VALUE ; char maxchar = ' ▁ ' ; HashMap < Character , Integer > um = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { um . put ( S . charAt ( i ) , um . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; if ( um . get ( S . charAt ( i ) ) > maxfreq ) { maxfreq = um . get ( S . charAt ( i ) ) ; maxchar = S . charAt ( i ) ; } } int emptySlots = ( maxfreq - 1 ) * K ; for ( Map . Entry < Character , Integer > it : um . entrySet ( ) ) { if ( it . getKey ( ) == maxchar ) continue ; emptySlots -= Math . min ( it . getValue ( ) , maxfreq - 1 ) ; } int ans = N + Math . max ( 0 , emptySlots ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " AAABBB " ; int K = 2 ; int N = S . length ( ) ; findMinimumTime ( S , N , K ) ; } }
Check if all characters of a string can be made equal by increments or decrements | Java program for the above approach ; Function to check if it is possible to make all characters of string S same or not ; Length of string ; Stores the sum of ASCII value ; Traverse the string S ; Update the weightOfString ; If the sum is divisible by N then print " Yes " ; Otherwise print " No " ; Driver Code
import java . io . * ; class GFG { static void canMakeEqual ( String S ) { int N = S . length ( ) ; int weightOfString = 0 ; for ( int i = 0 ; i < N ; i ++ ) { weightOfString += S . charAt ( i ) - ' a ' + 1 ; } if ( weightOfString % N == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " beb " ; canMakeEqual ( S ) ; } }
Modify a string by circularly shifting each character to the right by respective frequencies | java program for the above approach ; Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void addFrequencyToCharacter ( String Str ) { int frequency [ ] = new int [ 26 ] ; int N = Str . length ( ) ; char S [ ] = Str . toCharArray ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( ( int ) ( S [ i ] ) + add <= ( int ) ( ' z ' ) ) S [ i ] = ( char ) ( ( int ) ( S [ i ] ) + add ) ; else { add = ( ( int ) ( S [ i ] ) + add ) - ( ( int ) ( ' z ' ) ) ; S [ i ] = ( char ) ( ( int ) ( ' a ' ) + add - 1 ) ; } } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; } }
Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | java program for the above approach ; Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int minLength ( String S ) { Stack < Character > st = new Stack < > ( ) ; for ( char ch : S . toCharArray ( ) ) { if ( st . isEmpty ( ) ) st . push ( ch ) ; else { char top = st . peek ( ) ; if ( Math . abs ( ch - top ) == 1 ) st . pop ( ) ; else { st . push ( ch ) ; } } } return st . size ( ) ; } public static void main ( String [ ] args ) { String S = "12213" ; System . out . println ( minLength ( S ) ) ; } }
Minimum time required to complete all tasks without altering their order | Java program for the above approach ; Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code
import java . util . * ; class GFG { static void findMinimumTime ( String tasks , int K ) { Map < Character , Integer > map = new HashMap < > ( ) ; int curr_time = 0 ; for ( char c : tasks . toCharArray ( ) ) { if ( map . containsKey ( c ) ) { if ( curr_time - map . get ( c ) <= K ) { curr_time += K - ( curr_time - map . get ( c ) ) + 1 ; } } map . put ( c , curr_time ) ; curr_time ++ ; } System . out . println ( curr_time ) ; } public static void main ( String [ ] args ) { String S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; } }
Check if a string is a subsequence of another string ( using Stacks ) | Java approach for the above approach ; Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; if S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code
import java . util . Stack ; public class GFG { static void checkforSubsequence ( String S , String target ) { Stack < Character > s = new Stack < > ( ) ; for ( int i = 0 ; i < target . length ( ) ; i ++ ) { s . push ( target . charAt ( i ) ) ; } for ( int i = ( int ) S . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s . empty ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( S . charAt ( i ) == s . peek ( ) ) { s . pop ( ) ; } } if ( s . empty ( ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " KOTTAYAM " ; String target = " KOTA " ; checkforSubsequence ( S , target ) ; } }