codeparrot/xlcost-text-to-code huggingface dataset:
Maximum even length sub | Java code to find the maximum length of sub - string ( of even length ) which can be arranged into a Palindrome ; function that returns true if the given sub - string can be arranged into a Palindrome ; This function returns the maximum length of the sub - string ( of even length ) which can be arranged into a Palindrome ; If we reach end of the string ; if string is of even length ; if string can be arranged into a palindrome ; Even length sub - string ; Check if current sub - string can be arranged into a palindrome ; Odd length sub - string ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static HashMap < Integer , Integer > count = new HashMap < > ( ) ; static boolean canBePalindrome ( HashMap < Integer , Integer > count ) { for ( HashMap . Entry < Integer , Integer > entry : count . entrySet ( ) ) if ( ( entry . getValue ( ) & 1 ) == 1 ) return false ; return true ; } static int maxPal ( String str , int start , int end , HashMap < Integer , Integer > count ) { if ( end == str . length ( ) ) { if ( ( end - start ) % 2 == 0 ) if ( canBePalindrome ( count ) ) return end - start ; return 0 ; } else { if ( ( end - start ) % 2 == 0 ) { if ( canBePalindrome ( count ) ) { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; return Math . max ( end - start , maxPal ( str , start , end + 1 , count ) ) ; } else { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; return maxPal ( str , start , end + 1 , count ) ; } } else { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; HashMap < Integer , Integer > c = new HashMap < > ( count ) ; int length = maxPal ( str , start , end + 1 , c ) ; count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? - 1 : count . get ( ( int ) str . charAt ( end ) ) - 1 ) ; count . put ( ( int ) str . charAt ( start ) , count . get ( ( int ) str . charAt ( start ) ) == null ? - 1 : count . get ( ( int ) str . charAt ( start ) ) - 1 ) ; return Math . max ( length , maxPal ( str , start + 1 , end , count ) ) ; } } } public static void main ( String [ ] args ) { String str = "124565463" ; int start = 0 , end = 0 ; System . out . println ( maxPal ( str , start , end , count ) ) ; } }
Minimum deletions from string to reduce it to string with at most 2 unique characters | Java implementation of the above approach ; Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver program to test above functions
public class GFG { static int check ( String s ) { int i , j ; int fr [ ] = new int [ 26 ] ; int n = s . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { char x = s . charAt ( i ) ; fr [ x - ' a ' ] += 1 ; } int minimum = Integer . MAX_VALUE ; for ( i = 0 ; i < 26 ; i ++ ) { for ( j = i + 1 ; j < 26 ; j ++ ) { int z = fr [ i ] + fr [ j ] ; minimum = Math . min ( minimum , n - z ) ; } } return minimum ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( check ( s ) ) ; } }
Count and Print the alphabets having ASCII value not in the range [ l , r ] | Java implementation of the above approach ; Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
import java . util . * ; class Solution { static int CountCharacters ( String str , int l , int r ) { int cnt = 0 ; Map < Character , Integer > m = new HashMap < Character , Integer > ( ) ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str . charAt ( i ) && str . charAt ( i ) <= r ) ) { cnt ++ ; if ( m . get ( str . charAt ( i ) ) != null ) if ( m . get ( str . charAt ( i ) ) != 1 ) { System . out . print ( str . charAt ( i ) + " ▁ " ) ; m . put ( str . charAt ( i ) , m . get ( str . charAt ( i ) ) == null ? 0 : m . get ( str . charAt ( i ) ) + 1 ) ; } } } return cnt ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int l = 102 , r = 111 ; System . out . println ( "Characters with ASCII values not in the range [l, r]  in the given string are : "); System . out . println ( " and their count is " + CountCharacters(str, l, r)); } }
Count the number of words having sum of ASCII values less than and greater than k | Java implementation of the above approach ; Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; else Add the ascii value to sum ; Handling the Last word separately ; Driver code
class GFG { static void CountWords ( String str , int k ) { int sum = 0 ; int NumberOfWords = 0 ; int counter = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str . charAt ( i ) == ' ▁ ' ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } { sum += str . charAt ( i ) ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ " + " of ▁ ASCII ▁ values ▁ less ▁ than ▁ k ▁ = ▁ " + counter ) ; System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ " + " ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = ▁ " + ( NumberOfWords - counter ) ) ; } public static void main ( String [ ] args ) { String str = " Learn ▁ how ▁ to ▁ code " ; int k = 400 ; CountWords ( str , k ) ; } }
Rearrange a string in the form of integer sum followed by the minimized character | Java implementation of above approach ; function to return maximum volume ; separate digits and alphabets ; change digit sum to string ; change alphabet sum to string ; concatenate sum to alphabets string ; Driver code
import java . util . * ; class Solution { static String separateChar ( String str ) { int n = str . length ( ) , digitSum = 0 ; int alphabetSum = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) { digitSum += ( int ) ( str . charAt ( i ) - '0' ) ; } else { alphabetSum += str . charAt ( i ) - ' a ' + 1 ; alphabetSum %= 26 ; } } String sumStr = " " + ( digitSum ) ; char alphabetStr = ( char ) ( alphabetSum + ' a ' - 1 ) ; sumStr += alphabetStr ; return sumStr ; } public static void main ( String args [ ] ) { String str = "3652adyz3423" ; System . out . println ( separateChar ( str ) ) ; } }
Find the count of palindromic sub | Java program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; driver program
class GFG { final static int MAX_CHAR = 26 ; static int countPalindrome ( String str ) { int n = str . length ( ) ; int sum = 0 ; int hashTable [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] != 0 ) { sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } } return sum ; } public static void main ( String [ ] args ) { String str = " ananananddd " ; System . out . println ( countPalindrome ( str ) ) ; } }
Minimum number of elements to be removed so that pairwise consecutive elements are same | Java implementation of the above approach ; Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { i ++ ; } else { count ++ ; } } return count ; } public static void main ( String args [ ] ) { String str = "44522255" ; System . out . println ( countConsecutive ( str ) ) ; } }
Smallest odd digits number not less than N | Java program to print the smallest integer not less than N with all odd digits ; function to check if all digits are odd of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 == 0 ) if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
class Geeks { static int check_digits ( int n ) { while ( n > 0 ) { return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) > 0 ) return i ; } public static void main ( String args [ ] ) { int N = 2397 ; System . out . println ( smallest_number ( N ) ) ; } }
Smallest odd digits number not less than N | Java program to print the smallest integer not less than N with all odd digits ; function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s . charAt ( i ) - '0' ) ; } num = num * 10 + ( s . charAt ( index ) - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallestNumber ( N ) ) ; } }
Count and Print the alphabets having ASCII value in the range [ l , r ] | Java implementation of the above approach ; Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int CountCharacters ( String str , int l , int r ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( l <= str . charAt ( i ) && str . charAt ( i ) <= r ) { cnt ++ ; System . out . print ( str . charAt ( i ) + " ▁ " ) ; } } return cnt ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int l = 102 , r = 111 ; System . out . print ( " Characters ▁ with ▁ ASCII ▁ values " + " in the range [l, r] are NEW_LINE"); System . out . print ( " and their count is " + CountCharacters ( str , l , r ) ) ; } }
Minimum steps to remove substring 010 from a binary string | Java program to calculate steps to remove substring 010 from a binary string ; Function to find the minimum steps ; substring "010" found ; Driver code ; Get the binary string ; Find the minimum steps
import java . util . * ; class GFG { static int minSteps ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) - 2 ; i ++ ) { if ( ( ( int ) str . charAt ( i ) ) == '0' ) { if ( str . charAt ( i + 1 ) == '1' ) { if ( str . charAt ( i + 2 ) == '0' ) { count ++ ; i += 2 ; } } } } return count ; } public static void main ( String args [ ] ) { String str = "0101010" ; System . out . println ( minSteps ( str ) ) ; } }
XOR of Prime Frequencies of Characters in a String | Java program to find XOR of Prime Frequencies of Characters in a String ; Function to create Sieve to check primes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int xorOfPrime ( char [ ] s ) { boolean [ ] prime = new boolean [ 100005 ] ; for ( int i = 0 ; i < 100005 ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , 10005 ) ; int i , j ; Map < Character , Integer > m = new HashMap < > ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { if ( m . containsKey ( s [ i ] ) ) { m . put ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . put ( s [ i ] , 1 ) ; } } int result = 0 ; int flag = 0 ; for ( Map . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( prime [ entry . getValue ( ) ] ) { result ^= entry . getValue ( ) ; flag = 1 ; } } if ( flag != 1 ) return - 1 ; return result ; } public static void main ( String [ ] args ) { char [ ] s = " gggggeeekkkks " . toCharArray ( ) ; System . out . println ( xorOfPrime ( s ) ) ; } }
Count of alphabets having ASCII value less than and greater than k | Java implementation of the above approach ; Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code
import java . util . * ; class GFG { static int CountCharacters ( String str , int k ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ( ( int ) str . charAt ( i ) ) < k ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String str = " GeeksForGeeks " ; int k = 90 ; int count = CountCharacters ( str , k ) ; System . out . println ( " Characters ▁ with ▁ ASCII ▁ values ▁ less ▁ than ▁ K ▁ are ▁ " + count ) ; System . out . println ( " Characters ▁ with ▁ ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ K ▁ are ▁ " + ( str . length ( ) - count ) ) ; } }
De Bruijn sequence | Set 1 | Java implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
import java . util . * ; class GFG { static Set < String > seen = new HashSet < String > ( ) ; static Vector < Integer > edges = new Vector < Integer > ( ) ; static void dfs ( String node , int k , String A ) { for ( int i = 0 ; i < k ; ++ i ) { String str = node + A . charAt ( i ) ; if ( ! seen . contains ( str ) ) { seen . add ( str ) ; dfs ( str . substring ( 1 ) , k , A ) ; edges . add ( i ) ; } } } static String deBruijn ( int n , int k , String A ) { seen . clear ( ) ; edges . clear ( ) ; String startingNode = string ( n - 1 , A . charAt ( 0 ) ) ; dfs ( startingNode , k , A ) ; String S = " " ; int l = ( int ) Math . pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A . charAt ( edges . get ( i ) ) ; S += startingNode ; return S ; } private static String string ( int n , char charAt ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; String A = "01" ; System . out . print ( deBruijn ( n , k , A ) ) ; } }
Number of words in a camelcase sequence | Java code to find the count of words in a CamelCase sequence ; Function to find the count of words in a CamelCase sequence ; Driver code
class solution { static int countWords ( String str ) { int count = 1 ; for ( int i = 1 ; i < str . length ( ) - 1 ; i ++ ) { if ( str . charAt ( i ) >= 65 && str . charAt ( i ) <= 90 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String str = " geeksForGeeks " ; System . out . print ( countWords ( str ) ) ; } }
Check whether frequency of characters in a string makes Fibonacci Sequence | Java program to check whether frequency of characters in a string makes Fibonacci Sequence ; Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; Vector to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver Code
import java . util . HashMap ; import java . util . Vector ; class GFG { static String isFibonacci ( String s ) { HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) == null ? 1 : m . get ( s . charAt ( i ) ) + 1 ) ; Vector < Integer > v = new Vector < > ( ) ; int n = m . size ( ) ; int a = 1 , b = 1 ; int c ; v . add ( a ) ; v . add ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { v . add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; int i = 0 ; for ( HashMap . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( entry . getValue ( ) != v . elementAt ( i ) ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String s = " abeeedd " ; System . out . println ( isFibonacci ( s ) ) ; } }
Lexicographically smallest string formed by removing at most one character | Java program to find the lexicographically smallest String by removing at most one character ; Function to return the smallest String ; iterate the String ; first point where s [ i ] > s [ i + 1 ] ; append the String without i - th character in it ; leave the last character ; Driver Code
class GFG { static String smallest ( String s ) { int l = s . length ( ) ; String ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s . charAt ( j ) ; } } return ans ; } } ans = s . substring ( 0 , l - 1 ) ; return ans ; } public static void main ( String [ ] args ) { String s = " abcda " ; System . out . println ( smallest ( s ) ) ; } }
Arrangement of the characters of a word such that all vowels are at odd places | Java program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } static int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countPermutations ( String str ) { int even = ( int ) Math . floor ( ( double ) ( str . length ( ) / 2 ) ) ; int odd = str . length ( ) - even ; int ways = 0 ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ ( int ) ( str . charAt ( i ) - ' a ' ) ] ++ ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . length ( ) - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } public static void main ( String [ ] args ) { String str = " geeks " ; System . out . println ( countPermutations ( str ) ) ; } }
Replace all consonants with nearest vowels in a string | Java program to replace all consonants with nearest vowels in a string ; Function to replace consonant with nearest vowels ; Driver code
import java . util . * ; class solution { static String replacingConsonants ( String s ) { String str = " aaaeeeeiiiiioooooouuuuuuuu " ; char [ ] st = s . toCharArray ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int index = st [ i ] - ' a ' ; st [ i ] = str . charAt ( index ) ; } String str1 = new String ( st ) ; return str1 ; } public static void main ( String arr [ ] ) { String s = " geeksforgeeks " ; System . out . println ( replacingConsonants ( s ) ) ; } }
Replace consonants with next immediate consonants alphabetically in a String | Java program of above approach ; Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
class GFG { static boolean isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) { return false ; } return true ; } static String replaceConsonants ( char [ ] s ) { for ( int i = 0 ; i < s . length ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' z ' ) { s [ i ] = ' b ' ; } else { s [ i ] = ( char ) ( s [ i ] + 1 ) ; if ( isVowel ( s [ i ] ) ) { s [ i ] = ( char ) ( s [ i ] + 1 ) ; } } } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( replaceConsonants ( s . toCharArray ( ) ) ) ; } }
Remove characters from string that appears strictly less than K times | Java program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
class GFG { final static int MAX_CHAR = 26 ; static String removeChars ( String str , int k ) { int hash [ ] = new int [ MAX_CHAR ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; ++ i ) { hash [ str . charAt ( i ) - ' a ' ] ++ ; } String res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str . charAt ( i ) - ' a ' ] >= k ) { res += str . charAt ( i ) ; } } return res ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 2 ; System . out . println ( removeChars ( str , k ) ) ; } }
Count changes in Led Lights to display digits one by one | Java program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code
import java . io . * ; class GFG { static int countOnOff ( String n ) { int Led [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 } ; int len = n . length ( ) ; int sum = Led [ n . charAt ( 0 ) - '0' ] ; for ( int i = 1 ; i < len ; i ++ ) { sum = sum + Math . abs ( Led [ n . charAt ( i ) - '0' ] - Led [ n . charAt ( i - 1 ) - '0' ] ) ; } return sum ; } public static void main ( String args [ ] ) { String n = "082" ; System . out . println ( countOnOff ( n ) ) ; } }
Program to check if all characters have even frequency | Java implementation of the above approach ; creating a frequency array ; Finding length of s ; counting frequency of all characters ; checking if any odd frequency is there or not ; Driver Code
class GFG { static boolean check ( String s ) { int [ ] freq = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { freq [ ( s . charAt ( i ) ) - 97 ] += 1 ; } for ( int i = 0 ; i < freq . length ; i ++ ) { if ( freq [ i ] % 2 == 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " abaccaba " ; if ( check ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }