codeparrot/xlcost-text-to-code huggingface dataset:
Check loop in array according to given constraints | Java program to check if a given array is cyclic or not ; A simple Graph DFS based recursive function to check if there is cycle in graph with vertex v as root of DFS . Refer below article for details . https : www . geeksforgeeks . org / detect - cycle - in - a - graph / ; There is a cycle if an adjacent is visited and present in recursion call stack recur [ ] ; Returns true if arr [ ] has cycle ; Create a graph using given moves in arr [ ] ; Do DFS traversal of graph to detect cycle ; Driver Code
import java . util . Vector ; class GFG { static boolean isCycleRec ( int v , Vector < Integer > [ ] adj , Vector < Boolean > visited , Vector < Boolean > recur ) { visited . set ( v , true ) ; recur . set ( v , true ) ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( visited . elementAt ( adj [ v ] . elementAt ( i ) ) == false ) { if ( isCycleRec ( adj [ v ] . elementAt ( i ) , adj , visited , recur ) ) return true ; } else if ( visited . elementAt ( adj [ v ] . elementAt ( i ) ) == true && recur . elementAt ( adj [ v ] . elementAt ( i ) ) == true ) return true ; } recur . set ( v , false ) ; return false ; } @ SuppressWarnings ( " unchecked " ) static boolean isCycle ( int [ ] arr , int n ) { Vector < Integer > [ ] adj = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) if ( i != ( i + arr [ i ] + n ) % n && adj [ i ] != null ) adj [ i ] . add ( ( i + arr [ i ] + n ) % n ) ; Vector < Boolean > visited = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) visited . add ( true ) ; Vector < Boolean > recur = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) recur . add ( true ) ; for ( int i = 0 ; i < n ; i ++ ) if ( visited . elementAt ( i ) == false ) if ( isCycleRec ( i , adj , visited , recur ) ) return true ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , - 1 , 1 , 2 , 2 } ; int n = arr . length ; if ( isCycle ( arr , n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Queries to find the last non | Java implementation of the approach ; Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the last non - repeating character ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
public class GFG { static final int MAX = 256 ; static int freq [ ] [ ] ; static void preCalculate ( String str , int n ) { freq [ ( int ) str . charAt ( 0 ) ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str . charAt ( i ) ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } static int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } static String lastNonRepeating ( String str , int n , int l , int r ) { for ( int i = r ; i >= l ; i -- ) { char ch = str . charAt ( i ) ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ ] [ ] = { { 2 , 9 } , { 2 , 3 } , { 0 , 12 } } ; int q = queries . length ; freq = new int [ MAX ] [ n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( lastNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } } }
Queries to answer the X | Java implementation of the approach ; Function to pre - process the sub - strings in sorted order ; Generate all substrings ; Iterate to find all sub - strings ; Store the sub - string in the vector ; Sort the substrings lexicographically ; sort ( substrings . begin ( ) , substrings . end ( ) ) ; ; Driver code ; To store all the sub - strings ; Perform queries
import java . util . * ; class GFG { static void pre_process ( String substrings [ ] , String s ) { int n = s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String dup = " " ; for ( int j = i ; j < n ; j ++ ) { dup += s . charAt ( j ) ; substrings [ count ++ ] = dup ; } } int size = substrings . length ; for ( int i = 0 ; i < size - 1 ; i ++ ) { for ( int j = i + 1 ; j < substrings . length ; j ++ ) { if ( substrings [ i ] . compareTo ( substrings [ j ] ) > 0 ) { String temp = substrings [ i ] ; substrings [ i ] = substrings [ j ] ; substrings [ j ] = temp ; } } } } public static void main ( String args [ ] ) { String s = " geek " ; String substrings [ ] = new String [ 10 ] ; pre_process ( substrings , s ) ; int queries [ ] = { 1 , 5 , 10 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( substrings [ queries [ i ] - 1 ] ) ; } }
Print the final string when minimum value strings get concatenated in every operation | Java implementation of the approach ; Class that represents a pair ; Function that prints the final string ; Add all the strings and their corresponding values to the priority queue ; Take those two strings from the priority queue whose corresponding integer values are smaller and add them up as well as their values and add them back to the priority queue while there are more than a single element in the queue ; Get the minimum valued string ; Get the second minimum valued string ; Updated integer value ; Create new entry for the queue ; Add to the queue ; Print the only remaining string ; Driver code
import java . util . * ; import java . lang . * ; class Priority { String s ; int count ; } class PQ implements Comparator < Priority > { public int compare ( Priority a , Priority b ) { if ( a . count > b . count ) return 1 ; else if ( a . count < b . count ) return - 1 ; return 0 ; } } class GFG { static void FindString ( String str [ ] , int num [ ] , int n ) { Comparator < Priority > comparator = new PQ ( ) ; PriorityQueue < Priority > p = new PriorityQueue < Priority > ( comparator ) ; for ( int i = 0 ; i < n ; i ++ ) { Priority x = new Priority ( ) ; x . s = str [ i ] ; x . count = num [ i ] ; p . add ( x ) ; } while ( p . size ( ) > 1 ) { Priority x = p . poll ( ) ; p . remove ( x ) ; Priority y = p . poll ( ) ; p . remove ( y ) ; int temp = x . count + y . count ; String sb = x . s + y . s ; Priority z = new Priority ( ) ; z . count = temp ; z . s = sb ; p . add ( z ) ; } Priority z = p . poll ( ) ; System . out . println ( z . s ) ; } public static void main ( String [ ] args ) { String str [ ] = { " Geeks " , " For " , " Geeks " } ; int num [ ] = { 2 , 3 , 7 } ; int n = num . length ; FindString ( str , num , n ) ; } }
Minimum cost to modify a string | Java program to implement the above approach ; Function to return the minimum cost ; Initialize result ; To store the frequency of characters of the string ; Initialize array with 0 ; Update the frequencies of the characters of the string ; Loop to check all windows from a - z where window size is K ; Starting index of window ; Ending index of window ; Check if the string contains character ; Check if the character is on left side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Check if the character is on right side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Find the minimum of all costs for modifying the string ; Loop to check all windows Here window contains characters before z and after z of window size K ; Starting index of window ; Ending index of window ; Check if the string contains character ; If characters are outside window find the cost for modifying character add value to count ; Find the minimum of all costs for modifying the string ; Driver code
import java . util . Arrays ; class GFG { static int minCost ( String str , int K ) { int n = str . length ( ) ; int res = 999999999 , count = 0 , a , b ; int cnt [ ] = new int [ 27 ] ; Arrays . fill ( cnt , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ str . charAt ( i ) - ' a ' + 1 ] ++ ; for ( int i = 1 ; i < ( 26 - K + 1 ) ; i ++ ) { a = i ; b = i + K ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= a && j >= b ) count = count + ( Math . min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] ; else if ( j <= a && j <= b ) count = count + ( Math . min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } for ( int i = 26 - K + 1 ; i <= 26 ; i ++ ) { a = i ; b = ( i + K ) % 26 ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= b && j <= a ) count = count + ( Math . min ( j - b , a - j ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } return res ; } public static void main ( String [ ] args ) { String str = " abcdefghi " ; int K = 2 ; System . out . println ( minCost ( str , K ) ) ; } }
Generate all binary strings of length n with sub | Java implementation of the above approach ; Utility function to print the given binary string ; This function will be called recursively to generate the next bit for given binary string according to its current state ; Base - case : if the generated binary string meets the required length and the pattern "01" appears twice ; nextbit needs to be 0 because each time we call the function recursively , we call 2 times for 2 cases : next bit is 0 or 1 The is to assure that the binary string is printed one time only ; Generate the next bit for str and call recursive ; Assign first bit ; The next generated bit will wither be 0 or 1 ; } else If pattern "01" occurrence is < 2 ; Set next bit ; If pattern "01" appears then increase the occurrence of pattern ; If previous bit is 0 then next bit cannot be 1 ; Otherwise ; Driver code ; Length of the resulting strings must be at least 4 ; Generate all binary strings of length n with sub - string "01" appearing twice
class GFG { static void printBinStr ( int [ ] str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( str [ i ] ) ; } System . out . println ( ) ; } static void generateBinStr ( int [ ] str , int len , int currlen , int occur , int nextbit ) { if ( currlen == len ) { if ( occur == 2 && nextbit == 0 ) { printBinStr ( str , len ) ; } return ; } if ( currlen == 0 ) { str [ 0 ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; if ( occur < 2 ) { str [ currlen ] = nextbit ; if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { occur += 1 ; } generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { return ; } else { str [ currlen ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } } public static void main ( String [ ] args ) { int n = 5 ; if ( n < 4 ) { System . out . print ( - 1 ) ; } else { int [ ] str = new int [ n ] ; generateBinStr ( str , n , 0 , 0 , 0 ) ; generateBinStr ( str , n , 0 , 0 , 1 ) ; } } }
Print last character of each word in a string | Java implementation of the approach ; Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code
class GFG { static void printLastChar ( String str ) { str = str + " ▁ " ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) System . out . print ( str . charAt ( i - 1 ) + " ▁ " ) ; } } public static void main ( String s [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( str ) ; } }
Maximum length of balanced string after swapping and removal of characters | Java implementation of the approach ; Function to return the length of the longest balanced sub - string ; To store the count of parentheses ; Traversing the string ; Check type of parentheses and incrementing count for it ; Sum all pair of balanced parentheses ; Driven code
class GFG { static int maxBalancedStr ( String s ) { int open1 = 0 , close1 = 0 ; int open2 = 0 , close2 = 0 ; int open3 = 0 , close3 = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { switch ( s . charAt ( i ) ) { case ' ( ' : open1 ++ ; break ; case ' ) ' : close1 ++ ; break ; case ' { ' : open2 ++ ; break ; case ' } ' : close2 ++ ; break ; case ' [ ' : open3 ++ ; break ; case ' ] ' : close3 ++ ; break ; } } int maxLen = 2 * Math . min ( open1 , close1 ) + 2 * Math . min ( open2 , close2 ) + 2 * Math . min ( open3 , close3 ) ; return maxLen ; } public static void main ( String [ ] args ) { String s = " ) ) [ ] ] ( ( " ; System . out . println ( maxBalancedStr ( s ) ) ; } }
Bitwise OR of N binary strings | Java implementation of the approach ; Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Utility reverse ; Driver code
class GFG { static String strBitwiseOR ( String [ ] arr , int n ) { String res = " " ; int max_size = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max_size = Math . max ( max_size , ( int ) arr [ i ] . length ( ) ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_size - arr [ i ] . length ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_size ; i ++ ) { int curr_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] . charAt ( i ) - '0' ) ; res += ( char ) ( curr_bit + '0' ) ; } res = reverse ( res ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { public static void main ( String [ ] args ) { String arr [ ] = { "10" , "11" , "1000001" } ; int n = arr . length ; System . out . println ( strBitwiseOR ( arr , n ) ) ; } }
Minimum cost to make two strings same | Java implementation of the approach ; Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code
class GFG { static int findCost ( String s1 , String s2 , int a , int b , int c , int d , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 . charAt ( i ) == s2 . charAt ( i ) ) continue ; else { if ( ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '2' ) || ( s2 . charAt ( i ) == '1' && s1 . charAt ( i ) == '2' ) ) cost += Math . min ( d , Math . min ( a , b + c ) ) ; else if ( ( s1 . charAt ( i ) == '2' && s2 . charAt ( i ) == '3' ) || ( s2 . charAt ( i ) == '2' && s1 . charAt ( i ) == '3' ) ) cost += Math . min ( d , Math . min ( b , a + c ) ) ; else if ( ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '3' ) || ( s2 . charAt ( i ) == '1' && s1 . charAt ( i ) == '3' ) ) cost += Math . min ( d , Math . min ( c , a + b ) ) ; } } return cost ; } public static void main ( String [ ] args ) { String s1 = "121" ; String s2 = "223" ; int a = 2 , b = 3 , c = 4 , d = 10 ; int n = s1 . length ( ) ; System . out . println ( findCost ( s1 , s2 , a , b , c , d , n ) ) ; } }
Count the number of common divisors of the given strings | Java implementation of the approach ; Function that returns true if sub - string s [ 0. . . k ] is repeated a number of times to generate String s ; Function to return the count of common divisors ; If the length of the sub - string divides length of both the strings ; If prefixes match in both the strings ; If both the strings can be generated ; Driver code
class GFG { static boolean check ( String s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i % k ) ) { return false ; } } return true ; } static int countCommonDivisors ( String a , String b ) { int ct = 0 ; int n = a . length ( ) , m = b . length ( ) ; for ( int i = 1 ; i <= Math . min ( n , m ) ; i ++ ) { if ( n % i == 0 && m % i == 0 ) { if ( a . substring ( 0 , i ) . equals ( b . substring ( 0 , i ) ) ) { if ( check ( a , i ) && check ( b , i ) ) { ct ++ ; } } } } return ct ; } public static void main ( String [ ] args ) { String a = " xaxa " , b = " xaxaxaxa " ; System . out . println ( countCommonDivisors ( a , b ) ) ; } }
Level order traversal line by line | Set 3 ( Using One Queue ) | Java program to do level order traversal line by line ; A Binary Tree Node ; Prints level order traversal line by line using two queues . ; Create an empty queue for level order traversal ; Pushing root node into the queue . ; Pushing delimiter into the queue . ; Executing loop till queue becomes empty ; condition to check the occurence of next level ; Pushing left child current node ; Pushing right child current node ; Driver function ; Let us create binary tree shown above
import java . util . LinkedList ; import java . util . Queue ; public class GFG { static class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static void levelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { Node curr = q . poll ( ) ; if ( curr == null ) { if ( ! q . isEmpty ( ) ) { q . add ( null ) ; System . out . println ( ) ; } } else { if ( curr . left != null ) q . add ( curr . left ) ; if ( curr . right != null ) q . add ( curr . right ) ; System . out . print ( curr . data + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; levelOrder ( root ) ; } }
Convert given string so that it holds only distinct characters | Java implementation of the approach ; Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ as ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Return the modified string ; Driver code
class GFG { static int nextZero ( int i , int occurrences [ ] ) { while ( i < occurrences . length ) { if ( occurrences [ i ] == 0 ) return i ; i ++ ; } return - 1 ; } static String getModifiedString ( String str ) { int n = str . length ( ) ; if ( n > 26 ) return " - 1" ; char ch [ ] = str . toCharArray ( ) ; int i , occurrences [ ] = new int [ 26 ] ; for ( i = 0 ; i < n ; i ++ ) occurrences [ ch [ i ] - ' a ' ] ++ ; int index = nextZero ( 0 , occurrences ) ; for ( i = 0 ; i < n ; i ++ ) { if ( occurrences [ ch [ i ] - ' a ' ] > 1 ) { occurrences [ ch [ i ] - ' a ' ] -- ; ch [ i ] = ( char ) ( ' a ' + index ) ; occurrences [ index ] = 1 ; index = nextZero ( index + 1 , occurrences ) ; } } return String . valueOf ( ch ) ; } public static void main ( String arr [ ] ) { String str = " geeksforgeeks " ; System . out . print ( getModifiedString ( str ) ) ; } }
Replace all occurrences of a string with space | Java implementation to extract the secret message ; Function to extract the secret message ; Replacing all occurrences of Sub in Str by empty spaces ; Removing unwanted spaces in the start and end of the string ; Driver code
import java . io . * ; import java . util . * ; class GFG { static String extractSecretMessage ( String Str , String Sub ) { Str = Str . replaceAll ( Sub , " ▁ " ) ; Str = Str . trim ( ) ; return Str ; } public static void main ( String args [ ] ) { String Str = " LIELIEILIEAMLIECOOL " ; String Sub = " LIE " ; System . out . println ( extractSecretMessage ( Str , Sub ) ) ; } }
Sub | Java implementation of the approach ; Function to return the count of sub - strings starting from startIndex that are also the prefixes of str ; Function to return the count of all possible sub - strings of str that are also the prefixes of str ; If current character is equal to the starting character of str ; Driver code ; Function Call
public class GFG { public static int subStringsStartingHere ( String str , int n , int startIndex ) { int count = 0 , i = startIndex + 1 ; while ( i <= n ) { if ( str . startsWith ( str . substring ( startIndex , i ) ) ) { count ++ ; } else break ; i ++ ; } return count ; } public static int countSubStrings ( String str , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( 0 ) ) count += subStringsStartingHere ( str , n , i ) ; } return count ; } public static void main ( String [ ] args ) { String str = " ababc " ; int n = str . length ( ) ; System . out . println ( countSubStrings ( str , n ) ) ; } }
Binary Search a String | Java program to implement Binary Search for strings ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver method to test above
class GFG { static int binarySearch ( String [ ] arr , String x ) { int l = 0 , r = arr . length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; int res = x . compareTo ( arr [ m ] ) ; if ( res == 0 ) return m ; if ( res > 0 ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { String [ ] arr = { " contribute " , " geeks " , " ide " , " practice " } ; String x = " ide " ; int result = binarySearch ( arr , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }
Count characters in a string whose ASCII values are prime | Java implementation of above approach ; Function to find prime characters in the String ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a Boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; initialize the value ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all the characters ; Driver program ; print required answer
class Solution { static final int max_val = 257 ; static int PrimeCharacters ( String s ) { boolean prime [ ] = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( prime [ ( int ) ( s . charAt ( i ) ) ] ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " geeksforgeeks " ; System . out . print ( PrimeCharacters ( S ) ) ; } }