codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make the string satisfy the given condition | Java implementation of the approach ; Function to return the minimum operations required ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the minimum operations ; If the frequency of the current character in the string is less than 2 ; Count of characters to be removed so that the string starts and ends at the current character ; Driver code
class GFG { final static int MAX = 26 ; static int minOperation ( String str , int len ) { int first [ ] = new int [ MAX ] ; int last [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str . charAt ( i ) - ' a ' ) ; if ( first [ index ] == - 1 ) first [ index ] = i ; last [ index ] = i ; } int minOp = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == - 1 first [ i ] == last [ i ] ) continue ; int cnt = len - ( last [ i ] - first [ i ] + 1 ) ; if ( minOp == - 1 cnt < minOp ) minOp = cnt ; } return minOp ; } public static void main ( String [ ] args ) { String str = " abcda " ; int len = str . length ( ) ; System . out . println ( minOperation ( str , len ) ) ; } }
Queries to find the count of vowels in the substrings of the given string | Java implementation of the approach ; Function that returns true if ch is a vowel ; Function to return the count of vowels in the substring str [ l ... r ] ; To store the count of vowels ; For every character in the index range [ l , r ] ; If the current character is a vowel ; For every query ; Find the count of vowels for the current query ; Driver code
class GFG { static int N = 2 ; static boolean isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static int countVowels ( String str , int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) cnt ++ ; } return cnt ; } static void performQueries ( String str , int queries [ ] [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( countVowels ( str , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int queries [ ] [ ] = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = queries . length ; performQueries ( str , queries , q ) ; } }
Convert a String to a Singly Linked List | Java program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code
class GFG { static class node { char data ; node next ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static node string_to_SLL ( String text , node head ) { head = add ( text . charAt ( 0 ) ) ; node curr = head ; for ( int i = 1 ; i < text . length ( ) ; i ++ ) { curr . next = add ( text . charAt ( i ) ) ; curr = curr . next ; } return head ; } static void print ( node head ) { node curr = head ; while ( curr != null ) { System . out . print ( curr . data + " ▁ - > ▁ " ) ; curr = curr . next ; } } public static void main ( String [ ] args ) { String text = " GEEKS " ; node head = null ; head = string_to_SLL ( text , head ) ; print ( head ) ; } }
Reduce the string to minimum length with the given operation | Java implementation of the approach ; Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ; Driver code
import java . util . * ; class GFG { static int minLength ( String str , int len ) { Stack < Character > s = new Stack < Character > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . empty ( ) ) { s . push ( str . charAt ( i ) ) ; } else { char c = s . peek ( ) ; if ( c != str . charAt ( i ) && Character . toUpperCase ( c ) == Character . toUpperCase ( ( str . charAt ( i ) ) ) ) { s . pop ( ) ; } else { s . push ( str . charAt ( i ) ) ; } } } return s . size ( ) ; } public static void main ( String [ ] args ) { String str = " ASbBsd " ; int len = str . length ( ) ; System . out . println ( minLength ( str , len ) ) ; } }
Check whether two strings can be made equal by copying their characters with the adjacent ones | Java implementation of the above approach ; Function that returns true if both the strings can be made equal with the given operation ; Lengths of both the strings have to be equal ; To store the frequency of the characters of str1 ; For every character of str2 ; If current character of str2 also appears in str1 ; Driver code
class GFG { static int MAX = 26 ; static boolean canBeMadeEqual ( String str1 , String str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; if ( len1 == len2 ) { int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 . charAt ( i ) - ' a ' ] > 0 ) return true ; } } return false ; } public static void main ( String [ ] args ) { String str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimum characters that are to be inserted such that no three consecutive characters are same | Java implementation of the approach ; Function to return the count of characters that are to be inserted in str such that no three consecutive characters are same ; To store the count of operations required ; A character needs to be inserted after str [ i + 1 ] ; Current three consecutive characters are not same ; Driver code
import java . util . * ; class GFG { static int getCount ( char [ ] str , int n ) { int cnt = 0 ; int i = 0 ; while ( i < n - 2 ) { if ( str [ i ] == str [ i + 1 ] && str [ i ] == str [ i + 2 ] ) { cnt ++ ; i = i + 2 ; } else { i ++ ; } } return cnt ; } static public void main ( String [ ] arg ) { String str = " aabbbcc " ; int n = str . length ( ) ; System . out . println ( getCount ( str . toCharArray ( ) , n ) ) ; } }
Find the number of strings formed using distinct characters of a given string | Java implementation of the approach ; Function to return the factorial of n ; Function to return the count of all possible strings that can be formed with the characters of the given string without repeating characters ; To store the distinct characters of the string str ; Driver code
import java . util . * ; class GFG { static int fact ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact *= i ; return fact ; } static int countStrings ( String str , int n ) { Set < Character > distinct_char = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { distinct_char . add ( str . charAt ( i ) ) ; } return fact ( distinct_char . size ( ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; System . out . println ( countStrings ( str , n ) ) ; } }
Find the character made by adding all the characters of the given string | Java implementation of the approach ; Function to return the required character ; To store the sum of the characters of the given string ; Add the current character to the sum ; Return the required character ; Driver code
class GFG { static char getChar ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { sum += ( str . charAt ( i ) - ' a ' + 1 ) ; } if ( sum % 26 == 0 ) return ' z ' ; else { sum = sum % 26 ; return ( char ) ( ' a ' + sum - 1 ) ; } } public static void main ( String [ ] args ) { String str = " gfg " ; System . out . println ( getChar ( str ) ) ; } }
Reverse the given string in the range [ L , R ] | Java implementation of the approach ; Function to return the string after reversing characters in the range [ L , R ] ; Invalid range ; While there are characters to swap ; Swap ( str [ l ] , str [ r ] ) ; Driver code
import java . util . * ; class GFG { static String reverse ( char [ ] str , int len , int l , int r ) { if ( l < 0 r >= len l > r ) return " Invalid ▁ range ! " ; while ( l < r ) { char c = str [ l ] ; str [ l ] = str [ r ] ; str [ r ] = c ; l ++ ; r -- ; } String string = new String ( str ) ; return string ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; int l = 5 , r = 7 ; System . out . println ( reverse ( str . toCharArray ( ) , len , l , r ) ) ; } }
Program to Encrypt a String using ! and @ | Java program to Encrypt the String using ! and @ ; Function to encrypt the string ; evenPos is for storing encrypting char at evenPosition oddPos is for storing encrypting char at oddPosition ; Get the number of times the character is to be repeated ; if i is odd , print ' ! ' else print ' @ ' ; Driver code ; Encrypt the String
class GFG { static void encrypt ( char input [ ] ) { char evenPos = ' @ ' , oddPos = ' ! ' ; int repeat , ascii ; for ( int i = 0 ; i < input . length ; i ++ ) { ascii = input [ i ] ; repeat = ascii >= 97 ? ascii - 96 : ascii - 64 ; for ( int j = 0 ; j < repeat ; j ++ ) { if ( i % 2 == 0 ) System . out . printf ( " % c " , oddPos ) ; else System . out . printf ( " % c " , evenPos ) ; } } } public static void main ( String [ ] args ) { char input [ ] = { ' A ' , ' b ' , ' C ' , ' d ' } ; encrypt ( input ) ; } }
Check if expression contains redundant bracket or not | Set 2 | Java program to check for redundant braces in the string ; Function to check for redundant braces ; count of no of signs ; Driver Code
class GFG { static boolean IsRedundantBraces ( String A ) { int a = 0 , b = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) == ' ( ' && A . charAt ( i + 2 ) == ' ) ' ) return true ; if ( A . charAt ( i ) == ' * ' || A . charAt ( i ) == ' + ' || A . charAt ( i ) == ' - ' || A . charAt ( i ) == ' / ' ) a ++ ; if ( A . charAt ( i ) == ' ( ' ) b ++ ; } if ( b > a ) return true ; return false ; } public static void main ( String [ ] args ) { String A = " ( ( ( a + b ) ▁ + ▁ c ) ▁ + ▁ d ) " ; if ( IsRedundantBraces ( A ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Convert an unbalanced bracket sequence to a balanced sequence | Java implementation of the approach ; Function to return balancedBrackets string ; Initializing dep to 0 ; Stores maximum negative depth ; if dep is less than minDep ; if minDep is less than 0 then there is need to add ' ( ' at the front ; Reinitializing to check the updated string ; if dep is not 0 then there is need to add ' ) ' at the back ; Driver code
import java . util . * ; class GFG { static String balancedBrackets ( String str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < Math . abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } public static void main ( String [ ] args ) { String str = " ) ) ) ( ) " ; System . out . println ( balancedBrackets ( str ) ) ; } }
Minimum operations required to convert a binary string to all 0 s or all 1 s | Java implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code
class GFG { static int minOperations ( String str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) count ++ ; } return ( count + 1 ) / 2 ; } public static void main ( String [ ] args ) { String str = "000111" ; int n = str . length ( ) ; System . out . println ( minOperations ( str , n ) ) ; } }
Append a digit in the end to make the number equal to the length of the remaining string | Java implementation of the approach ; Function to return the required digit ; To store the position of the first numeric digit in the string ; To store the length of the string without the numeric digits in the end ; pw stores the current power of 10 and num is to store the number which is appended in the end ; If current character is a numeric digit ; Get the current digit ; Build the number ; If number exceeds the length ; Next power of 10 ; Append 0 in the end ; Required number that must be added ; If number is not a single digit ; Driver code
import java . io . * ; class GFG { static int find_digit ( String s , int n ) { int first_digit = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) < '0' || s . charAt ( i ) > '9' ) { first_digit = i ; break ; } } first_digit ++ ; int s_len = first_digit ; int num = 0 , pw = 1 ; int i = n - 1 ; while ( i >= 0 ) { if ( s . charAt ( i ) >= '0' && s . charAt ( i ) <= '9' ) { int digit = s . charAt ( i ) - '0' ; num = num + ( pw * digit ) ; if ( num >= s_len ) return - 1 ; pw = pw * 10 ; } i -- ; } num = num * 10 ; int req = s_len - num ; if ( req > 9 req < 0 ) return - 1 ; return req ; } public static void main ( String [ ] args ) { String s = " abcd0" ; int n = s . length ( ) ; System . out . print ( find_digit ( s , n ) ) ; } }
Check whether str1 can be converted to str2 with the given operations | Java implementation of the approach ; Function that returns true if str1 can be converted to str2 with the given operations ; Traverse from left to right ; If the two characters do not match ; If possible to combine ; If not possible to combine ; If the two characters match ; If possible to convert one string to another ; Driver code
class GFG { static boolean canConvert ( String str1 , String str2 ) { int i = 0 , j = 0 ; while ( i < str1 . length ( ) && j < str2 . length ( ) ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { if ( str1 . charAt ( i ) == '0' && str2 . charAt ( j ) == '1' && i + 1 < str1 . length ( ) && str1 . charAt ( i + 1 ) == '0' ) { i += 2 ; j ++ ; } else { return false ; } } else { i ++ ; j ++ ; } } if ( i == str1 . length ( ) && j == str2 . length ( ) ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = "00100" , str2 = "111" ; if ( canConvert ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Reverse the Words of a String using Stack | Java implementation of the above approach ; Function to reverse the words of the given String without using strtok ( ) . ; Create an empty String stack ; Create an empty temporary String ; Traversing the entire String ; Push the temporary variable into the stack ; Assigning temporary variable as empty ; For the last word of the String ; Get the words in reverse order ; Driver code
import java . util . * ; class GFG { static void reverse ( String s ) { Stack < String > stc = new Stack < > ( ) ; String temp = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ▁ ' ) { stc . add ( temp ) ; temp = " " ; } else { temp = temp + s . charAt ( i ) ; } } stc . add ( temp ) ; while ( ! stc . isEmpty ( ) ) { temp = stc . peek ( ) ; System . out . print ( temp + " ▁ " ) ; stc . pop ( ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " I ▁ Love ▁ To ▁ Code " ; reverse ( s ) ; } }
Print an N x M matrix such that each row and column has all the vowels in it | Java implementation of the approach ; Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code
class GFG { static void printMatrix ( int n , int m ) { if ( n < 5 m < 5 ) { System . out . print ( - 1 ) ; return ; } char [ ] s = " aeiou " . toCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( s [ j % 5 ] + " ▁ " ) ; } System . out . println ( ) ; char c = s [ 0 ] ; for ( int k = 0 ; k < 4 ; k ++ ) { s [ k ] = s [ k + 1 ] ; } s [ 4 ] = c ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; printMatrix ( n , m ) ; } }
Check if a given string is made up of two alternating characters | Java implementation of the approach ; Function that returns true if the string is made up of two alternating characters ; Check if ith character matches with the character at index ( i + 2 ) ; If string consists of a single character repeating itself ; Driver code
import java . io . * ; class GFG { static boolean isTwoAlter ( String s ) { for ( int i = 0 ; i < s . length ( ) - 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i + 2 ) ) { return false ; } } if ( s . charAt ( 0 ) == s . charAt ( 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " ABAB " ; if ( isTwoAlter ( str ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Number of character corrections in the given strings to make them equal | Java implementation of the approach ; Function to return the count of operations required ; To store the count of operations ; No operation required ; One operation is required when any two characters are equal ; Two operations are required when none of the characters are equal ; Return the minimum count of operations required ; Driver code
class GFG { static int minOperations ( int n , String a , String b , String c ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char x = a . charAt ( i ) ; char y = b . charAt ( i ) ; char z = c . charAt ( i ) ; if ( x == y && y == z ) ; else if ( x == y y == z x == z ) { ans ++ ; } else { ans += 2 ; } } return ans ; } public static void main ( String [ ] args ) { String a = " place " ; String b = " abcde " ; String c = " plybe " ; int n = a . length ( ) ; System . out . print ( minOperations ( n , a , b , c ) ) ; } }
Check if string can be made lexicographically smaller by reversing any substring | Java implementation of the approach ; Function that returns true if s can be made lexicographically smaller by reversing a sub - string in s ; Traverse in the string ; Check if s [ i + 1 ] < s [ i ] ; Not possible ; Driver code
class GFG { static boolean check ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) return true ; } return false ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( check ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of sub | Java implementation of the approach ; Function to return the count of required sub - strings ; Number of sub - strings from position of current x to the end of str ; To store the number of characters before x ; Driver code
class GFG { static int countSubStr ( String str , int n , char x ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == x ) { res += ( ( count + 1 ) * ( n - i ) ) ; count = 0 ; } else count ++ ; } return res ; } public static void main ( String [ ] args ) { String str = " abcabc " ; int n = str . length ( ) ; char x = ' c ' ; System . out . println ( countSubStr ( str , n , x ) ) ; } }
Count of sub | Java implementation of the approach ; Function to return the count of possible sub - strings of length n ; Driver code
import java . util . * ; class GFG { static int countSubStr ( String str , int n ) { int len = str . length ( ) ; return ( len - n + 1 ) ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int n = 5 ; System . out . print ( countSubStr ( str , n ) ) ; } }
Count of sub | Java implementation of the approach ; Function to return the number of sub - strings that do not contain the given character c ; Length of the string ; Traverse in the string ; If current character is different from the given character ; Update the number of sub - strings ; Reset count to 0 ; For the characters appearing after the last occurrence of c ; Driver code
class GFG { static int countSubstrings ( String s , char c ) { int n = s . length ( ) ; int cnt = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) != c ) cnt ++ ; else { sum += ( cnt * ( cnt + 1 ) ) / 2 ; cnt = 0 ; } } sum += ( cnt * ( cnt + 1 ) ) / 2 ; return sum ; } public static void main ( String [ ] args ) { String s = " baa " ; char c = ' b ' ; System . out . println ( countSubstrings ( s , c ) ) ; } }
Minimize ASCII values sum after removing all occurrences of one character | Java implementation of the approach ; Function to return the minimized sum ; To store the occurrences of each character of the string ; Update the occurrence ; Calculate the sum ; Get the character which is contributing the maximum value to the sum ; Count of occurrence of the character multiplied by its ASCII value ; Return the minimized sum ; Driver code
import java . util . Arrays ; import java . lang . Math ; class GfG { static int getMinimizedSum ( String str , int len ) { int i , maxVal = Integer . MIN_VALUE , sum = 0 ; int occurrences [ ] = new int [ 26 ] ; Arrays . fill ( occurrences , 0 ) ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str . charAt ( i ) - ' a ' ] ++ ; sum += ( int ) str . charAt ( i ) ; } for ( i = 0 ; i < 26 ; i ++ ) maxVal = Math . max ( maxVal , occurrences [ i ] * ( i + ' a ' ) ) ; return ( sum - maxVal ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . println ( getMinimizedSum ( str , len ) ) ; } }
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | Java implementation of the approach ; Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code
class GFG { static boolean isPalindrome ( String s ) { int i = 0 ; int j = s . length ( ) - 1 ; while ( i < j ) { if ( s . charAt ( i ) != s . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( String S1 , String S2 , int n ) { String S = " " ; for ( int i = 0 ; i < n ; i ++ ) { S = S + S1 . charAt ( i ) ; String Temp = " " ; for ( int j = i + 1 ; j < n ; j ++ ) Temp += S2 . charAt ( j ) ; if ( isPalindrome ( S + Temp ) ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { String S1 = " abcdf " , S2 = " sfgba " ; int n = S1 . length ( ) ; System . out . println ( getIndex ( S1 , S2 , n ) ) ; } }