codeparrot/xlcost-text-to-code huggingface dataset:
Check if string follows order of characters defined by a pattern or not | Set 1 | Java program check if characters in the input string follows the same order as determined by characters present in the given pattern ; Function to check if characters in the input string follows the same order as determined by characters present in the given pattern ; len stores length of the given pattern ; if length of pattern is more than length of input string , return false ; ; x , y are two adjacent characters in pattern ; find index of last occurrence of character x in the input string ; find index of first occurrence of character y in the input string ; return false if x or y are not present in the input string OR last occurrence of x is after the first occurrence of y in the input string ; return true if string matches the pattern ; Driver code
class GFG { static boolean checkPattern ( String str , String pattern ) { int len = pattern . length ( ) ; if ( str . length ( ) < len ) { return false ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { char x = pattern . charAt ( i ) ; char y = pattern . charAt ( i + 1 ) ; int last = str . lastIndexOf ( x ) ; int first = str . indexOf ( y ) ; if ( last == - 1 first == - 1 last > first ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " engineers ▁ rock " ; String pattern = " gsr " ; System . out . println ( checkPattern ( str , pattern ) ) ; } }
Calculate sum of all numbers present in a string | Java program to calculate sum of all numbers present in a string containing alphanumeric characters ; Function to calculate sum of all numbers present in a string containing alphanumeric characters ; A temporary string ; holds sum of all numbers present in the string ; read each character in input string ; if current character is a digit ; if current character is an alphabet ; increment sum by number found earlier ( if any ) ; reset temporary string to empty ; atoi ( temp . c_str ( ) ) takes care of trailing numbers ; Driver code ; input alphanumeric string ; Function call
class GFG { static int findSum ( String str ) { String temp = "0" ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( Character . isDigit ( ch ) ) temp += ch ; else { sum += Integer . parseInt ( temp ) ; temp = "0" ; } } return sum + Integer . parseInt ( temp ) ; } public static void main ( String [ ] args ) { String str = "12abc20yz68" ; System . out . println ( findSum ( str ) ) ; } }
Count number of substrings with exactly k distinct characters | Java program to CountKSubStr number of substrings with exactly distinct characters in a given string ; Function to count number of substrings with exactly k unique characters ; Initialize result ; To store count of characters from ' a ' to ' z ' ; Consider all substrings beginning with str [ i ] ; Initializing count array with 0 ; Consider all substrings between str [ i . . j ] ; If this is a new character for this substring , increment dist_count . ; Increment count of current character ; If distinct character count becomes k , then increment result . ; Driver Program
import java . util . Arrays ; public class CountKSubStr { int countkDist ( String str , int k ) { int res = 0 ; int n = str . length ( ) ; int cnt [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int dist_count = 0 ; Arrays . fill ( cnt , 0 ) ; for ( int j = i ; j < n ; j ++ ) { if ( cnt [ str . charAt ( j ) - ' a ' ] == 0 ) dist_count ++ ; cnt [ str . charAt ( j ) - ' a ' ] ++ ; if ( dist_count == k ) res ++ ; } } return res ; } public static void main ( String [ ] args ) { CountKSubStr ob = new CountKSubStr ( ) ; String ch = " abcbaa " ; int k = 3 ; System . out . println ( " Total ▁ substrings ▁ with ▁ exactly ▁ " + k + " ▁ distinct ▁ characters ▁ : ▁ " + ob . countkDist ( ch , k ) ) ; } }
Lower case to upper case | Java program to convert a string to uppercase ; Converts a string to uppercase ; Driver code
class GFG { static String to_upper ( char [ ] in ) { for ( int i = 0 ; i < in . length ; i ++ ) { if ( ' a ' <= in [ i ] & in [ i ] <= ' z ' ) { in [ i ] = ( char ) ( in [ i ] - ' a ' + ' A ' ) ; } } return String . valueOf ( in ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( to_upper ( str . toCharArray ( ) ) ) ; } }
Longest Common Prefix using Character by Character Matching | A Java Program to find the longest common prefix ; A Function to find the string having the minimum length and returns that length ; A Function that returns the longest common prefix from the array of strings ; Our resultant string char current ; The current character ; Current character ( must be same in all strings to be a part of result ) ; Append to result ; Driver program to test above function
class GFG { static int findMinLength ( String arr [ ] , int n ) { int min = arr [ 0 ] . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] . length ( ) < min ) { min = arr [ i ] . length ( ) ; } } return ( min ) ; } static String commonPrefix ( String arr [ ] , int n ) { int minlen = findMinLength ( arr , n ) ; String result = " " ; for ( int i = 0 ; i < minlen ; i ++ ) { current = arr [ 0 ] . charAt ( i ) ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] . charAt ( i ) != current ) { return result ; } } result += ( current ) ; } return ( result ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) > 0 ) { System . out . println ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + ans ) ; } else { System . out . println ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; } } }
Print Concatenation of Zig | Java program to print string obtained by concatenation of different rows of Zig - Zag fashion ; Prints concatenation of all rows of str 's  Zig-Zag fashion ; Corner Case ( Only one row ) ; Find length of string ; Create an array of strings for all n rows ; Initialize index for array of strings arr [ ] ; True if we are moving down in rows , else false ; Traverse through given string ; append current character to current row ; If last row is reached , change direction to ' up ' ; If 1 st row is reached , change direction to ' down ' ; If direction is down , increment , else decrement ; Print concatenation of all rows ; Driver Code
import java . util . Arrays ; class GFG { static void printZigZagConcat ( String str , int n ) { if ( n == 1 ) { System . out . print ( str ) ; return ; } char [ ] str1 = str . toCharArray ( ) ; int len = str . length ( ) ; String [ ] arr = new String [ n ] ; Arrays . fill ( arr , " " ) ; int row = 0 ; boolean down = true ; for ( int i = 0 ; i < len ; ++ i ) { arr [ row ] += ( str1 [ i ] ) ; if ( row == n - 1 ) { down = false ; } else if ( row == 0 ) { down = true ; } if ( down ) { row ++ ; } else { row -- ; } } for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] ) ; } } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; int n = 3 ; printZigZagConcat ( str , n ) ; } }
Check if two given strings are isomorphic to each other | Java program to check if two strings are isomorphic ; Function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one corresponance ; To mark visited characters in str2 ; To store mapping of every character from str1 to that of str2 . Initialize all entries of map as - 1. ; Process all characters one by on ; If current character of str1 is seen first time in it . ; If current character of str2 is already seen , one to one mapping not possible ; Mark current character of str2 as visited ; Store mapping of current characters ; If this is not first appearance of current character in str1 , then check if previous appearance mapped to same character of str2 ; driver program
import java . io . * ; import java . util . * ; class Isomorphic { static int size = 256 ; static boolean areIsomorphic ( String str1 , String str2 ) { int m = str1 . length ( ) ; int n = str2 . length ( ) ; if ( m != n ) return false ; Boolean [ ] marked = new Boolean [ size ] ; Arrays . fill ( marked , Boolean . FALSE ) ; int [ ] map = new int [ size ] ; Arrays . fill ( map , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map [ str1 . charAt ( i ) ] == - 1 ) { if ( marked [ str2 . charAt ( i ) ] == true ) return false ; marked [ str2 . charAt ( i ) ] = true ; map [ str1 . charAt ( i ) ] = str2 . charAt ( i ) ; } else if ( map [ str1 . charAt ( i ) ] != str2 . charAt ( i ) ) return false ; } return true ; } public static void main ( String [ ] args ) { boolean res = areIsomorphic ( " aab " , " xxy " ) ; System . out . println ( res ) ; res = areIsomorphic ( " aab " , " xyz " ) ; System . out . println ( res ) ; } }
Check if two given strings are isomorphic to each other | Java program for the above approach ; This function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one correspondence ; For counting the previous appearances of character in both the strings ; Process all characters one by one ; For string to be isomorphic the previous counts of appearances of current character in both string must be same if it is not same we return false . ; Driver Code
import java . io . * ; class GFG { static final int CHAR = 26 ; static boolean isoMorphic ( String str1 , String str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; if ( n != m ) return false ; int [ ] countChars1 = new int [ CHAR ] ; int [ ] countChars2 = new int [ CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { countChars1 [ str1 . charAt ( i ) - ' a ' ] ++ ; countChars2 [ str2 . charAt ( i ) - ' a ' ] ++ ; if ( countChars1 [ str1 . charAt ( i ) - ' a ' ] != countChars2 [ str2 . charAt ( i ) - ' a ' ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { System . out . println ( isoMorphic ( " aab " , " xxy " ) ? 1 : 0 ) ; System . out . println ( isoMorphic ( " aab " , " xyz " ) ? 1 : 0 ) ; } }
Minimum insertions to form shortest palindrome | Java program to find minimum number of insertions on left side to form a palindrome . ; Returns true if a string str [ st . . end ] is palindrome ; Returns count of insertions on left side to make str [ ] a palindrome ; Find the largest prefix of given string that is palindrome . ; Characters after the palindromic prefix must be added at the beginning also to make the complete string palindrome ; Driver Code
import java . util . * ; class GFG { static boolean isPalin ( char [ ] str , int st , int end ) { while ( st < end ) { if ( str [ st ] != str [ end ] ) return false ; st ++ ; end -- ; } return true ; } static int findMinInsert ( char [ ] str , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( isPalin ( str , 0 , i ) ) return ( n - i - 1 ) ; } return 0 ; } public static void main ( String [ ] args ) { char [ ] Input = " JAVA " . toCharArray ( ) ; System . out . println ( findMinInsert ( Input , Input . length ) ) ; } }
Remove repeated digits in a given number | Java program to remove repeated digits ; Store first digits as previous digit ; Initialize power ; Iterate through all digits of n , note that the digits are processed from least significant digit to most significant digit . ; Store current digit ; Add the current digit to the beginning of result ; Update previous result and power ; Remove last digit from n ; Driver program
import java . io . * ; class GFG { static long removeRecur ( long n ) { long prev_digit = n % 10 ; long pow = 10 ; long res = prev_digit ; while ( n > 0 ) { long curr_digit = n % 10 ; if ( curr_digit != prev_digit ) { res += curr_digit * pow ; prev_digit = curr_digit ; pow *= 10 ; } n = n / 10 ; } return res ; } public static void main ( String [ ] args ) { long n = 12224 ; System . out . println ( removeRecur ( n ) ) ; } }
Remove " b " and " ac " from a given string | A Java program to remove " b " and ' ac ' from input string ; The main function that removes occurrences of " a " and " bc " in input string ; previous character ; current character ; check if current and next character forms ac ; if current character is b ; if current char is ' c ▁ & & ▁ last ▁ char ▁ in ▁ output ▁ ▁ ▁ is ▁ ' a ' so delete both  ; else copy curr char to output string ; Driver program to check above functions
class GfG { static void stringFilter ( char str [ ] ) { int n = str . length ; int i = - 1 ; int j = 0 ; while ( j < n ) { if ( j < n - 1 && str [ j ] == ' a ' && str [ j + 1 ] == ' c ' ) j += 2 ; else if ( str [ j ] == ' b ' ) j ++ ; else if ( i >= 0 && str [ j ] == ' c ' && str [ i ] == ' a ' ) { i -- ; j ++ ; } else str [ ++ i ] = str [ j ++ ] ; } System . out . println ( new String ( str ) . substring ( 0 , i + 1 ) ) ; } public static void main ( String [ ] args ) { String str1 = " ad " ; stringFilter ( str1 . toCharArray ( ) ) ; String str2 = " acbac " ; stringFilter ( str2 . toCharArray ( ) ) ; String str3 = " aaac " ; stringFilter ( str3 . toCharArray ( ) ) ; String str4 = " react " ; stringFilter ( str4 . toCharArray ( ) ) ; String str5 = " aa " ; stringFilter ( str5 . toCharArray ( ) ) ; String str6 = " ababaac " ; stringFilter ( str6 . toCharArray ( ) ) ; } }
Write your own atoi ( ) | A simple Java program for implementation of atoi ; if whitespaces then ignore . ; sign of number ; checking for valid input ; handling overflow test case ; Driver code ; Function call
class GFG { static int myAtoi ( char [ ] str ) { int sign = 1 , base = 0 , i = 0 ; while ( str [ i ] == ' ▁ ' ) { i ++ ; } if ( str [ i ] == ' - ' str [ i ] == ' + ' ) { sign = 1 - 2 * ( str [ i ++ ] == ' - ' ? 1 : 0 ) ; } while ( i < str . length && str [ i ] >= '0' && str [ i ] <= '9' ) { if ( base > Integer . MAX_VALUE / 10 || ( base == Integer . MAX_VALUE / 10 && str [ i ] - '0' > 7 ) ) { if ( sign == 1 ) return Integer . MAX_VALUE ; else return Integer . MIN_VALUE ; } base = 10 * base + ( str [ i ++ ] - '0' ) ; } return base * sign ; } public static void main ( String [ ] args ) { char str [ ] = " ▁ - 123" . toCharArray ( ) ; int val = myAtoi ( str ) ; System . out . printf ( " % d ▁ " , val ) ; } }
Check whether two strings are anagram of each other | Java program to check if two strings are anagrams of each other ; function to check if two strings are anagrams of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; Driver code ; Function call
class GFG { static int NO_OF_CHARS = 256 ; static boolean areAnagram ( char [ ] str1 , char [ ] str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . length ; i ++ ) { count [ str1 [ i ] - ' a ' ] ++ ; count [ str2 [ i ] - ' a ' ] -- ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) { return false ; } return true ; } public static void main ( String [ ] args ) { char str1 [ ] = " geeksforgeeks " . toCharArray ( ) ; char str2 [ ] = " forgeeksgeeks " . toCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) System . out . print ( " The ▁ two ▁ strings ▁ are ▁ " + " anagram ▁ of ▁ each ▁ other " ) ; else System . out . print ( " The ▁ two ▁ strings ▁ are ▁ " + " not ▁ anagram ▁ of ▁ each ▁ other " ) ; } }
Length of the longest substring without repeating characters | Java program to find the length of the longest substring without repeating characters ; This function returns true if all characters in str [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static Boolean areDistinct ( String str , int i , int j ) { boolean [ ] visited = new boolean [ 26 ] ; for ( int k = i ; k <= j ; k ++ ) { if ( visited [ str . charAt ( k ) - ' a ' ] == true ) return false ; visited [ str . charAt ( k ) - ' a ' ] = true ; } return true ; } public static int longestUniqueSubsttr ( String str ) { int n = str . length ( ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( areDistinct ( str , i , j ) ) res = Math . max ( res , j - i + 1 ) ; return res ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( " The ▁ input ▁ string ▁ is ▁ " + str ) ; int len = longestUniqueSubsttr ( str ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " + " non - repeating ▁ character ▁ " + " substring ▁ is ▁ " + len ) ; } }
Given a string , find its first non | JAVA program to find first non - repeating character ; this function return the index of first non - repeating character if found , or else it returns - 1 ; initializing all elements to - 1 ; sets all repeating characters to - 2 and non - repeating characters contain the index where they occur ; If this character is not - 1 or - 2 then it means that this character occurred only once so find the min index of all characters that occur only once , that 's our first index ; if res remains Integer . MAX_VALUE , it means there are no characters that repeat only once or the string is empty
public class GFG { public static int firstNonRepeating ( String str ) { int [ ] fi = new int [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) fi [ i ] = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( fi [ str . charAt ( i ) ] == - 1 ) { fi [ str . charAt ( i ) ] = i ; } else { fi [ str . charAt ( i ) ] = - 2 ; } } int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( fi [ i ] >= 0 ) res = Math . min ( res , fi [ i ] ) ; } if ( res == Integer . MAX_VALUE ) return - 1 ; else return res ; } public static void main ( String args [ ] ) { String str ; str = " geeksforgeeks " ; int firstIndex = firstNonRepeating ( str ) ; if ( firstIndex == - 1 ) System . out . println ( " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ) ; else System . out . println ( " First ▁ non - repeating ▁ character ▁ is ▁ " + str . charAt ( firstIndex ) ) ; } }
Remove duplicates from a given string | Java program to create a unique string ; Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; if c is present in str , it returns the index of c , else it returns - 1 ; adding c to str if - 1 is returned ; Driver code ; Input string with repeating chars
import java . util . * ; class IndexOf { public static String unique ( String s ) { String str = new String ( ) ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = s . charAt ( i ) ; if ( str . indexOf ( c ) < 0 ) { str += c ; } } return str ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( unique ( s ) ) ; } }
Sum of all subsets whose sum is a Perfect Number from a given array | Java program for the above approach ; Function to check is a given number is a perfect number or not ; Stores the sum of its divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find sum of all subsets from an array whose sum is a perfect number ; Print the current subset sum if it is a perfect number ; Check if sum is a perfect number or not ; Calculate sum of the subset including arr [ l ] ; Calculate sum of the subset excluding arr [ l ] ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int isPerfect ( int x ) { int sum_div = 1 ; for ( int i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } static void subsetSum ( int [ ] arr , int l , int r , int sum ) { if ( l > r ) { if ( isPerfect ( sum ) != 0 ) { System . out . print ( sum + " ▁ " ) ; } return ; } subsetSum ( arr , l + 1 , r , sum + arr [ l ] ) ; subsetSum ( arr , l + 1 , r , sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 6 } ; int N = arr . length ; subsetSum ( arr , 0 , N - 1 , 0 ) ; } }
Print all possible ways to split an array into K subsets | Java program for above approach ; Utility function to find all possible ways to split array into K subsets ; If count of elements in K subsets are greater than or equal to N ; If count of subsets formed is equal to K ; Print K subsets by splitting array into K subsets ; Print current subset ; If current element is the last element of the subset ; Otherwise ; If any subset is occupied , then push the element in that first ; Recursively do the same for remaining elements ; Backtrack ; Otherwise , push it in an empty subset and increase the subset count by 1 ; Break to avoid the case of going in other empty subsets , if available , and forming the same combination ; Function to to find all possible ways to split array into K subsets ; Stores K subset by splitting array into K subsets ; Size of each subset must be less than the number of elements ; Driver function ; Given array ; Given K ; Size of the array ; Prints all possible splits into subsets
import java . util . * ; import java . lang . * ; class Gfg { static void PartitionSub ( int arr [ ] , int i , int N , int K , int nos , ArrayList < ArrayList < Integer > > v ) { if ( i >= N ) { if ( nos == K ) { for ( int x = 0 ; x < v . size ( ) ; x ++ ) { System . out . print ( " { ▁ " ) ; for ( int y = 0 ; y < v . get ( x ) . size ( ) ; y ++ ) { System . out . print ( v . get ( x ) . get ( y ) ) ; if ( y == v . get ( x ) . size ( ) - 1 ) { System . out . print ( " ▁ " ) ; } else { System . out . print ( " , ▁ " ) ; } } if ( x == v . size ( ) - 1 ) { System . out . print ( " } " ) ; } else { System . out . print ( " } , ▁ " ) ; } } System . out . println ( ) ; ; } return ; } for ( int j = 0 ; j < K ; j ++ ) { if ( v . get ( j ) . size ( ) > 0 ) { v . get ( j ) . add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos , v ) ; v . get ( j ) . remove ( v . get ( j ) . size ( ) - 1 ) ; } else { v . get ( j ) . add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos + 1 , v ) ; v . get ( j ) . remove ( v . get ( j ) . size ( ) - 1 ) ; break ; } } } static void partKSubsets ( int arr [ ] , int N , int K ) { ArrayList < ArrayList < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < K ; i ++ ) v . add ( new ArrayList < > ( ) ) ; if ( K == 0 K > N ) { System . out . println ( " Not ▁ Possible " ) ; } else { System . out . println ( " The ▁ Subset ▁ Combinations ▁ are : ▁ " ) ; PartitionSub ( arr , 0 , N , K , 0 , v ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . length ; partKSubsets ( arr , N , K ) ; } }