codeparrot/xlcost-text-to-code huggingface dataset:
Find one extra character in a string | Java program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; Driver code ; given string
import java . io . * ; class GFG { static char findExtraCharcter ( String strA , String strB ) { int res = 0 , i ; for ( i = 0 ; i < strA . length ( ) ; i ++ ) { res ^= strA . charAt ( i ) ; } for ( i = 0 ; i < strB . length ( ) ; i ++ ) { res ^= strB . charAt ( i ) ; } return ( ( char ) ( res ) ) ; } public static void main ( String args [ ] ) { String strA = " abcd " ; String strB = " cbdad " ; System . out . println ( findExtraCharcter ( strA , strB ) ) ; } }
Find one extra character in a string | Java program to find extra character in one string ; Determine String with extra character . ; Add character codes of both the strings ; Add last character code of large String . ; Minus the character code of smaller string from the character code of large string . The result will be the extra character code . ; Driver code
public class Test { private static char findExtraCharacter ( String s1 , String s2 ) { String smallStr ; String largeStr ; if ( s1 . length ( ) > s2 . length ( ) ) { smallStr = s2 ; largeStr = s1 ; } else { smallStr = s1 ; largeStr = s2 ; } int smallStrCodeTotal = 0 ; int largeStrCodeTotal = 0 ; int i = 0 ; for ( ; i < smallStr . length ( ) ; i ++ ) { smallStrCodeTotal += smallStr . charAt ( i ) ; largeStrCodeTotal += largeStr . charAt ( i ) ; } largeStrCodeTotal += largeStr . charAt ( i ) ; int intChar = largeStrCodeTotal - smallStrCodeTotal ; return ( char ) intChar ; } public static void main ( String [ ] args ) { String s1 = " abcd " ; String s2 = " cbdae " ; char extraChar = findExtraCharacter ( s1 , s2 ) ; System . out . println ( " Extra ▁ character : ▁ " + extraChar ) ; } }
Function to copy string ( Iterative and Recursive ) | Iterative Java Program to copy one String to another . ; Function to copy one string to other assuming that other string has enough space . ; Driver code
class GFG { static void myCopy ( char s1 [ ] , char s2 [ ] ) { int i = 0 ; for ( i = 0 ; i < s1 . length ; i ++ ) s2 [ i ] = s1 [ i ] ; } public static void main ( String [ ] args ) { char s1 [ ] = " GEEKSFORGEEKS " . toCharArray ( ) ; char s2 [ ] = new char [ s1 . length ] ; myCopy ( s1 , s2 ) ; System . out . println ( String . valueOf ( s2 ) ) ; } }
Evaluate an array expression with numbers , + and | Java program to find sum of given array of string type in integer form ; Function to find the sum of given array ; if string is empty ; parseInt function to convert string into integer ; parseInt function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code
import java . io . * ; class GFG { public static int calculateSum ( String arr [ ] , int n ) { if ( n == 0 ) return 0 ; String s = arr [ 0 ] ; int value = Integer . parseInt ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = Integer . parseInt ( s ) ; char operation = arr [ i - 1 ] . charAt ( 0 ) ; if ( operation == ' + ' ) sum += value ; else sum -= value ; } return sum ; } public static void main ( String [ ] args ) { String arr [ ] = { "3" , " + " , "4" , " - " , "7" , " + " , "13" } ; int n = arr . length ; System . out . println ( calculateSum ( arr , n ) ) ; } }
Check if two strings have a common substring | Java program to check if two strings have common substring ; function to return true if strings have common substring and no if strings have no common substring ; vector for storing character occurrences ; increment vector index for every character of str1 ; checking common substring of str2 in str1 ; Driver code
import java . util . Arrays ; class GFG { static int MAX_CHAR = 26 ; static boolean twoStrings ( String s1 , String s2 ) { boolean v [ ] = new boolean [ MAX_CHAR ] ; Arrays . fill ( v , false ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) v [ s1 . charAt ( i ) - ' a ' ] = true ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) if ( v [ s2 . charAt ( i ) - ' a ' ] ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = " hello " ; String str2 = " world " ; if ( twoStrings ( str1 , str2 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
String with maximum number of unique characters | Java code to find the largest string ; Function to find string with maximum number of unique characters . ; Index of string with maximum unique characters ; iterate through all strings ; array indicating any alphabet included or not included ; count number of unique alphabets in each string ; keep track of maximum number of alphabets ; print result ; Driver code
import java . lang . * ; import java . io . * ; import java . util . Arrays ; class Geek { public static void LargestString ( String na [ ] ) { int N = na . length ; int c [ ] = new int [ N ] ; int m = 0 ; for ( int j = 0 ; j < N ; j ++ ) { boolean character [ ] = new boolean [ 26 ] ; for ( int k = 0 ; k < na [ j ] . length ( ) ; k ++ ) { int x = na [ j ] . charAt ( k ) - ' A ' ; if ( ( na [ j ] . charAt ( k ) != ' ▁ ' ) && ( character [ x ] == false ) ) { c [ j ] ++ ; character [ x ] = true ; } } if ( c [ j ] > c [ m ] ) m = j ; } System . out . println ( na [ m ] ) ; } public static void main ( String [ ] args ) { String na [ ] = { " BOB " , " A ▁ AB ▁ C ▁ JOHNSON " , " ANJALI " , " ASKRIT " , " ARMAN ▁ MALLIK " } ; LargestString ( na ) ; } }
Program for length of the longest word in a sentence | Java program to find the number of charters in the longest word in the sentence . ; Driver code
class GFG { static int LongestWordLength ( String str ) { String [ ] words = str . split ( " ▁ " ) ; int length = 0 ; for ( String word : words ) { if ( length < word . length ( ) ) { length = word . length ( ) ; } } return length ; } public static void main ( String args [ ] ) { String str = " I ▁ am ▁ an ▁ intern ▁ at ▁ geeksforgeeks " ; System . out . println ( LongestWordLength ( str ) ) ; } }
Morse Code Implementation | Java program to demonstrate Morse code ; function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; character by character print Morse code ; Driver code
class GFG { static String morseEncode ( char x ) { switch ( x ) { case ' a ' : return " . - " ; case ' b ' : return " - . . . " ; case ' c ' : return " - . - . " ; case ' d ' : return " - . . " ; case ' e ' : return " . " ; case ' f ' : return " . . - . " ; case ' g ' : return " - - . " ; case ' h ' : return " . . . . " ; case ' i ' : return " . . " ; case ' j ' : return " . - - - " ; case ' k ' : return " - . - " ; case ' l ' : return " . - . . " ; case ' m ' : return " - - " ; case ' n ' : return " - . " ; case ' o ' : return " - - - " ; case ' p ' : return " . - - . " ; case ' q ' : return " - - . - " ; case ' r ' : return " . - . " ; case ' s ' : return " . . . " ; case ' t ' : return " - " ; case ' u ' : return " . . - " ; case ' v ' : return " . . . - " ; case ' w ' : return " . - - " ; case ' x ' : return " - . . - " ; case ' y ' : return " - . - - " ; case ' z ' : return " - - . . " ; case '1' : return " . - - - - " ; case '2' : return " . . - - - " ; case '3' : return " . . . - - " ; case '4' : return " . . . . - " ; case '5' : return " . . . . . " ; case '6' : return " - . . . . " ; case '7' : return " - - . . . " ; case '8' : return " - - - . . " ; case '9' : return " - - - - . " ; case '0' : return " - - - - - " ; } return " " ; } static void morseCode ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) System . out . print ( morseEncode ( s . charAt ( i ) ) ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; morseCode ( s ) ; } }
Polybius Square Cipher | Java Program to implement polybius cipher ; Function to display polybius cipher text ; convert each character to its encrypted code ; finding row of the table ; finding column of the table ; if character is ' k ' ; if character is greater than ' j ' ; Driver code ; print the cipher of "geeksforgeeks
class GFG { static void polybiusCipher ( String s ) { int row , col ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { row = ( int ) Math . ceil ( ( s . charAt ( i ) - ' a ' ) / 5 ) + 1 ; col = ( ( s . charAt ( i ) - ' a ' ) % 5 ) + 1 ; if ( s . charAt ( i ) == ' k ' ) { row = row - 1 ; col = 5 - col + 1 ; } else if ( s . charAt ( i ) >= ' j ' ) { if ( col == 1 ) { col = 6 ; row = row - 1 ; } col = col - 1 ; } System . out . print ( row + " " + col ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; polybiusCipher ( s ) ; } }
Minimum removal to make palindrome permutation | Java Program to find minimum number of removal to make any permutation of the string a palindrome ; function to find minimum removal of characters ; hash to store frequency of each character ; to set hash array to zeros ; count frequency of each character ; count the odd frequency characters ; if count is - 1 return 0 otherwise return count ; Driver code
import java . util . Arrays ; class GFG { static final int MAX_CHAR = 26 ; static int minRemoval ( String str ) { int hash [ ] = new int [ MAX_CHAR ] ; Arrays . fill ( hash , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) hash [ str . charAt ( i ) - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( hash [ i ] % 2 == 1 ) count ++ ; return ( count == 0 ) ? 0 : count - 1 ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( minRemoval ( str ) ) ; } }
Union | Java program to implement Union - Find with union by rank and path compression ; Arr to represent parent of index i ; Size to represent the number of nodes in subgxrph rooted at index i ; set parent of every node to itself and size of node to one ; Each time we follow a path , find function compresses it further until the path length is greater than or equal to 1. ; while we reach a node whose parent is equal to itself ; Skip one level ; Move to the new level ; A function that does union of two nodes x and y where xr is root node of x and yr is root node of y ; Make yr parent of xr ; Make xr parent of yr ; The main function to check whether a given gxrph contains cycle or not ; Itexrte through all edges of gxrph , find nodes connecting them . If root nodes of both are same , then there is cycle in gxrph . ; find root of i ; find root of adj [ i ] [ j ] ; If same parent ; Make them connect ; Driver Code ; Initialize the values for arxry Arr and Size ; Adjacency list for graph ; call is_cycle to check if it contains cycle
import java . util . * ; class GFG { static int MAX_VERTEX = 101 ; static int [ ] Arr = new int [ MAX_VERTEX ] ; static int [ ] size = new int [ MAX_VERTEX ] ; static void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { Arr [ i ] = i ; size [ i ] = 1 ; } } static int find ( int i ) { while ( Arr [ i ] != i ) { Arr [ i ] = Arr [ Arr [ i ] ] ; i = Arr [ i ] ; } return i ; } static void _union ( int xr , int yr ) { if ( size [ xr ] < size [ yr ] ) { Arr [ xr ] = Arr [ yr ] ; size [ yr ] += size [ xr ] ; } else { Arr [ yr ] = Arr [ xr ] ; size [ xr ] += size [ yr ] ; } } static int isCycle ( Vector < Integer > adj [ ] , int V ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < adj [ i ] . size ( ) ; j ++ ) { int x = find ( i ) ; int y = find ( adj [ i ] . get ( j ) ) ; if ( x == y ) return 1 ; _union ( x , y ) ; } } return 0 ; } public static void main ( String [ ] args ) { int V = 3 ; initialize ( V ) ; Vector < Integer > [ ] adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 0 ] . add ( 1 ) ; adj [ 0 ] . add ( 2 ) ; adj [ 1 ] . add ( 2 ) ; if ( isCycle ( adj , V ) == 1 ) System . out . print ( "Graph contains Cycle.NEW_LINE"); else System . out . print ( "Graph does not contain Cycle.NEW_LINE"); } }
Queries for frequencies of characters in substrings | Java program to find occurrence of character in substring l to r ; To store count of all character ; To pre - process string from 0 to size of string ; Store occurrence of character i ; Store occurrence o all character upto i ; To return occurrence of character in range l to r ; Return occurrence of character from 0 to r minus its occurrence from 0 to l ; Driver Code
import java . util . * ; class GFG { static int MAX_LEN = 1005 ; static int MAX_CHAR = 26 ; static int [ ] [ ] cnt = new int [ MAX_LEN ] [ MAX_CHAR ] ; static void preProcess ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ i ] [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) cnt [ i ] [ j ] += cnt [ i - 1 ] [ j ] ; } } static int findCharFreq ( int l , int r , char c ) { return ( cnt [ r ] [ ( c ) - 97 ] - cnt [ l ] [ ( c ) - 97 ] ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int Q = 4 ; preProcess ( s ) ; System . out . println ( findCharFreq ( 0 , 5 , ' e ' ) ) ; System . out . println ( findCharFreq ( 2 , 6 , ' f ' ) ) ; System . out . println ( findCharFreq ( 4 , 7 , ' m ' ) ) ; System . out . println ( findCharFreq ( 0 , 12 , ' e ' ) ) ; } }
Longest Uncommon Subsequence | Java program to find longest uncommon subsequence using naive method ; function to calculate length of longest uncommon subsequence ; Case 1 : If strings are equal ; for case 2 and case 3 ; Driver code ; input strings
import java . io . * ; import java . util . * ; class GfG { static int findLUSlength ( String a , String b ) { if ( a . equals ( b ) == true ) return 0 ; return Math . max ( a . length ( ) , b . length ( ) ) ; } public static void main ( String [ ] args ) { String a = " abcdabcd " , b = " abcabc " ; System . out . println ( findLUSlength ( a , b ) ) ; } }
Interchanging first and second halves of strings | Java code to create two new strings by swapping first and second halves ; Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . Please refer below for details of substring . https : www . geeksforgeeks . org / java - lang - string - substring - java / ; Driver function ; Calling function
import java . io . * ; class ns { public static void swapTwoHalves ( String a , String b ) { int la = a . length ( ) ; int lb = b . length ( ) ; String c = a . substring ( 0 , la / 2 ) + b . substring ( lb / 2 , lb ) ; String d = b . substring ( 0 , lb / 2 ) + a . substring ( la / 2 , la ) ; System . out . println ( c + "NEW_LINE" + d); } public static void main ( String args [ ] ) { String a = " remuneration " ; String b = " day " ; swapTwoHalves ( a , b ) ; } }
Merge two strings in chunks of given size | Java program to merge n number of strings ; Function performing calculations ; Length of string a ; Length f string b ; Pointers for string a and string b ; pa and pb denote the number of characters of both a and b extracted ; If entire substring of length k can be extracted ;  ; If the remaining string is of length less than k ; If the string has been traversed ; If entire substring of length k can be extracted ; If the remaining string is of length less than k ; If the string has been traversed ; Driver function
import java . io . * ; class msc { public static void solve ( String a , String b , int k ) { String s = " " ; int la = a . length ( ) ; int lb = b . length ( ) ; int l = la + lb ; int indexa = 0 , indexb = 0 ; while ( l > 0 ) { int pa = 0 , pb = 0 ; if ( la - indexa >= k ) { s = s + a . substring ( indexa , indexa + k ) ; indexa = indexa + k ; pa = k ; } ''		Please refer below link for details of library function https : www . geeksforgeeks . org / stdsubstr - in - ccpp / ''' else if ( la - indexa < k && la - indexa > 0 ) { s = s + a . substring ( indexa , la ) ; pa = la - indexa ; indexa = la ; } else if ( indexa >= la ) pa = 0 ; if ( lb - indexb >= k ) { s = s + b . substring ( indexb , indexb + k ) ; pb = k ; indexb = indexb + k ; } else if ( lb - indexb < k && lb - indexb > 0 ) { s = s + b . substring ( indexb , lb ) ; pb = lb - indexb ; indexb = lb ; } else if ( indexb >= lb ) pb = 0 ; l = l - pa - pb ; } System . out . println ( s ) ; } public static void main ( String args [ ] ) throws IOException { String a = " determination " , b = " stance " ; int k = 3 ; solve ( a , b , k ) ; } }
Longest sub | Java implementation to find the length of the longest substring having frequency of each character less than equal to k ; function to find the length of the longest substring having frequency of each character less than equal to k ; hash table to store frequency of each table ; ' start ' index of the current substring ; to store the maximum length ; traverse the string ' str ' ; get the current character as ' ch ' ; increase frequency of ' ch ' in ' freq [ ] ' ; if frequency of ' ch ' becomes more than ' k ' ; update ' maxLen ' ; decrease frequency of each character as they are encountered from the ' start ' index until frequency of ' ch ' is greater than ' k ' ; decrement frequency by '1' ; increment ' start ' ; update maxLen ; required length ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public final static int SIZE = 26 ; public static int longSubstring ( String str1 , int k ) { int [ ] freq = new int [ SIZE ] ; char [ ] str = str1 . toCharArray ( ) ; int start = 0 ; int maxLen = 0 ; char ch ; int n = str1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ch = str [ i ] ; freq [ ch - ' a ' ] ++ ; if ( freq [ ch - ' a ' ] > k ) { if ( maxLen < ( i - start ) ) maxLen = i - start ; while ( freq [ ch - ' a ' ] > k ) { freq [ str [ start ] - ' a ' ] -- ; start ++ ; } } } if ( maxLen < ( n - start ) ) maxLen = n - start ; return maxLen ; } public static void main ( String argc [ ] ) { String str = " babcaag " ; int k = 1 ; System . out . println ( " Length ▁ = ▁ " + longSubstring ( str , k ) ) ; } }
Magical Indices in an array | Java program to find number of magical indices in the given array . ; Function to count number of magical indices . ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
import java . io . * ; import java . util . * ; public class GFG { static int solve ( int [ ] A , int n ) { int i , cnt = 0 , j ; int [ ] parent = new int [ n + 1 ] ; int [ ] vis = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { parent [ i ] = - 1 ; vis [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == - 1 ) { while ( parent [ j ] == - 1 ) { parent [ j ] = i ; j = ( j + A [ j ] + 1 ) % n ; } if ( parent [ j ] == i ) { while ( vis [ j ] == 0 ) { vis [ j ] = 1 ; cnt ++ ; j = ( j + A [ j ] + 1 ) % n ; } } } } return cnt ; } public static void main ( String args [ ] ) { int [ ] A = { 0 , 0 , 0 , 2 } ; int n = A . length ; System . out . print ( solve ( A , n ) ) ; } }
Minimum cost to connect all cities | Java code to find out minimum cost path to connect all the cities ; Function to find out minimum valued node among the nodes which are not yet included in MST ; Loop through all the values of the nodes which are not yet included in MST and find the minimum valued one . ; Function to find out the MST and the cost of the MST . ; Array to store the parent node of a particular node . ; Array to store key value of each node . ; Boolean Array to hold bool values whether a node is included in MST or not . ; Set all the key values to infinite and none of the nodes is included in MST . ; Start to find the MST from node 0. Parent of node 0 is none so set - 1. key value or minimum cost to reach 0 th node from 0 th node is 0. ; Find the rest n - 1 nodes of MST . ; First find out the minimum node among the nodes which are not yet included in MST . ; Now the uth node is included in MST . ; Update the values of neighbor nodes of u which are not yet included in MST . ; Find out the cost by adding the edge values of MST . ; Driver code ; Input 1 ; Input 2
import java . util . * ; class GFG { static int minnode ( int n , int keyval [ ] , boolean mstset [ ] ) { int mini = Integer . MAX_VALUE ; int mini_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mstset [ i ] == false && keyval [ i ] < mini ) { mini = keyval [ i ] ; mini_index = i ; } } return mini_index ; } static void findcost ( int n , int city [ ] [ ] ) { int parent [ ] = new int [ n ] ; int keyval [ ] = new int [ n ] ; boolean mstset [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { keyval [ i ] = Integer . MAX_VALUE ; mstset [ i ] = false ; } parent [ 0 ] = - 1 ; keyval [ 0 ] = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = minnode ( n , keyval , mstset ) ; mstset [ u ] = true ; for ( int v = 0 ; v < n ; v ++ ) { if ( city [ u ] [ v ] > 0 && mstset [ v ] == false && city [ u ] [ v ] < keyval [ v ] ) { keyval [ v ] = city [ u ] [ v ] ; parent [ v ] = u ; } } } int cost = 0 ; for ( int i = 1 ; i < n ; i ++ ) cost += city [ parent [ i ] ] [ i ] ; System . out . println ( cost ) ; } public static void main ( String args [ ] ) { int n1 = 5 ; int city1 [ ] [ ] = { { 0 , 1 , 2 , 3 , 4 } , { 1 , 0 , 5 , 0 , 7 } , { 2 , 5 , 0 , 6 , 0 } , { 3 , 0 , 6 , 0 , 0 } , { 4 , 7 , 0 , 0 , 0 } } ; findcost ( n1 , city1 ) ; int n2 = 6 ; int city2 [ ] [ ] = { { 0 , 1 , 1 , 100 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 100 , 0 , 0 , 0 , 2 , 2 } , { 0 , 0 , 0 , 2 , 0 , 2 } , { 0 , 0 , 0 , 2 , 2 , 0 } } ; findcost ( n2 , city2 ) ; } }