codeparrot/xlcost-text-to-code huggingface dataset:
Number of substrings with count of each character as k |
import java . util . * ; class GFG { static boolean have_same_frequency ( int [ ] freq , int k ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] != k ) { return false ; } } return true ; } static int count_substrings ( String s , int k ) { int count = 0 ; int distinct = 0 ; boolean [ ] have = new boolean [ 26 ] ; Arrays . fill ( have , false ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { have [ ( ( int ) ( s . charAt ( i ) - ' a ' ) ) ] = true ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( have [ i ] ) { distinct ++ ; } } for ( int length = 1 ; length <= distinct ; length ++ ) { int window_length = length * k ; int [ ] freq = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; int window_start = 0 ; int window_end = window_start + window_length - 1 ; for ( int i = window_start ; i <= Math . min ( window_end , s . length ( ) - 1 ) ; i ++ ) { freq [ ( ( int ) ( s . charAt ( i ) - ' a ' ) ) ] ++ ; } while ( window_end < s . length ( ) ) { if ( have_same_frequency ( freq , k ) ) { count ++ ; } freq [ ( ( int ) ( s . charAt ( window_start ) - ' a ' ) ) ] -- ; window_start ++ ; window_end ++ ; if ( window_end < s . length ( ) ) { freq [ ( ( int ) ( s . charAt ( window_end ) - ' a ' ) ) ] ++ ; } } } return count ; } public static void main ( String [ ] args ) { String s = " aabbcc " ; int k = 2 ; System . out . println ( count_substrings ( s , k ) ) ; s = " aabbc " ; k = 2 ; System . out . println ( count_substrings ( s , k ) ) ; } }
Longest subsequence where every character appears at | Java program to Find longest subsequence where every character appears at - least k times ; Count frequencies of all characters ; Traverse given string again and print all those characters whose frequency is more than or equal to k . ; Driver code
class GFG { static final int MAX_CHARS = 26 ; static void longestSubseqWithK ( String str , int k ) { int n = str . length ( ) ; int freq [ ] = new int [ MAX_CHARS ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str . charAt ( i ) - ' a ' ] >= k ) { System . out . print ( str . charAt ( i ) ) ; } } } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 2 ; longestSubseqWithK ( str , k ) ; } }
Generating distinct subsequences of a given string in lexicographic order | Java program to print all distinct subsequences of a string . ; Finds and stores result in st for a given string s . ; If current string is not already present . ; Traverse current string , one by one remove every character and recur . ; Driver code
import java . util . * ; class GFG { static void generate ( Set < String > st , String s ) { if ( s . length ( ) == 0 ) { return ; } if ( ! st . contains ( s ) ) { st . add ( s ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { String t = s ; t = t . substring ( 0 , i ) + t . substring ( i + 1 ) ; generate ( st , t ) ; } } return ; } public static void main ( String args [ ] ) { String s = " xyz " ; TreeSet < String > st = new TreeSet < > ( ) ; generate ( st , s ) ; for ( String str : st ) { System . out . println ( str ) ; } } }
Print all subsequences of a string | Java Program to print all subsequence of a given string . ; Set to store all the subsequences ; Function computes all the subsequence of an string ; Iterate over the entire string ; Iterate from the end of the string to generate substrings ; Drop kth character in the substring and if its not in the set then recur ; Drop character from the string ; Driver code
import java . util . HashSet ; public class Subsequence { static HashSet < String > st = new HashSet < > ( ) ; static void subsequence ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = str . length ( ) ; j > i ; j -- ) { String sub_str = str . substring ( i , j ) ; if ( ! st . contains ( sub_str ) ) st . add ( sub_str ) ; for ( int k = 1 ; k < sub_str . length ( ) - 1 ; k ++ ) { StringBuffer sb = new StringBuffer ( sub_str ) ; sb . deleteCharAt ( k ) ; if ( ! st . contains ( sb ) ) ; subsequence ( sb . toString ( ) ) ; } } } } public static void main ( String [ ] args ) { String s = " aabc " ; subsequence ( s ) ; System . out . println ( st ) ; } }
Print all subsequences of a string | Java program to generate power set in lexicographic order . ; str : Stores input string n : Length of str . curr : Stores current permutation index : Index in current permutation , curr ; base case ; backtracking ; Generates power set in lexicographic order . ; Driver code
class GFG { static void printSubSeqRec ( String str , int n , int index , String curr ) { if ( index == n ) { return ; } if ( curr != null && ! curr . trim ( ) . isEmpty ( ) ) { System . out . println ( curr ) ; } for ( int i = index + 1 ; i < n ; i ++ ) { curr += str . charAt ( i ) ; printSubSeqRec ( str , n , i , curr ) ; curr = curr . substring ( 0 , curr . length ( ) - 1 ) ; } } static void printSubSeq ( String str ) { int index = - 1 ; String curr = " " ; printSubSeqRec ( str , str . length ( ) , index , curr ) ; } public static void main ( String [ ] args ) { String str = " cab " ; printSubSeq ( str ) ; } }
Recursive solution to count substrings with same first and last characters | Java program to count substrings with same first and last characters ; Function to count substrings with same first and last characters ; base cases ; Driver code
class GFG { static int countSubstrs ( String str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) res ++ ; return res ; } public static void main ( String [ ] args ) { String str = " abcab " ; int n = str . length ( ) ; System . out . print ( countSubstrs ( str , 0 , n - 1 , n ) ) ; } }
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Java Program to find minimum number of manipulations required to make two strings identical ; Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code
public class Similar_strings { static int countManipulations ( String s1 , String s2 ) { int count = 0 ; int char_count [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { char_count [ s2 . charAt ( i ) - ' a ' ] -- ; } for ( int i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += Math . abs ( char_count [ i ] ) ; } } return count / 2 ; } public static void main ( String [ ] args ) { String s1 = " ddcf " ; String s2 = " cedk " ; System . out . println ( countManipulations ( s1 , s2 ) ) ; } }
Least number of manipulations needed to ensure two strings have identical characters | Java program to count least number of manipulations to have two strings set of same characters ; return the count of manipulations required ; count the number of different characters in both strings ; check the difference in characters by comparing count arrays ; driver program
import java . io . * ; public class GFG { static int MAX_CHAR = 26 ; static int leastCount ( String s1 , String s2 , int n ) { int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { count1 [ s1 . charAt ( i ) - ' a ' ] += 1 ; count2 [ s2 . charAt ( i ) - ' a ' ] += 1 ; } int res = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( count1 [ i ] != 0 ) { res += Math . abs ( count1 [ i ] - count2 [ i ] ) ; } } return res ; } static public void main ( String [ ] args ) { String s1 = " abc " ; String s2 = " cdd " ; int len = s1 . length ( ) ; int res = leastCount ( s1 , s2 , len ) ; System . out . println ( res ) ; } }
Given two strings check which string makes a palindrome first | Java program to check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code
public class First_Palin { static final int MAX_CHAR = 26 ; static char stringPalindrome ( String A , String B ) { int [ ] countA = new int [ MAX_CHAR ] ; int [ ] countB = new int [ MAX_CHAR ] ; int l1 = A . length ( ) ; int l2 = B . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) countA [ A . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) countB [ B . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( ( countA [ i ] > 1 && countB [ i ] == 0 ) ) return ' A ' ; return ' B ' ; } public static void main ( String args [ ] ) { String a = " abcdea " ; String b = " bcdesg " ; System . out . println ( stringPalindrome ( a , b ) ) ; } }
Print the longest common substring | Java implementation to print the longest common substring ; function to find and print the longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; To store the index of the cell which contains the maximum value . This cell 's index helps in building  up the longest common substring from right to left. ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; if true , then no common substring exists ; allocate space for the longest common substring ; traverse up diagonally form the ( row , col ) cell until LCSuff [ row ] [ col ] != 0 ; resultStr = X . charAt ( row - 1 ) + resultStr ; or Y [ col - 1 ] ; move diagonally up to previous cell ; required longest common substring ; Driver program to test above function
public class Longest_common_substr { static void printLCSubStr ( String X , String Y , int m , int n ) { int [ ] [ ] LCSuff = new int [ m + 1 ] [ n + 1 ] ; int len = 0 ; int row = 0 , col = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) LCSuff [ i ] [ j ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { LCSuff [ i ] [ j ] = LCSuff [ i - 1 ] [ j - 1 ] + 1 ; if ( len < LCSuff [ i ] [ j ] ) { len = LCSuff [ i ] [ j ] ; row = i ; col = j ; } } else LCSuff [ i ] [ j ] = 0 ; } } if ( len == 0 ) { System . out . println ( " No ▁ Common ▁ Substring " ) ; return ; } String resultStr = " " ; while ( LCSuff [ row ] [ col ] != 0 ) { -- len ; row -- ; col -- ; } System . out . println ( resultStr ) ; } public static void main ( String args [ ] ) { String X = " OldSite : GeeksforGeeks . org " ; String Y = " NewSite : GeeksQuiz . com " ; int m = X . length ( ) ; int n = Y . length ( ) ; printLCSubStr ( X , Y , m , n ) ; } }
Find the first repeated word in a string | Java program for finding first repeated word in a string ; returns first repeated word ; break string into tokens and then each string into set if a word appeared before appears again , return the word and break ; hashmap for storing word and its count in sentence ; store all the words of string and the count of word in hashmap ; setOfWords . put ( token [ i ] , setOfWords . get ( token [ i ] ) + 1 ) ; word exists ; insert new word to set ; either take a new stream or store the words in vector of strings in previous loop ; driver program
import java . util . * ; class GFG { static String findFirstRepeated ( String s ) { String token [ ] = s . split ( " ▁ " ) ; HashMap < String , Integer > setOfWords = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < token . length ; i ++ ) { if ( setOfWords . containsKey ( token [ i ] ) ) else setOfWords . put ( token [ i ] , 1 ) ; } for ( int i = 0 ; i < token . length ; i ++ ) { int count = setOfWords . get ( token [ i ] ) ; if ( count > 1 ) { return token [ i ] ; } } return " NoRepetition " ; } public static void main ( String args [ ] ) { String s = " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ; String firstWord = findFirstRepeated ( s ) ; if ( ! firstWord . equals ( " NoRepetition " ) ) System . out . println ( " First ▁ repeated ▁ word ▁ : : ▁ " + firstWord ) ; else System . out . println ( " No ▁ Repetitionn " ) ; } }
Find the first repeated word in a string | Java program for finding first repeated word in a string ; returns first repeated word ; break string into tokens ; hashset for storing words ; store the words of string in hashset ; if word exists return ; insert new word to set ; driver program
import java . util . * ; public class GFG { static String findFirstRepeated ( String s ) { String token [ ] = s . split ( " ▁ " ) ; HashSet < String > set = new HashSet < String > ( ) ; for ( int i = 0 ; i < token . length ; i ++ ) { if ( set . contains ( token [ i ] ) ) { return token [ i ] ; } set . add ( token [ i ] ) ; } return " NoRepetition " ; } public static void main ( String args [ ] ) { String s = " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ; String firstWord = findFirstRepeated ( s ) ; if ( ! firstWord . equals ( " NoRepetition " ) ) System . out . println ( " First ▁ repeated ▁ word ▁ : : ▁ " + firstWord ) ; else System . out . println ( " No ▁ Repetitionn " ) ; } }
Convert all substrings of length ' k ' from base ' b ' to decimal | Simple Java program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code
class GFG { static void substringConversions ( String str , int k , int b ) { for ( int i = 0 ; i + k <= str . length ( ) ; i ++ ) { String sub = str . substring ( i , i + k ) ; int sum = 0 , counter = 0 ; for ( int j = sub . length ( ) - 1 ; j >= 0 ; j -- ) { sum = ( int ) ( sum + ( ( sub . charAt ( j ) - '0' ) * Math . pow ( b , counter ) ) ) ; counter ++ ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; } }
Longest Possible Chunked Palindrome | Java program to find the length of longest palindromic chunk ; Here s is the string whose LCP is needed ln is length of string evaluated till now and str is original string ; if there is noting at all ! ! ; if a single letter is left out ; for each length of substring chunk in string ; if left side chunk and right side chunk are same ; Call LCP for the part between the chunks and add 2 to the result . Length of string evaluated till now is increased by ( i + 1 ) * 2 ; Wrapper over LPCRec ( ) ; driver function
import java . util . * ; import java . lang . * ; import java . io . * ; class LongestPalindromicChunk { private static int LPCRec ( String curr_str , int count , int len , String str ) { if ( curr_str == null || curr_str . isEmpty ( ) ) return ( 0 ) ; if ( curr_str . length ( ) <= 1 ) { if ( count != 0 && str . length ( ) - len <= 1 ) return ( count + 1 ) ; else return ( 1 ) ; } int n = curr_str . length ( ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( curr_str . substring ( 0 , i + 1 ) . equals ( curr_str . substring ( n - 1 - i , n ) ) ) { return LPCRec ( curr_str . substring ( i + 1 , n - 1 - i ) , count + 2 , len + ( i + 1 ) * 2 , str ) ; } } return count + 1 ; } public static int LPC ( String str ) { return LPCRec ( str , 0 , 0 , str ) ; } public static void main ( String [ ] args ) { System . out . println ( " V ▁ : ▁ " + LPC ( " V " ) ) ; System . out . println ( " VOLVO ▁ : ▁ " + LPC ( " VOLVO " ) ) ; System . out . println ( " VOLVOV ▁ : ▁ " + LPC ( " VOLVOV " ) ) ; System . out . println ( " ghiabcdefhelloadamhelloabcdefghi ▁ : ▁ " + LPC ( " ghiabcdefhelloadamhelloabcdefghi " ) ) ; System . out . println ( " ghiabcdefhelloadamhelloabcdefghik ▁ : ▁ " + LPC ( " ghiabcdefhelloadamhelloabcdefghik " ) ) ; System . out . println ( " antaprezatepzapreanta ▁ : ▁ " + LPC ( " antaprezatepzapreanta " ) ) ; } }
Find numbers of balancing positions in string | Java program to find number of balancing points in string ; method to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Method
class GFG { static final int MAX_CHAR = 256 ; static int countBalance ( String s ) { char [ ] str = s . toCharArray ( ) ; int [ ] rightVisited = new int [ MAX_CHAR ] ; int [ ] leftVisited = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) rightVisited [ str [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftVisited [ str [ i ] ] ++ ; rightVisited [ str [ i ] ] -- ; int j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } public static void main ( String [ ] args ) { String str = " abaababa " ; System . out . println ( countBalance ( str ) ) ; } }
Min flips of continuous characters to make all characters same in a string | Java program to find min flips in binary string to make all characters equal ; To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver program to check findFlips ( )
public class minFlips { static int findFlips ( String str , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str . charAt ( i ) ) res ++ ; last = str . charAt ( i ) ; } return res / 2 ; } public static void main ( String [ ] args ) { String str = "00011110001110" ; int n = str . length ( ) ; System . out . println ( findFlips ( str , n ) ) ; } }
Maximum length substring having all same characters after k changes | Java program to find maximum length equal character string with k changes ; method to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; method which returns maximum length of substring ; Driver Method
public class GFG { static int findLen ( String A , int n , int k , char ch ) { int maxlen = 1 ; int cnt = 0 ; int l = 0 , r = 0 ; while ( r < n ) { if ( A . charAt ( r ) != ch ) ++ cnt ; while ( cnt > k ) { if ( A . charAt ( l ) != ch ) -- cnt ; ++ l ; } maxlen = Math . max ( maxlen , r - l + 1 ) ; ++ r ; } return maxlen ; } static int answer ( String A , int n , int k ) { int maxlen = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) { maxlen = Math . max ( maxlen , findLen ( A , n , k , ( char ) ( i + ' A ' ) ) ) ; maxlen = Math . max ( maxlen , findLen ( A , n , k , ( char ) ( i + ' a ' ) ) ) ; } return maxlen ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; String A = " ABABA " ; System . out . println ( " Maximum ▁ length ▁ = ▁ " + answer ( A , n , k ) ) ; n = 6 ; k = 4 ; String B = " HHHHHH " ; System . out . println ( " Maximum ▁ length ▁ = ▁ " + answer ( B , n , k ) ) ; } }
Quick way to check if all the characters of a string are same | Java program for above approach ; Function to check is all the characters in string are or not ; Insert characters in the set ; If all characters are same Size of set will always be 1 ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static void allCharactersSame ( String s ) { Set < Character > s1 = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) s1 . add ( s . charAt ( i ) ) ; if ( s1 . size ( ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { String str = " nnnn " ; allCharactersSame ( str ) ; } }
Find the character in first string that is present at minimum index in second string | Java implementation to find the character in first string that is present at minimum index in second string ; method to find the minimum index character ; to store the index of character having minimum index ; lengths of the two strings ; traverse ' patt ' ; for each character of ' patt ' traverse ' str ' ; if patt . charAt ( i ) is found in ' str ' , check if it has the minimum index or not . If yes , then update ' minIndex ' and break ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver Method
public class GFG { static void printMinIndexChar ( String str , String patt ) { int minIndex = Integer . MAX_VALUE ; int m = str . length ( ) ; int n = patt . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( patt . charAt ( i ) == str . charAt ( j ) && j < minIndex ) { minIndex = j ; break ; } } } if ( minIndex != Integer . MAX_VALUE ) System . out . println ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str . charAt ( minIndex ) ) ; else System . out . println ( " No ▁ character ▁ present " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }
Find the character in first string that is present at minimum index in second string | Java implementation to find the character in first string that is present at minimum index in second string ; method to find the minimum index character ; map to store the first index of each character of ' str ' ; to store the index of character having minimum index ; lengths of the two strings ; store the first index of each character of ' str ' ; traverse the string ' patt ' ; if patt [ i ] is found in ' um ' , check if it has the minimum index or not accordingly update ' minIndex ' ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver Method
import java . util . HashMap ; public class GFG { static void printMinIndexChar ( String str , String patt ) { HashMap < Character , Integer > hm = new HashMap < > ( ) ; int minIndex = Integer . MAX_VALUE ; int m = str . length ( ) ; int n = patt . length ( ) ; for ( int i = 0 ; i < m ; i ++ ) if ( ! hm . containsKey ( str . charAt ( i ) ) ) hm . put ( str . charAt ( i ) , i ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hm . containsKey ( patt . charAt ( i ) ) && hm . get ( patt . charAt ( i ) ) < minIndex ) minIndex = hm . get ( patt . charAt ( i ) ) ; if ( minIndex != Integer . MAX_VALUE ) System . out . println ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str . charAt ( minIndex ) ) ; else System . out . println ( " No ▁ character ▁ present " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }