codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically first palindromic string | Java program to find first palindromic permutation of given string ; Function to count frequency of each char in the string . freq [ 0 ] for ' a ' , ... . , freq [ 25 ] for ' z ' ; Cases to check whether a palindr0mic string can be formed or not ; count_odd to count no of chars with odd frequency ; For even length string no odd freq character ; For odd length string one odd freq character ; Function to find odd freq char and reducing its freq by 1 returns " " if odd freq char is not present ; To find lexicographically first palindromic string . ; Assigning odd freq character if present else empty string . ; Traverse characters in increasing order ; Divide all occurrences into two halves . Note that odd character is removed by findOddAndRemoveItsFreq ( ) ; creating front string ; creating rear string ; Final palindromic string which is lexicographically first ; Driver program
class GFG { static char MAX_CHAR = 26 ; static void countFreq ( String str , int freq [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } } static boolean canMakePalindrome ( int freq [ ] , int len ) { int count_odd = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { count_odd ++ ; } } if ( len % 2 == 0 ) { if ( count_odd > 0 ) { return false ; } else { return true ; } } if ( count_odd != 1 ) { return false ; } return true ; } static String findOddAndRemoveItsFreq ( int freq [ ] ) { String odd_str = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { freq [ i ] -- ; odd_str = odd_str + ( char ) ( i + ' a ' ) ; return odd_str ; } } return odd_str ; } static String findPalindromicString ( String str ) { int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; countFreq ( str , freq , len ) ; if ( ! canMakePalindrome ( freq , len ) ) { return " No ▁ Palindromic ▁ String " ; } String odd_str = findOddAndRemoveItsFreq ( freq ) ; String front_str = " " , rear_str = " ▁ " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { String temp = " " ; if ( freq [ i ] != 0 ) { char ch = ( char ) ( i + ' a ' ) ; for ( int j = 1 ; j <= freq [ i ] / 2 ; j ++ ) { temp = temp + ch ; } front_str = front_str + temp ; rear_str = temp + rear_str ; } } return ( front_str + odd_str + rear_str ) ; } public static void main ( String [ ] args ) { String str = " malayalam " ; System . out . println ( findPalindromicString ( str ) ) ; } }
Count substrings with same first and last characters | Java program to count all substrings with same first and last characters . ; Returns true if first and last characters of s are same . ; Starting point of substring ; Length of substring ; Check if current substring has same starting and ending characters . ; Driver function
public class GFG { static boolean checkEquality ( String s ) { return ( s . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) ; } static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . substring ( i , i + len ) ) ) result ++ ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }
Count substrings with same first and last characters | Space efficient Java program to count all substrings with same first and last characters . ; Iterating through all substrings in way so that we can find first and last character easily ; Driver function
public class GFG { static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s . charAt ( i ) == s . charAt ( j ) ) result ++ ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }
Find the missing number in a string of numbers with no separator | Java program to find a missing number in a string of consecutive numbers without any separator . ; gets the integer at position i with length m , returns it or - 1 , if none ; Find value at index i and length m . ; Returns value of missing number ; Try all lengths for first number ; Get value of first number with current length / ; To store missing number of current length ; To indicate whether the sequence failed anywhere for current length . ; Find subsequent numbers with previous number as n ; If we haven 't yet found the missing number  for current length. Next number is n+2. Note  that we use Log10 as (n+2) may have more  length than n. ; } If next value is ( n + 1 ) ; return - 1 ; not found or no missing number ; Driver code
class GFG { static final int MAX_DIGITS = 6 ; static int getValue ( String str , int i , int m ) { if ( i + m > str . length ( ) ) { return - 1 ; } int value = 0 ; for ( int j = 0 ; j < m ; j ++ ) { int c = str . charAt ( i + j ) - '0' ; if ( c < 0 c > 9 ) { return - 1 ; } value = value * 10 + c ; } return value ; } static int findMissingNumber ( String str ) { for ( int m = 1 ; m <= MAX_DIGITS ; ++ m ) { int n = getValue ( str , 0 , m ) ; if ( n == - 1 ) { break ; } int missingNo = - 1 ; boolean fail = false ; for ( int i = m ; i != str . length ( ) ; i += 1 + Math . log10 ( n ) ) { if ( ( missingNo == - 1 ) && ( getValue ( str , i , ( int ) ( 1 + Math . log10 ( n + 2 ) ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; else if ( getValue ( str , i , ( int ) ( 1 + Math . log10 ( n + 1 ) ) ) == n + 1 ) { n ++ ; } else { fail = true ; break ; } } if ( ! fail ) { return missingNo ; } } } public static void main ( String [ ] args ) { System . out . println ( findMissingNumber ( "99101102" ) ) ; } }
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 ; Find the maximum repeating character starting from str [ i ] ; Update result if required ; Driver code
public class GFG { static char maxRepeating ( String str ) { int len = str . length ( ) ; int count = 0 ; char res = str . charAt ( 0 ) ; for ( int i = 0 ; i < len ; i ++ ) { int cur_count = 1 ; for ( int j = i + 1 ; j < len ; j ++ ) { if ( str . charAt ( i ) != str . charAt ( j ) ) break ; cur_count ++ ; } if ( cur_count > count ) { count = cur_count ; res = str . charAt ( i ) ; } } return res ; } public static void main ( String args [ ] ) { String str = " aaaabbaaccde " ; System . out . println ( maxRepeating ( str ) ) ; } }
Sum of two large numbers | Java program to find sum of two large numbers . ; Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty String for storing result ; Calculate length of both String ; Reverse both of Strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant String ; Driver code
import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; str1 = new StringBuilder ( str1 ) . reverse ( ) . toString ( ) ; str2 = new StringBuilder ( str2 ) . reverse ( ) . toString ( ) ; int carry = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n1 ; i < n2 ; i ++ ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; str = new StringBuilder ( str ) . reverse ( ) . toString ( ) ; return str ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }
Sum of two large numbers | Java program to find sum of two large numbers . ; Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty String for storing result ; Calculate length of both String ; Initially take carry zero ; Traverse from end of both Strings ; Do school mathematics , compute sum of current digits and carry ; Add remaining digits of str2 [ ] ; Add remaining carry ; reverse resultant String ; Driver code
import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n2 - n1 ; int carry = 0 ; for ( int i = n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i + diff ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n2 - n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; return new StringBuilder ( str ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }
Palindrome pair in an array of words ( or strings ) | Java program to find if there is a pair that can form a palindrome . ; Utility function to check if a string is a palindrome ; compare each character from starting with its corresponding character from last ; Function to check if a palindrome pair exists ; Consider each pair one by one ; concatenate both strings ; check if the concatenated string is palindrome ; check if the concatenated string is palindrome ; Driver code
import java . util . Arrays ; import java . util . List ; public class Palin_pair1 { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( len - i - 1 ) ) return false ; return true ; } static boolean checkPalindromePair ( List < String > vect ) { for ( int i = 0 ; i < vect . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < vect . size ( ) ; j ++ ) { String check_str = " " ; check_str = check_str + vect . get ( i ) + vect . get ( j ) ; if ( isPalindrome ( check_str ) ) return true ; check_str = vect . get ( j ) + vect . get ( i ) ; if ( isPalindrome ( check_str ) ) return true ; } } return false ; } public static void main ( String args [ ] ) { List < String > vect = Arrays . asList ( " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " ) ; if ( checkPalindromePair ( vect ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Print consecutive characters together in a line | Java program to print consecutive characters together in a line . ; Driver code
class GFG { static void print ( String str1 ) { char str [ ] = str1 . toCharArray ( ) ; System . out . print ( str [ 0 ] ) ; for ( int i = 1 ; i < str . length ; i ++ ) { if ( ( str [ i ] == str [ i - 1 ] + 1 ) || ( str [ i ] == str [ i - 1 ] - 1 ) ) { System . out . print ( str [ i ] ) ; } else { System . out . print ( "NEW_LINE" + str[i]); } } } public static void main ( String [ ] args ) { String str = " ABCXYZACCD " ; print ( str ) ; } }
Efficiently check if a string has all unique characters without using any additional data structure | A space efficient Java program to check if all characters of string are unique . ; Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; driver code
class GFG { static boolean areChractersUnique ( String str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { int val = ( str . charAt ( i ) - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } public static void main ( String [ ] args ) { String s = " aaabbccdaa " ; if ( areChractersUnique ( s ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Count of words whose i | Java program to count words whose ith letter is either ( i - 1 ) th , ith , or ( i + 1 ) th letter of given word . ; Return the count of words . ; If word contain single letter , return 1. ; Checking for first letter . ; Traversing the string and multiplying for combinations . ; If all three letters are same . ; If two letter are distinct . ; If all three letter are distinct . ; Checking for last letter . ; Driven Program
public class GFG { static int countWords ( String str , int len ) { int count = 1 ; if ( len == 1 ) return count ; if ( str . charAt ( 0 ) == str . charAt ( 1 ) ) count *= 1 ; else count *= 2 ; for ( int j = 1 ; j < len - 1 ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) && str . charAt ( j ) == str . charAt ( j + 1 ) ) count *= 1 ; else if ( str . charAt ( j ) == str . charAt ( j - 1 ) || str . charAt ( j ) == str . charAt ( j + 1 ) || str . charAt ( j - 1 ) == str . charAt ( j + 1 ) ) count *= 2 ; else count *= 3 ; } if ( str . charAt ( len - 1 ) == str . charAt ( len - 2 ) ) count *= 1 ; else count *= 2 ; return count ; } public static void main ( String args [ ] ) { String str = " abc " ; int len = str . length ( ) ; System . out . println ( countWords ( str , len ) ) ; } }
Maximum and minimum sums from two numbers with digit replacements | Java program to find maximum and minimum possible sums of two numbers that we can get if replacing digit from 5 to 6 and vice versa are allowed . ; Find new value of x after replacing digit " from " to " to " ; Required digit found , replace it ; Returns maximum and minimum possible sums of x1 and x2 if digit replacements are allowed . ; We always get minimum sum if we replace 6 with 5. ; We always get maximum sum if we replace 5 with 6. ; Driver code
class GFG { static int replaceDig ( int x , int from , int to ) { int result = 0 ; int multiply = 1 ; while ( x > 0 ) { int reminder = x % 10 ; if ( reminder == from ) result = result + to * multiply ; else result = result + reminder * multiply ; multiply *= 10 ; x = x / 10 ; } return result ; } static void calculateMinMaxSum ( int x1 , int x2 ) { int minSum = replaceDig ( x1 , 6 , 5 ) + replaceDig ( x2 , 6 , 5 ) ; int maxSum = replaceDig ( x1 , 5 , 6 ) + replaceDig ( x2 , 5 , 6 ) ; System . out . print ( " Minimum ▁ sum ▁ = ▁ " + minSum ) ; System . out . print ( " Maximum sum = " + maxSum); } public static void main ( String [ ] args ) { int x1 = 5466 , x2 = 4555 ; calculateMinMaxSum ( x1 , x2 ) ; } }
Queries on substring palindrome formation | Java program to Queries on substring palindrome formation . ; Query type 1 : update string position i with character x . ; Print " Yes " if range [ L . . R ] can form palindrome , else print " No " . ; Find the frequency of each character in S [ L ... R ] . ; Checking if more than one character have frequency greater than 1. ; Driven code
class GFG { static void qType1 ( int l , int x , char str [ ] ) { str [ l - 1 ] = ( char ) x ; } static void qType2 ( int l , int r , char str [ ] ) { int freq [ ] = new int [ 27 ] ; for ( int i = l - 1 ; i <= r - 1 ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int count = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ j ] % 2 != 0 ) { count ++ ; } } if ( count <= 1 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; qType1 ( 4 , ' g ' , str ) ; qType2 ( 1 , 4 , str ) ; qType2 ( 2 , 3 , str ) ; qType1 ( 10 , ' t ' , str ) ; qType2 ( 10 , 11 , str ) ; } }
Queries on substring palindrome formation | Java program to Queries on substring palindrome formation . ; Return the frequency of the character in the i - th prefix . ; Updating the BIT ; Query to update the character in the string . ; Adding - 1 at L position ; Updating the character ; Adding + 1 at R position ; Query to find if rearrangement of character in range L ... R can form palindrome ; Checking on the first character of the string S . ; Checking if frequency of character is even or odd . ; Creating the Binary Index Tree of all aphabet ; Driver code
import java . util . * ; class GFG { static int max = 1000 ; static int getFrequency ( int tree [ ] [ ] , int idx , int i ) { int sum = 0 ; while ( idx > 0 ) { sum += tree [ idx ] [ i ] ; idx -= ( idx & - idx ) ; } return sum ; } static void update ( int tree [ ] [ ] , int idx , int val , int i ) { while ( idx <= max ) { tree [ idx ] [ i ] += val ; idx += ( idx & - idx ) ; } } static void qType1 ( int tree [ ] [ ] , int l , int x , char str [ ] ) { update ( tree , l , - 1 , str [ l - 1 ] - 97 + 1 ) ; str [ l - 1 ] = ( char ) x ; update ( tree , l , 1 , str [ l - 1 ] - 97 + 1 ) ; } static void qType2 ( int tree [ ] [ ] , int l , int r , char str [ ] ) { int count = 0 ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( l == 1 ) { if ( getFrequency ( tree , r , i ) % 2 == 1 ) count ++ ; } else { if ( ( getFrequency ( tree , r , i ) - getFrequency ( tree , l - 1 , i ) ) % 2 == 1 ) count ++ ; } } if ( count <= 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } static void buildBIT ( int tree [ ] [ ] , char str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) update ( tree , i + 1 , 1 , str [ i ] - 97 + 1 ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; int tree [ ] [ ] = new int [ max ] [ 27 ] ; buildBIT ( tree , str , n ) ; qType1 ( tree , 4 , ' g ' , str ) ; qType2 ( tree , 1 , 4 , str ) ; qType2 ( tree , 2 , 3 , str ) ; qType1 ( tree , 10 , ' t ' , str ) ; qType2 ( tree , 10 , 11 , str ) ; } }
Partition a number into two divisible parts | Java program to check if a string can be splitted into two strings such that one is divisible by ' a ' and other is divisible by ' b ' . ; Finds if it is possible to partition str into two parts such that first part is divisible by a and second part is divisible by b . ; Create an array of size len + 1 and initialize it with 0. Store remainders from left to right when divided by ' a ' ; Compute remainders from right to left when divided by ' b ' ; Find a point that can partition a number ; If split is not possible at this point ; We can split at i if one of the following two is true . a ) All characters after str . charAt ( i ] are 0 b ) String after str . charAt ( i ] is divisible by b , i . e . , str . charAt ( i + 1. . n - 1 ] is divisible by b . ; Driver code
class GFG { static void findDivision ( String str , int a , int b ) { int len = str . length ( ) ; int [ ] lr = new int [ len + 1 ] ; lr [ 0 ] = ( ( int ) str . charAt ( 0 ) - ( int ) '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( ( int ) str . charAt ( i ) - ( int ) '0' ) ) % a ; int [ ] rl = new int [ len + 1 ] ; rl [ len - 1 ] = ( ( int ) str . charAt ( len - 1 ) - ( int ) '0' ) % b ; int power10 = 10 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( ( int ) str . charAt ( i ) - ( int ) '0' ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { System . out . println ( " YES " ) ; for ( int k = 0 ; k <= i ; k ++ ) System . out . print ( str . charAt ( k ) ) ; System . out . print ( " , ▁ " ) ; for ( int k = i + 1 ; k < len ; k ++ ) System . out . print ( str . charAt ( k ) ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { String str = "123" ; int a = 12 , b = 3 ; findDivision ( str , a , b ) ; } }
Efficient method for 2 's complement of a binary string | An efficient Java program to find 2 's complement ; Method to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concat 1 at the starting of string ; Continue traversal after the position of first '1' ; Just flip the values ; return the modified string ; Driver method
class Test { static String findTwoscomplement ( StringBuffer str ) { int n = str . length ( ) ; int i ; for ( i = n - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == '1' ) break ; if ( i == - 1 ) return "1" + str ; for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( str . charAt ( k ) == '1' ) str . replace ( k , k + 1 , "0" ) ; else str . replace ( k , k + 1 , "1" ) ; } return str . toString ( ) ; } public static void main ( String [ ] args ) { StringBuffer str = new StringBuffer ( "00000101" ) ; System . out . println ( findTwoscomplement ( str ) ) ; } }
Check length of a string is equal to the number appended at its last | Java program to check if size of string is appended at the end or not . ; Function to find if given number is equal to length or not ; Traverse string from end and find the number stored at the end . x is used to store power of 10. ; Check if number is equal to string length except that number 's digits ; Drivers code
import java . io . * ; class GFG { static boolean isequal ( String str ) { int n = str . length ( ) ; int num = 0 , x = 1 , i = n - 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( '0' <= str . charAt ( i ) && str . charAt ( i ) <= '9' ) { num = ( str . charAt ( i ) - '0' ) * x + num ; x = x * 10 ; if ( num >= n ) return false ; } else break ; } return num == i + 1 ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks13" ; if ( isequal ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if two strings are k | Java program for the above approach ; Function to check k anagram of two strings ; First Condition : If both the strings have different length , then they cannot form anagram ; Converting str1 to Character Array arr1 ; Converting str2 to Character Array arr2 ; Sort arr1 in increasing order ; Sort arr2 in increasing order ; Iterate till str1 . length ( ) ; Condition if arr1 [ i ] is not equal to arr2 [ i ] then add it to list ; Condition to check if strings for K - anagram or not ; Driver Code ; Function Call
import java . util . * ; class GFG { public static boolean kAnagrams ( String str1 , String str2 , int k ) { int flag = 0 ; List < Character > list = new ArrayList < > ( ) ; if ( str1 . length ( ) != str2 . length ( ) ) System . exit ( 0 ) ; char arr1 [ ] = str1 . toCharArray ( ) ; char arr2 [ ] = str2 . toCharArray ( ) ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { list . add ( arr2 [ i ] ) ; } } if ( list . size ( ) <= k ) flag = 1 ; if ( flag == 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { String str1 = " fodr " ; String str2 = " gork " ; int k = 2 ; kAnagrams ( str1 , str2 , k ) ; if ( kAnagrams ( str1 , str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimum number of characters to be removed to make a binary string alternate | Java program to find minimum number of characters to be removed to make a string alternate . ; Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; result ++ ; then need to delete a character ; Driver code
import java . io . * ; public class GFG { static int countToMake0lternate ( String s ) { int result = 0 ; for ( int i = 0 ; i < ( s . length ( ) - 1 ) ; i ++ ) if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) return result ; } static public void main ( String [ ] args ) { System . out . println ( countToMake0lternate ( "000111" ) ) ; System . out . println ( countToMake0lternate ( "11111" ) ) ; System . out . println ( countToMake0lternate ( "01010101" ) ) ; } }