codeparrot/xlcost-text-to-code huggingface dataset:
Students with maximum average score of three subjects | Java program to find the list of students having maximum average score ; Function to find the list of students having maximum average score Driver code ; Variables to store average score of a student and maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver code ; Number of elements in string array
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void getStudentsList ( String [ ] file ) { int avgScore ; int maxAvgScore = Integer . MIN_VALUE ; ArrayList < String > names = new ArrayList < > ( ) ; for ( int i = 0 ; i < file . length ; i += 4 ) { avgScore = ( Integer . parseInt ( file [ i + 1 ] ) + Integer . parseInt ( file [ i + 2 ] ) + Integer . parseInt ( file [ i + 3 ] ) ) / 3 ; if ( avgScore > maxAvgScore ) { maxAvgScore = avgScore ; names . clear ( ) ; names . add ( file [ i ] ) ; } else if ( avgScore == maxAvgScore ) names . add ( file [ i ] ) ; } for ( int i = 0 ; i < names . size ( ) ; i ++ ) { System . out . print ( names . get ( i ) + " ▁ " ) ; } System . out . print ( maxAvgScore ) ; } public static void main ( String args [ ] ) { String [ ] file = { " Shrikanth " , "20" , "30" , "10" , " Ram " , "100" , "50" , "10" } ; getStudentsList ( file ) ; } }
Number of sub | Java program to find the number of sub - Strings of s1 which are anagram of any sub - String of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of String pat [ ] in String txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - Strings of s1 that are anagrams of any sub - String of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of subStrings ; If s2 has any subString which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver code
class GFG { static int MAX_LEN = 1005 ; static int MAX_CHAR = 26 ; static int ALL_CHARS = 256 ; static boolean compare ( char [ ] arr1 , char [ ] arr2 ) { for ( int i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static boolean search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int i ; char countP [ ] = new char [ ALL_CHARS ] ; char countTW [ ] = new char [ ALL_CHARS ] ; for ( i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } static int calculatesubString ( String s1 , String s2 , int n ) { int count = 0 , j = 0 , x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . substring ( i , i + len ) , s2 ) ) { count = count + 1 ; } } } return count ; } public static void main ( String args [ ] ) { String str1 = " PLEASEHELPIMTRAPPED " ; String str2 = " INAKICKSTARTFACTORY " ; int len = str1 . length ( ) ; System . out . println ( calculatesubString ( str1 , str2 , len ) ) ; } }
Sum of the alphabetical values of the characters of a string | Java implementation of the approach ; Function to find string score ; Driver code
import java . io . * ; class GFG { static int strScore ( String str [ ] , String s , int n ) { int score = 0 , index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == s ) { for ( int j = 0 ; j < s . length ( ) ; j ++ ) score += s . charAt ( j ) - ' a ' + 1 ; index = i + 1 ; break ; } } score = score * index ; return score ; } public static void main ( String [ ] args ) { String str [ ] = { " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " } ; String s = " abhinav " ; int n = str . length ; int score = strScore ( str , s , n ) ; System . out . println ( score ) ; } }
Minimum swaps to group similar characters side by side ? | checks whether a String has similar characters side by side ; If similar chars side by side , continue ; If we have found a char equal to current char and does not exist side to it , return false ; counts min swap operations to convert a String that has similar characters side by side ; Base case ; considering swapping of i and l chars ; Backtrack ; not considering swapping of i and l chars ; taking Math . min of above two ; Utility swap ; Driver code
import java . util . * ; class GFG { static boolean sameJavaharAdj ( char str [ ] ) { int n = str . length , i ; TreeSet < Character > st = new TreeSet < > ( ) ; st . add ( str [ 0 ] ) ; for ( i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) { continue ; } if ( st . contains ( str [ i ] ) & ( str [ i ] != st . last ( ) ) ) { return false ; } st . add ( str [ i ] ) ; } return true ; } static int minSwaps ( char str [ ] , int l , int r , int cnt , int minm ) { if ( l == r ) { if ( sameJavaharAdj ( str ) ) { return cnt ; } else { return Integer . MAX_VALUE ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { swap ( str , i , l ) ; cnt ++ ; int x = minSwaps ( str , l + 1 , r , cnt , minm ) ; swap ( str , i , l ) ; cnt -- ; int y = minSwaps ( str , l + 1 , r , cnt , minm ) ; minm = Math . min ( minm , Math . min ( x , y ) ) ; } return minm ; } static void swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public static void main ( String [ ] args ) { String str = " abbaacb " ; int n = str . length ( ) , cnt = 0 , minm = Integer . MAX_VALUE ; System . out . print ( minSwaps ( str . toCharArray ( ) , 0 , n - 1 , cnt , minm ) ) ; ; } }
Union | Naive implementation of find ; Naive implementation of union ( )
static int find ( int parent [ ] , int i ) { if ( parent [ i ] == - 1 ) return i ; return find ( parent , parent [ i ] ) ; } static void Union ( int parent [ ] , int x , int y ) { int xset = find ( parent , x ) ; int yset = find ( parent , y ) ; parent [ xset ] = yset ; }
Get K | Java implementation of above approach ; Function to return the K - th letter from new String . ; finding size = length of new string S ' ; get the K - th letter ; Driver program
class GFG { static String K_thletter ( String S , int K ) { int N = S . length ( ) ; long size = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( Character . isDigit ( S . charAt ( i ) ) ) { size = size * ( S . charAt ( i ) - '0' ) ; } else { size += 1 ; } } for ( int i = N - 1 ; i >= 0 ; -- i ) { K %= size ; if ( K == 0 && Character . isAlphabetic ( S . charAt ( i ) ) ) { return ( String ) " " + S . charAt ( i ) ; } if ( Character . isDigit ( S . charAt ( i ) ) ) { size = size / ( S . charAt ( i ) - '0' ) ; } else { size -= 1 ; } } return null ; } public static void main ( String [ ] args ) { String S = " geeks2for2" ; int K = 15 ; System . out . println ( K_thletter ( S , K ) ) ; } }
Minimum number of Parentheses to be added to make it valid | Java Program to find minimum number of ' ( ' or ' ) ' must be added to make Parentheses string valid . ; Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer
public class GFG { static int minParentheses ( String p ) { int bal = 0 ; int ans = 0 ; for ( int i = 0 ; i < p . length ( ) ; ++ i ) { bal += p . charAt ( i ) == ' ( ' ? 1 : - 1 ; if ( bal == - 1 ) { ans += 1 ; bal += 1 ; } } return bal + ans ; } public static void main ( String args [ ] ) { String p = " ( ) ) " ; System . out . println ( minParentheses ( p ) ) ; } }
Check if suffix and prefix of a string are palindromes | Java implementation of the approach ; Utility reverse ; Swap values of l and r ; Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code
import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = 0 ; 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 ) ; } static boolean isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . equals ( p ) ) ; } static boolean CheckStr ( String s ) { int l = s . length ( ) ; int i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( 0 , i ) ) ) { break ; } } if ( i == ( l + 1 ) ) { return false ; } i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( l - i , l ) ) ) { return true ; } } return false ; } public static void main ( String args [ ] ) { String s = " abccbarfgdbd " ; if ( CheckStr ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Rotations of a Binary String with Odd Value | Java program to find count of rotations with odd value . ; function to calculate total odd decimal equivalent ; Driver code
class solution { static int oddEquivalent ( String s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) count ++ ; } return count ; } public static void main ( String ar [ ] ) { String s = "1011011" ; int n = s . length ( ) ; System . out . println ( oddEquivalent ( s , n ) ) ; } }
Minimum operation require to make first and last character same | Java program to find minimum operation require to make first and last character same ; Return the minimum operation requires to make string first and last character same . ; Store indexes of first occurrences of characters . ; Initialize all the elements to - 1 ; Initialize result ; Traverse through all characters ; Find first occurrence ; Update result for subsequent occurrences ; Driven Program
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static final int MAX = 256 ; static int minimumOperation ( String s ) { int n = s . length ( ) ; Vector < Integer > first_occ = new Vector < Integer > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) first_occ . add ( i , - 1 ) ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) s . charAt ( i ) ; if ( first_occ . elementAt ( x ) == - 1 ) first_occ . set ( x , i ) ; else { int last_occ = ( n - i - 1 ) ; res = Math . min ( res , first_occ . get ( x ) + last_occ ) ; } } return res ; } public static void main ( String args [ ] ) { String s = " bacdefghipalop " ; System . out . println ( minimumOperation ( s ) ) ; } }
Lexicographically smallest and largest substring of size k | Java program to find lexicographically largest and smallest substrings of size k . ; Initialize min and max as first substring of size k ; Consider all remaining substrings . We consider every substring ending with index i . ; Print result . ; Driver Code
public class GFG { public static void getSmallestAndLargest ( String s , int k ) { String currStr = s . substring ( 0 , k ) ; String lexMin = currStr ; String lexMax = currStr ; for ( int i = k ; i < s . length ( ) ; i ++ ) { currStr = currStr . substring ( 1 , k ) + s . charAt ( i ) ; if ( lexMax . compareTo ( currStr ) < 0 ) lexMax = currStr ; if ( lexMin . compareTo ( currStr ) > 0 ) lexMin = currStr ; } System . out . println ( lexMin ) ; System . out . println ( lexMax ) ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int k = 3 ; getSmallestAndLargest ( str , k ) ; } }
Program to print the initials of a name with the surname | Java program to print the initials of a name with the surname ; to remove any leading or trailing spaces ; to store extracted words ; forming the word ; when space is encountered it means the name is completed and thereby extracted ; printing the first letter of the name in capital letters ; for the surname , we have to print the entire surname and not just the initial string " t " has the surname now ; first letter of surname in capital letter ; rest of the letters in small ; printing surname ; Driver Code
import java . util . * ; class Initials { public static void printInitials ( String str ) { int len = str . length ( ) ; str = str . trim ( ) ; String t = " " ; for ( int i = 0 ; i < len ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) { t = t + ch ; } else { System . out . print ( Character . toUpperCase ( t . charAt ( 0 ) ) + " . ▁ " ) ; t = " " ; } } String temp = " " ; for ( int j = 0 ; j < t . length ( ) ; j ++ ) { if ( j == 0 ) temp = temp + Character . toUpperCase ( t . charAt ( 0 ) ) ; else temp = temp + Character . toLowerCase ( t . charAt ( j ) ) ; } System . out . println ( temp ) ; } public static void main ( String [ ] args ) { String str = " ishita ▁ bhuiya " ; printInitials ( str ) ; } }
Count of strings that can be formed from another string using each character at | Java program to print the number of times str2 can be formed from str1 using the characters of str1 only once ; Function to find the number of str2 that can be formed using characters of str1 ; iterate and mark the frequencies of all characters in str1 ; find the minimum frequency of every character in str1 ; Driver Code
class GFG { static int findNumberOfTimes ( String str1 , String str2 ) { int freq [ ] = new int [ 26 ] ; int freq2 [ ] = new int [ 26 ] ; int l1 = str1 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) { freq [ str1 . charAt ( i ) - ' a ' ] += 1 ; } int l2 = str2 . length ( ) ; for ( int i = 0 ; i < l2 ; i ++ ) { freq2 [ str2 . charAt ( i ) - ' a ' ] += 1 ; } int count = Integer . MAX_VALUE ; for ( int i = 0 ; i < l2 ; i ++ ) { if ( freq2 [ str2 . charAt ( i ) - ' a ' ] != 0 ) count = Math . min ( count , freq [ str2 . charAt ( i ) - ' a ' ] / freq2 [ str2 . charAt ( i ) - ' a ' ] ) ; } return count ; } public static void main ( String [ ] args ) { String str1 = " foreeksgekseg " ; String str2 = " geeks " ; System . out . println ( findNumberOfTimes ( str1 , str2 ) ) ; } }
String transformation using XOR and OR | Java program to check if string1 can be converted to string2 using XOR and OR operations ; function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both strings have only '0' ; if both string do not have a '1' . ; Driver code
import java . io . * ; import java . util . * ; class GFG { static boolean solve ( String s1 , String s2 ) { boolean flag1 = false , flag2 = false ; if ( s1 . length ( ) != s2 . length ( ) ) return false ; int l = s1 . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s1 . charAt ( i ) == '1' ) flag1 = true ; if ( s2 . charAt ( i ) == '1' ) flag2 = true ; if ( flag1 == true && flag2 == true ) return true ; } if ( ! flag1 && ! flag2 ) return true ; return false ; } public static void main ( String args [ ] ) { String s1 = "100101" ; String s2 = "100000" ; if ( solve ( s1 , s2 ) == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Smallest alphabet greater than a given character | Java Program to find the smallest character from the given set of letter , which is greater than the target element ; Returns the smallest character from the given set of letters that is greater than K ; Take the first element as l and the rightmost element as r ; if this while condition does not satisfy simply return the first element . ; Return the smallest element ; Driver Code ; Function call
class GFG { static char nextGreatestAlphabet ( char alphabets [ ] , char K ) { int n = alphabets . length ; if ( K >= alphabets [ n - 1 ] ) return alphabets [ 0 ] ; int l = 0 , r = alphabets . length - 1 ; int ans = - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( alphabets [ mid ] > K ) { r = mid - 1 ; ans = mid ; } else l = mid + 1 ; } return alphabets [ ans ] ; } public static void main ( String [ ] args ) { char letters [ ] = { ' A ' , ' r ' , ' z ' } ; char K = ' z ' ; char result = nextGreatestAlphabet ( letters , K ) ; System . out . println ( result ) ; } }
Longest substring of 0 s in a string formed by k concatenations | Java code to find maximum length of substring that contains only 0 's ; Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Drivers code
import java . io . * ; import java . util . * ; import java . lang . * ; class GfG { public static int subzero ( String s , int k ) { int ans = 0 , curr = 0 ; int len = s . length ( ) ; char [ ] str = s . toCharArray ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str [ i ] == '0' ) curr ++ ; else curr = 0 ; ans = Math . max ( ans , curr ) ; } if ( ans == len ) return len * k ; else { int pre = 0 , suff = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) pre ++ ; else break ; } for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) suff ++ ; else break ; } if ( k > 1 ) ans = Math . max ( ans , pre + suff ) ; return ans ; } } public static void main ( String [ ] args ) { String str = "00100110" ; int k = 5 ; System . out . println ( subzero ( str , k ) ) ; } }
Find nth term of the Dragon Curve Sequence | Java code to find nth term of the Dragon Curve Sequence ; function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Driver program ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
import java . util . * ; class solution { static String Dragon_Curve_Sequence ( int n ) { String s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { String temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { temp += s . charAt ( j ) ; if ( prev == '0' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } public static void main ( String args [ ] ) { int n = 4 ; String s = Dragon_Curve_Sequence ( n ) ; System . out . println ( s ) ; } }
Number of substrings of a string | Java program to count number of substrings of a string ; Driver code
import java . io . * ; public class GFG { static int countNonEmptySubstr ( String str ) { int n = str . length ( ) ; return n * ( n + 1 ) / 2 ; } public static void main ( String args [ ] ) { String s = " abcde " ; System . out . println ( countNonEmptySubstr ( s ) ) ; } }
Count substrings with each character occurring at most k times | Java program to count number of substrings in which each character has count less than or equal to k . ; variable to store count of substrings . ; array to store count of each character . ; Initialize all characters count to zero . ; increment character count ; check only the count of current character because only the count of this character is changed . The ending point is incremented to current position only if all other characters have count at most k and hence their count is not checked . If count is less than k , then increase ans by 1. ; } if count is less than k , then break as subsequent substrings for this starting point will also have count greater than k and hence are reduntant to check . ; return the final count of substrings . ; Driver code
import java . util . Arrays ; class GFG { static int findSubstrings ( String s , int k ) { int ans = 0 ; int cnt [ ] = new int [ 26 ] ; int i , j , n = s . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { Arrays . fill ( cnt , 0 ) ; for ( j = i ; j < n ; j ++ ) { cnt [ s . charAt ( j ) - ' a ' ] ++ ; if ( cnt [ s . charAt ( j ) - ' a ' ] <= k ) { ans ++ ; else { break ; } } } return ans ; } static public void main ( String [ ] args ) { String S = " aaabb " ; int k = 2 ; System . out . println ( findSubstrings ( S , k ) ) ; } }
Check if characters of one string can be swapped to form other | Java program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code
class GFG { static int MAX = 26 ; static boolean targetstring ( String str1 , String str2 ) { int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; if ( l1 != l2 ) return false ; int [ ] map = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) map [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) { map [ str2 . charAt ( i ) - ' a ' ] -- ; if ( map [ str2 . charAt ( i ) - ' a ' ] < 0 ) return false ; } return true ; } public static void main ( String args [ ] ) { String str1 = " geeksforgeeks " ; String str2 = " geegeeksksfor " ; if ( targetstring ( str1 , str2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Find the Number which contain the digit d | Java program to print the number which contain the digit d from 0 to n ; Returns true if d is present as digit in number x . ; Breal loop if d is present as digit ; If loop broke ; function to display the values ; Check all numbers one by one ; checking for digit ; Driver code
class GFG { static boolean isDigitPresent ( int x , int d ) { while ( x > 0 ) { if ( x % 10 == d ) break ; x = x / 10 ; } return ( x > 0 ) ; } static void printNumbers ( int n , int d ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i == d || isDigitPresent ( i , d ) ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 47 , d = 7 ; printNumbers ( n , d ) ; } }
Find one extra character in a string | Java program to find extra character in one string ; store string values in map ; store second string in map with frequency ; store first string in map with frequency ; if the frequency is 1 then this character is which is added extra ; Driver code ; given string ; find Extra Character
class GFG { static char findExtraCharcter ( char [ ] strA , char [ ] strB ) { int [ ] m1 = new int [ 256 ] ; for ( int i = 0 ; i < strB . length ; i ++ ) m1 [ strB [ i ] ] ++ ; for ( int i = 0 ; i < strA . length ; i ++ ) m1 [ strA [ i ] ] -- ; for ( int i = 0 ; i < m1 . length ; i ++ ) { if ( m1 [ i ] == 1 ) return ( char ) i ; } return Character . MIN_VALUE ; } public static void main ( String [ ] args ) { String strA = " abcd " ; String strB = " cbdad " ; System . out . println ( findExtraCharcter ( strA . toCharArray ( ) , strB . toCharArray ( ) ) ) ; } }