codeparrot/xlcost-text-to-code huggingface dataset:
Find if an array contains a string with one mismatch | Java program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code
import java . util . * ; class GFG { static boolean check ( Vector < String > list , String s ) { int n = ( int ) list . size ( ) ; if ( n == 0 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( list . get ( i ) . length ( ) != s . length ( ) ) { continue ; } boolean diff = false ; for ( int j = 0 ; j < ( int ) list . get ( i ) . length ( ) ; j ++ ) { if ( list . get ( i ) . charAt ( j ) != s . charAt ( j ) ) { if ( ! diff ) { diff = true ; } else { diff = false ; break ; } } } if ( diff ) { return true ; } } return false ; } public static void main ( String [ ] args ) { Vector < String > s = new Vector < > ( ) ; s . add ( " bana " ) ; s . add ( " apple " ) ; s . add ( " banacb " ) ; s . add ( " bonanza " ) ; s . add ( " banamf " ) ; System . out . println ( check ( s , " banana " ) == true ? 1 : 0 ) ; } }
Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | Java program to find if a sentence is palindrome ; To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver program to test sentencePallindrome ( )
public class GFG { static boolean sentencePalindrome ( String str ) { int l = 0 ; int h = str . length ( ) - 1 ; str = str . toLowerCase ( ) ; while ( l <= h ) { char getAtl = str . charAt ( l ) ; char getAth = str . charAt ( h ) ; if ( ! ( getAtl >= ' a ' && getAtl <= ' z ' ) ) l ++ ; else if ( ! ( getAth >= ' a ' && getAth <= ' z ' ) ) h -- ; else if ( getAtl == getAth ) { l ++ ; h -- ; } else return false ; } return true ; } public static void main ( String [ ] args ) { String str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( str ) ) System . out . println ( " Sentence ▁ is ▁ palindrome " ) ; else System . out . println ( " Sentence ▁ is ▁ not " + " ▁ " + " palindrome " ) ; } }
Ways to remove one element from a binary string so that XOR becomes zero | Java program to count number of ways to remove an element so that XOR of remaining string becomes 0. ; Returns number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code to test above function
import java . util . * ; class CountWays { static int xorZero ( String s ) { int one_count = 0 , zero_count = 0 ; char [ ] str = s . toCharArray ( ) ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } public static void main ( String [ ] args ) { String s = "11111" ; System . out . println ( xorZero ( s ) ) ; } }
Keyword Cipher | Java program for encoding the string using classical cipher ; Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Driver code ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text
class GFG { static String encoder ( char [ ] key ) { String encoded = " " ; boolean [ ] arr = new boolean [ 26 ] ; for ( int i = 0 ; i < key . length ; i ++ ) { if ( key [ i ] >= ' A ' && key [ i ] <= ' Z ' ) { if ( arr [ key [ i ] - 65 ] == false ) { encoded += ( char ) key [ i ] ; arr [ key [ i ] - 65 ] = true ; } } else if ( key [ i ] >= ' a ' && key [ i ] <= ' z ' ) { if ( arr [ key [ i ] - 97 ] == false ) { encoded += ( char ) ( key [ i ] - 32 ) ; arr [ key [ i ] - 97 ] = true ; } } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == false ) { arr [ i ] = true ; encoded += ( char ) ( i + 65 ) ; } } return encoded ; } static String cipheredIt ( String msg , String encoded ) { String cipher = " " ; for ( int i = 0 ; i < msg . length ( ) ; i ++ ) { if ( msg . charAt ( i ) >= ' a ' && msg . charAt ( i ) <= ' z ' ) { int pos = msg . charAt ( i ) - 97 ; cipher += encoded . charAt ( pos ) ; } else if ( msg . charAt ( i ) >= ' A ' && msg . charAt ( i ) <= ' Z ' ) { int pos = msg . charAt ( i ) - 65 ; cipher += encoded . charAt ( pos ) ; } else { cipher += msg . charAt ( i ) ; } } return cipher ; } public static void main ( String [ ] args ) { String key ; key = " Computer " ; System . out . println ( " Keyword ▁ : ▁ " + key ) ; String encoded = encoder ( key . toCharArray ( ) ) ; String message = " GeeksforGeeks " ; System . out . println ( " Message ▁ before ▁ Ciphering ▁ : ▁ " + message ) ; System . out . println ( " Ciphered ▁ Text ▁ : ▁ " + cipheredIt ( message , encoded ) ) ; } }
Check if both halves of the string have same set of characters | Java program to check if it two half of string contain same Character set or not ; function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; Driver program to test above function ; String to be checked
public class GFG { static final int MAX_CHAR = 26 ; static boolean checkCorrectOrNot ( String s ) { int [ ] count = new int [ MAX_CHAR ] ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s . charAt ( i ) - ' a ' ] ++ ; count [ s . charAt ( j ) - ' a ' ] -- ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } public static void main ( String args [ ] ) { String s = " abab " ; if ( checkCorrectOrNot ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if a string is Isogram or not | Java code to check string is isogram or not ; function to check isogram ; loop to store count of chars and check if it is greater than 1 ; if count > 1 , return false ; Driver code ; checking str as isogram ; checking str2 as isogram
class GFG { static boolean check_isogram ( String str ) { int length = str . length ( ) ; int mapHash [ ] = new int [ 26 ] ; for ( int i = 0 ; i < length ; i ++ ) { mapHash [ str . charAt ( i ) - ' a ' ] ++ ; if ( mapHash [ str [ i ] - ' a ' ] > 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " geeks " ; String str2 = " computer " ; if ( check_isogram ( str ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; if ( check_isogram ( str2 ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | Java program to check if a string is valid or not . ; Method returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver method
class Test { static boolean checkString ( String s ) { int len = s . length ( ) ; int first = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . charAt ( i ) == '1' ) { first = i ; break ; } } int last = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) { last = i ; break ; } } for ( int i = first ; i <= last ; i ++ ) if ( s . charAt ( i ) == '0' ) return false ; return true ; } public static void main ( String args [ ] ) { String s = "00011111111100000" ; System . out . println ( checkString ( s ) ? " VALID " : " NOT ▁ VALID " ) ; } }
Program to print all substrings of a given string | Java program to print all possible subStrings of a given String without checking for duplication . ; Function to print all ( n * ( n + 1 ) ) / 2 subStrings of a given String s of length n . ; Fix start index in outer loop . Reveal new character in inner loop till end of String . Print till - now - formed String . ; Driver code
import java . io . * ; class GFG { public static void printAllSubStrings ( String s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { char [ ] temp = new char [ n - i + 1 ] ; int tempindex = 0 ; for ( int j = i ; j < n ; j ++ ) { temp [ tempindex ++ ] = s . charAt ( j ) ; temp [ tempindex ] = ' \0' ; System . out . println ( temp ) ; } } } public static void main ( String [ ] args ) { String s = " Geeky " ; printAllSubStrings ( s , s . length ( ) ) ; } }
Reverse a string preserving space positions | Java program to implement the above approach ; Initialize two pointers as two corners ; Move both pointers toward each other ; If character at start or end is space , ignore it ; If both are not spaces , do swap ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static void preserveSpace ( String str ) { int n = str . length ( ) ; int start = 0 ; int end = n - 1 ; char [ ] Str = str . toCharArray ( ) ; while ( start < end ) { if ( Str [ start ] == ' ▁ ' ) { start ++ ; continue ; } else if ( Str [ end ] == ' ▁ ' ) { end -- ; continue ; } else { char temp = Str [ start ] ; Str [ start ] = Str [ end ] ; Str [ end ] = temp ; start ++ ; end -- ; } } System . out . println ( String . valueOf ( Str ) ) ; } public static void main ( String [ ] args ) { String str = " internship ▁ at ▁ geeks ▁ for ▁ geeks " ; preserveSpace ( str ) ; } }
Put spaces between words starting with capital letters | Java program to put spaces between words starting with capital letters . ; Function to amend the sentence ; Traverse the string ; Convert to lowercase if its an uppercase character ; Print space before it if its an uppercase character ; Print the character ; if lowercase character then just print ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class AddSpaceinSentence { public static void amendSentence ( String sstr ) { char [ ] str = sstr . toCharArray ( ) ; for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { str [ i ] = ( char ) ( str [ i ] + 32 ) ; if ( i != 0 ) System . out . print ( " ▁ " ) ; System . out . print ( str [ i ] ) ; } else System . out . print ( str [ i ] ) ; } } public static void main ( String [ ] args ) { String str = " BruceWayneIsBatman " ; amendSentence ( str ) ; } }
C ++ program to concatenate a string given number of times | Java program to concatenate given string n number of times ; Function which return string by concatenating it . ; Copying given string to temporary string . ; Concatenating strings ; Driver code
class GFG { static String repeat ( String s , int n ) { String s1 = s ; for ( int i = 1 ; i < n ; i ++ ) s += s1 ; return s ; } public static void main ( String [ ] args ) { String s = " geeks " ; int n = 3 ; System . out . println ( repeat ( s , n ) ) ; } }
Number of distinct permutation a String can have | Java program to find number of distinct permutations of a string . ; Utility function to find factorial of n . ; Returns count of distinct permutations of str . ; finding frequency of all the lower case alphabet and storing them in array of integer ; finding factorial of number of appearances and multiplying them since they are repeating alphabets ; finding factorial of size of string and dividing it by factorial found after multiplying ; Driver code
public class GFG { static final int MAX_CHAR = 26 ; static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int countDistinctPermutations ( String str ) { int length = str . length ( ) ; int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < length ; i ++ ) if ( str . charAt ( i ) >= ' a ' ) freq [ str . charAt ( i ) - ' a ' ] ++ ; int fact = 1 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) fact = fact * factorial ( freq [ i ] ) ; return factorial ( length ) / fact ; } public static void main ( String args [ ] ) { String str = " fvvfhvgv " ; System . out . println ( countDistinctPermutations ( str ) ) ; } }
Determine if a string has all Unique Characters | Java program to illustrate String with unique characters without using any data structure ; Assuming string can have characters a - z this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; Driver Code
import java . util . * ; class GfG { boolean uniqueCharacters ( String str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int bitAtIndex = str . charAt ( i ) - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) return false ; checker = checker | ( 1 << bitAtIndex ) ; } return true ; } public static void main ( String args [ ] ) { GfG obj = new GfG ( ) ; String input = " geekforgeeks " ; if ( obj . uniqueCharacters ( input ) ) System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ all ▁ unique ▁ characters " ) ; else System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ duplicate ▁ characters " ) ; } }
Alternate vowel and consonant string | Java implementation of alternate vowel and consonant string ; ' ch ' is vowel or not ; create alternate vowel and consonant string str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required alternate vowel and consonant string ; count vowels and update vowel string ; count consonants and update consonant string ; no such string can be formed ; remove first character of vowel string then create alternate string with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant string then create alternate string with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating string with consonant ; start creating string with vowel ; Driver code
import java . util . * ; class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) finalStr = ( finalStr + str1 . charAt ( i ) ) + str2 . charAt ( j ) ; return finalStr ; } static String findAltStr ( String str ) { int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . charAt ( i ) ; if ( isVowel ( ch ) ) { nv ++ ; vstr = vstr + ch ; } else { nc ++ ; cstr = cstr + ch ; } } if ( Math . abs ( nv - nc ) >= 2 ) return " no ▁ such ▁ string " ; if ( nv > nc ) return ( vstr . charAt ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; if ( nc > nv ) return ( cstr . charAt ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; if ( cstr . charAt ( 0 ) < vstr . charAt ( 0 ) ) return createAltStr ( cstr , vstr , 0 , nv ) ; return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void main ( String args [ ] ) { String str = " geeks " ; System . out . println ( findAltStr ( str ) ) ; } }
Check whether K | Java program to check if k - th bit of a given number is set or not ; driver code
class Number { public static void isKthBitSet ( int n , int k ) { if ( ( n & ( 1 << ( k - 1 ) ) ) > 0 ) System . out . print ( " SET " ) ; else System . out . print ( " NOT ▁ SET " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 1 ; isKthBitSet ( n , k ) ; } }
Reverse string without using any temporary variable | Reversing a string using reverse ( ) ; Reverse str [ beign . . end ]
class GFG { public static void main ( String [ ] args ) { StringBuilder str = new StringBuilder ( " geeksforgeeks " ) ; str . reverse ( ) ; System . out . println ( str ) ; } }
Recursive function to check if a string is palindrome | A recursive JAVA program to check whether a given String is palindrome or not ; A recursive function that check a str ( s . . e ) is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code
import java . io . * ; class GFG { static boolean isPalRec ( String str , int s , int e ) { if ( s == e ) return true ; if ( ( str . charAt ( s ) ) != ( str . charAt ( e ) ) ) return false ; if ( s < e + 1 ) return isPalRec ( str , s + 1 , e - 1 ) ; return true ; } static boolean isPalindrome ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return true ; return isPalRec ( str , 0 , n - 1 ) ; } public static void main ( String args [ ] ) { String str = " geeg " ; if ( isPalindrome ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count substrings with same first and last characters | Most efficient Java program to count all substrings with same first and last characters . ; assuming lower case only ; Calculating frequency of each character in the string . ; Computing result using counts ; Driver function
public class GFG { static final int MAX_CHAR = 26 ; static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }
Maximum consecutive repeating character in string | Java program to find the maximum consecutive repeating character in given string ; function to find out the maximum repeating character in given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code
class GFG { static char maxRepeating ( String str ) { int n = str . length ( ) ; int count = 0 ; char res = str . charAt ( 0 ) ; int cur_count = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res = str . charAt ( i ) ; } cur_count = 1 ; } } return res ; } public static void main ( String args [ ] ) { String str = " aaaabbaaccde " ; System . out . println ( maxRepeating ( str ) ) ; } }
Queries on subsequence of string | Java program to answer subsequence queries for a given string . ; Precompute the position of each character from each position of String S ; Computing position of each character from each position of String S ; Print " Yes " if T is subsequence of S , else " No " ; Traversing the string T ; If next position is greater than length of S set flag to false . ; Setting position of next character ; Driven Program
public class Query_Subsequence { static final int MAX = 10000 ; static final int CHAR_SIZE = 26 ; static void precompute ( int mat [ ] [ ] , String str , int len ) { for ( int i = 0 ; i < CHAR_SIZE ; ++ i ) mat [ len ] [ i ] = len ; for ( int i = len - 1 ; i >= 0 ; -- i ) { for ( int j = 0 ; j < CHAR_SIZE ; ++ j ) mat [ i ] [ j ] = mat [ i + 1 ] [ j ] ; mat [ i ] [ str . charAt ( i ) - ' a ' ] = i ; } } static boolean query ( int mat [ ] [ ] , String str , int len ) { int pos = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( mat [ pos ] [ str . charAt ( i ) - ' a ' ] >= len ) return false ; else pos = mat [ pos ] [ str . charAt ( i ) - ' a ' ] + 1 ; } return true ; } public static void main ( String args [ ] ) { String S = " geeksforgeeks " ; int len = S . length ( ) ; int [ ] [ ] mat = new int [ MAX ] [ CHAR_SIZE ] ; precompute ( mat , S , len ) ; String get = query ( mat , " gg " , len ) ? " Yes " : " No " ; System . out . println ( get ) ; get = query ( mat , " gro " , len ) ? " Yes " : " No " ; System . out . println ( get ) ; get = query ( mat , " gfg " , len ) ? " Yes " : " No " ; System . out . println ( get ) ; get = query ( mat , " orf " , len ) ? " Yes " : " No " ; System . out . println ( get ) ; } }
Queries for characters in a repeated string | Java Program to Queries for same characters in a repeated string ; Print whether index i and j have same element or not ; Finding relative position of index i , j ; Checking is element are same at index i , j ; Driver Code
import java . io . * ; public class GFG { static void query ( String s , int i , int j ) { int n = s . length ( ) ; i %= n ; j %= n ; if ( s . charAt ( i ) == s . charAt ( j ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } static public void main ( String [ ] args ) { String X = " geeksforgeeks " ; query ( X , 0 , 8 ) ; query ( X , 8 , 13 ) ; query ( X , 6 , 15 ) ; } }
Count of character pairs at same distance as in English alphabets | A Simple Java program to find pairs with distance equal to English alphabet distance ; Method to count pairs ; Increment count if characters are at same distance ; Driver method
class Test { static int countPairs ( String str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str . charAt ( i ) - str . charAt ( j ) ) == Math . abs ( i - j ) ) result ++ ; return result ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; System . out . println ( countPairs ( str ) ) ; } }
Longest common subsequence with permutations allowed | Java program to find LCS with permutations allowed ; Function to calculate longest String str1 -- > first String str2 -- > second String count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest String whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; for ( int i = 0 ; i < 26 ; i ++ ) append character ( ' a ' + i ) in resultant String ' result ' by min ( count1 [ i ] , count2i ] ) times ; Driver program to run the case
class GFG { static void longestString ( String str1 , String str2 ) { int count1 [ ] = new int [ 26 ] , count2 [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; } String result = " " ; { for ( int j = 1 ; j <= Math . min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) { result += ( char ) ( ' a ' + i ) ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { String str1 = " geeks " , str2 = " cake " ; longestString ( str1 , str2 ) ; } }