codeparrot/xlcost-text-to-code huggingface dataset:
Count of replacements required to make the sum of all Pairs of given type from the Array equal | Java program implement the above approach ; Function to find the minimum replacements required ; Stores the maximum and minimum values for every pair of the form arr [ i ] , arr [ n - i - 1 ] ; Map for storing frequencies of every sum formed by pairs ; Minimum element in the pair ; Maximum element in the pair ; Incrementing the frequency of sum encountered ; Insert minimum and maximum values ; Sorting the vectors ; Iterate over all possible values of x ; Count of pairs for which x > x + k ; Count of pairs for which x < mn + 1 ; Count of pairs requiring 2 replacements ; Count of pairs requiring no replacements ; Count of pairs requiring 1 replacement ; Update the answer ; Return the answer ; Driver Code
import java . util . * ; import java . io . * ; class GFG { static int inf = ( int ) 1e18 ; static int minimumReplacement ( int [ ] arr , int N , int K ) { int ans = inf ; ArrayList < Integer > max_values = new ArrayList < > ( ) ; ArrayList < Integer > min_values = new ArrayList < > ( ) ; Map < Integer , Integer > sum_equal_to_x = new HashMap < > ( ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int mn = Math . min ( arr [ i ] , arr [ N - i - 1 ] ) ; int mx = Math . max ( arr [ i ] , arr [ N - i - 1 ] ) ; sum_equal_to_x . put ( arr [ i ] + arr [ N - i - 1 ] , sum_equal_to_x . getOrDefault ( arr [ i ] + arr [ N - i - 1 ] , 0 ) + 1 ) ; min_values . add ( mn ) ; max_values . add ( mx ) ; } Collections . sort ( max_values ) ; Collections . sort ( min_values ) ; for ( int x = 2 ; x <= 2 * K ; x ++ ) { int mp1 = max_values . indexOf ( x - K ) ; int mp2 = min_values . indexOf ( x ) ; int rep2 = mp1 + ( N / 2 - mp2 ) ; int rep0 = sum_equal_to_x . getOrDefault ( x , - 1 ) ; int rep1 = ( N / 2 - rep2 - rep0 ) ; ans = Math . min ( ans , rep2 * 2 + rep1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 3 ; int arr [ ] = { 1 , 2 , 2 , 1 } ; System . out . print ( minimumReplacement ( arr , N , K ) ) ; } }
Print the Forests of a Binary Tree after removal of given Nodes | Java Program to implement the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code
import java . util . * ; class GFG { static HashMap < Integer , Boolean > mp = new HashMap < > ( ) ; static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static boolean deleteNode ( int nodeVal ) { return mp . containsKey ( nodeVal ) ; } static Node treePruning ( Node root , Vector < Node > result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . add ( root . left ) ; } if ( root . right != null ) { result . add ( root . right ) ; } return null ; } return root ; } static void printInorderTree ( Node root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; printInorderTree ( root . right ) ; } static void printForests ( Node root , int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , true ) ; } Vector < Node > result = new Vector < > ( ) ; if ( treePruning ( root , result ) != null ) result . add ( root ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { printInorderTree ( result . get ( i ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; int arr [ ] = { 14 , 23 , 1 } ; int n = arr . length ; printForests ( root , arr , n ) ; } }
Count of Ways to obtain given Sum from the given Array elements | Java Program to implement the above approach ; Function to call dfs to calculate the number of ways ; If target + sum is odd or S exceeds sum ; No sultion exists ; Return the answer ; Driver Code
import java . io . * ; class GFG { static int knapSack ( int [ ] nums , int S ) { int sum = 0 ; for ( int i : nums ) sum += i ; if ( sum < S || - sum > - S || ( S + sum ) % 2 == 1 ) return 0 ; int [ ] dp = new int [ ( S + sum ) / 2 + 1 ] ; dp [ 0 ] = 1 ; for ( int num : nums ) { for ( int i = dp . length - 1 ; i >= num ; i -- ) { dp [ i ] += dp [ i - num ] ; } } return dp [ dp . length - 1 ] ; } public static void main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = knapSack ( arr , S ) ; System . out . println ( answer ) ; } }
Maximum distance between two elements whose absolute difference is K | Java program for the above approach ; Function that find maximum distance between two elements whose absolute difference is K ; To store the first index ; Traverse elements and find maximum distance between 2 elements whose absolute difference is K ; If this is first occurrence of element then insert its index in map ; If next element present in map then update max distance ; If previous element present in map then update max distance ; Return the answer ; Driver Code ; Given array arr [ ] ; Given difference K ; Function call
import java . io . * ; import java . util . * ; class GFG { static int maxDistance ( int [ ] arr , int K ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int maxDist = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ! map . containsKey ( arr [ i ] ) ) map . put ( arr [ i ] , i ) ; if ( map . containsKey ( arr [ i ] + K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] + K ) ) ; } if ( map . containsKey ( arr [ i ] - K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] - K ) ) ; } } if ( maxDist == 0 ) return - 1 ; else return maxDist ; } public static void main ( String args [ ] ) { int [ ] arr = { 11 , 2 , 3 , 8 , 5 , 2 } ; int K = 2 ; System . out . println ( maxDistance ( arr , K ) ) ; } }
Count of Substrings with at least K pairwise Distinct Characters having same Frequency | Java program for the above approach ; Function to find the subString with K pairwise distinct characters and with same frequency ; Stores the occurrence of each character in the subString ; Length of the String ; Iterate over the String ; Set all values at each index to zero ; Stores the count of unique characters ; Moving the subString ending at j ; Calculate the index of character in frequency array ; Increment the frequency ; Update the maximum index ; Check for both the conditions ; Return the answer ; Driver Code ; Function call
import java . util . * ; class GFG { static int no_of_subString ( String s , int N ) { int fre [ ] = new int [ 26 ] ; int str_len ; str_len = ( int ) s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < str_len ; i ++ ) { Arrays . fill ( fre , 0 ) ; int max_index = 0 ; int dist = 0 ; for ( int j = i ; j < str_len ; j ++ ) { int x = s . charAt ( j ) - ' a ' ; if ( fre [ x ] == 0 ) dist ++ ; fre [ x ] ++ ; max_index = Math . max ( max_index , fre [ x ] ) ; if ( dist >= N && ( ( max_index * dist ) == ( j - i + 1 ) ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s = " abhay " ; int N = 3 ; System . out . print ( no_of_subString ( s , N ) ) ; } }
Count of Ordered Pairs ( X , Y ) satisfying the Equation 1 / X + 1 / Y = 1 / N | Java program for the above approach ; Function to find number of ordered positive integer pairs ( x , y ) such that they satisfy the equation ; Initialize answer variable ; Iterate over all possible values of y ; For valid x and y , ( n * n ) % ( y - n ) has to be 0 ; Increment count of ordered pairs ; Print the answer ; Driver Code ; Function call
class GFG { static void solve ( int n ) { int ans = 0 ; for ( int y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int n = 5 ; solve ( n ) ; } }
Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | Java program to implement the above approach ; Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver Code ; Desigining the tree ; Counter counts no . of consecutive nodes
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] adj = new Vector [ 100005 ] ; static int [ ] visited = new int [ 100005 ] ; static int ans = 0 ; static void dfs ( int node , int count , int m , int arr [ ] , int k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . size ( ) == 1 && node != 1 ) { ans ++ ; } for ( int x : adj [ node ] ) { if ( visited [ x ] == 0 ) { dfs ( x , count , m , arr , k ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 3 , 2 , 1 , 2 , 1 } ; int N = 7 , K = 2 , M = 2 ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 1 ] . add ( 2 ) ; adj [ 2 ] . add ( 1 ) ; adj [ 1 ] . add ( 3 ) ; adj [ 3 ] . add ( 1 ) ; adj [ 2 ] . add ( 4 ) ; adj [ 4 ] . add ( 2 ) ; adj [ 2 ] . add ( 5 ) ; adj [ 5 ] . add ( 2 ) ; adj [ 3 ] . add ( 6 ) ; adj [ 6 ] . add ( 3 ) ; adj [ 3 ] . add ( 7 ) ; adj [ 7 ] . add ( 3 ) ; int counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; System . out . print ( ans + "NEW_LINE"); } }
Count of N | Java Program to implement the above approach ; Function to calculate and return the reverse of the number ; Function to calculate the total count of N - digit numbers satisfying the necessary conditions ; Initialize two variables ; Calculate the sum of odd and even positions ; Check for divisibility ; Return the answer ; Driver Code
class GFG { public static long reverse ( long num ) { long rev = 0 ; while ( num > 0 ) { int r = ( int ) ( num % 10 ) ; rev = rev * 10 + r ; num /= 10 ; } return rev ; } public static long count ( int N , int A , int B ) { long l = ( long ) Math . pow ( 10 , N - 1 ) , r = ( long ) Math . pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; long ans = 0 ; for ( long i = l ; i <= r ; i ++ ) { int even_sum = 0 , odd_sum = 0 ; long itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += num % 10 ; else even_sum += num % 10 ; num /= 10 ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , A = 5 , B = 3 ; System . out . println ( count ( N , A , B ) ) ; } }
Distance Traveled by Two Trains together in the same Direction | Java program to find the distance traveled together by the two trains ; Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int calc_distance ( int A [ ] , int B [ ] , int n ) { int distance_traveled_A = 0 ; int distance_traveled_B = 0 ; int answer = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { distance_traveled_A += A [ i ] ; distance_traveled_B += B [ i ] ; if ( ( distance_traveled_A == distance_traveled_B ) && ( A [ i ] == B [ i ] ) ) { answer += A [ i ] ; } } return answer ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 2 , 4 } ; int B [ ] = { 2 , 1 , 3 , 1 , 4 } ; int N = A . length ; System . out . println ( calc_distance ( A , B , N ) ) ; } }
Sum of Nodes and respective Neighbors on the path from root to a vertex V | Java Program to implement the above approach ; Creating Adjacency list ; Function to perform DFS traversal ; Initializing parent of each node to p ; Iterate over the children ; Function to add values of children to respective parent nodes ; Root node ; Function to return sum of values of each node in path from V to the root ; Path from node V to root node ; Driver Code ; Insert edges into the graph ; Values assigned to each vertex ; Constructing the tree using adjacency list ; Parent array ; store values in the parent array ; Add values of children to the parent ; Find sum of nodes lying in the path ; Add root node since its value is not included yet
import java . io . * ; import java . util . * ; class GFG { private static List < List < Integer > > constructTree ( int n , int [ ] [ ] edges ) { List < List < Integer > > adjl = new ArrayList < List < Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adjl . add ( new ArrayList < Integer > ( ) ) ; } for ( int [ ] e : edges ) { int u = e [ 0 ] ; int v = e [ 1 ] ; adjl . get ( u ) . add ( v ) ; adjl . get ( v ) . add ( u ) ; } return adjl ; } private static void DFS ( List < List < Integer > > adjl , int [ ] parent , int u , int p ) { parent [ u ] = p ; for ( int v : adjl . get ( u ) ) { if ( v != p ) { DFS ( adjl , parent , v , u ) ; } } } private static int [ ] valuesFromChildren ( int [ ] parent , int [ ] values ) { int [ ] valuesChildren = new int [ parent . length ] ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == - 1 ) continue ; else { int p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } private static int findSumOfValues ( int v , int [ ] parent , int [ ] valuesChildren ) { int cur_node = v ; int sum = 0 ; while ( cur_node != - 1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 8 ; int [ ] [ ] edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 4 , 7 } , { 3 , 6 } } ; int v = 7 ; int [ ] values = new int [ ] { 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 } ; List < List < Integer > > adjl = constructTree ( n , edges ) ; int [ ] parent = new int [ n ] ; DFS ( adjl , parent , 0 , - 1 ) ; int [ ] valuesChildren = valuesFromChildren ( parent , values ) ; int sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; System . out . println ( sum ) ; } }
Find a pair in Array with second largest product | Java program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Driver Code ; Given array ; Function Call
class GFG { static void maxProduct ( int arr [ ] , int N ) { if ( N < 3 ) { return ; } int a = arr [ 0 ] , b = arr [ 1 ] ; int c = 0 , d = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a ; d = b ; a = arr [ i ] ; b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] ; d = arr [ j ] ; } System . out . println ( c + " ▁ " + d ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . length ; maxProduct ( arr , N ) ; } }
Count of substrings consisting of even number of vowels | Java Program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings ; If the current character is a vowel ; Increase count ; If substring contains even number of vowels ; Increase the answer ; Print the final answer ; Driver Code
class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static void countSubstrings ( String s , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( s . charAt ( j ) ) ) { count ++ ; } if ( count % 2 == 0 ) result ++ ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int n = 5 ; String s = " abcde " ; countSubstrings ( s , n ) ; } }
Count of substrings consisting of even number of vowels | Java Program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static void countSubstrings ( String s , int n ) { int temp [ ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s . charAt ( i ) ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { int n = 5 ; String s = " abcde " ; countSubstrings ( s , n ) ; } }
Find the Level of a Binary Tree with Width K | Java program to implement the above approach ; Structure of binary tree node ; Function to create new node ; Function returns required level of width k , if found else - 1 ; To store the node and the label and perform traversal ; Taking the last label of each level of the tree ; Check width of current level ; If the width is equal to k then return that level ; Taking the first label of each level of the tree ; If any level does not has width equal to k , return - 1 ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class pair { Node first ; int second ; pair ( Node first , int second ) { this . first = first ; this . second = second ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static int findLevel ( Node root , int k , int level ) { Queue < pair > qt = new LinkedList < > ( ) ; qt . add ( new pair ( root , 0 ) ) ; int count = 1 , b = 0 , a = 0 ; while ( ! qt . isEmpty ( ) ) { pair temp = qt . peek ( ) ; qt . poll ( ) ; if ( count == 1 ) { b = temp . second ; } if ( temp . first . left != null ) { qt . add ( new pair ( temp . first . left , 2 * temp . second ) ) ; } if ( temp . first . right != null ) { qt . add ( new pair ( temp . first . right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( ( b - a + 1 ) == k ) return level ; pair secondLabel = qt . peek ( ) ; a = secondLabel . second ; level += 1 ; count = qt . size ( ) ; } } return - 1 ; } public static void main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . left . right . right = newNode ( 4 ) ; int k = 4 ; System . out . println ( findLevel ( root , k , 1 ) ) ; } }
Maximize median after doing K addition operation on the Array | Java program for the above approach ; Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Driver code ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call
import java . util . * ; class GFG { static boolean possible ( int arr [ ] , int N , int mid , int K ) { int add = 0 ; for ( int i = N / 2 - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } static int findMaxMedian ( int arr [ ] , int N , int K ) { int low = 1 ; int mx = 0 ; for ( int i = 0 ; i < N ; ++ i ) { mx = Math . max ( mx , arr [ i ] ) ; } int high = K + mx ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ N / 2 ] ) { return ( arr [ N / 2 ] + low - 1 ) / 2 ; } } return low - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 6 } ; int K = 10 ; int N = arr . length ; Arrays . sort ( arr ) ; System . out . println ( findMaxMedian ( arr , N , K ) ) ; } }
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | Java program to implement the above approach ; Function that returns true if the count of elements is less than mid ; To store count of elements less than mid ; Loop through each row ; Count elements less than mid in the ith row ; Function that returns the Kth smallest element in the NxM Matrix after sorting in an array ; Initialize low and high ; Perform binary search ; Find the mid ; Check if the count of elements is less than mid ; Return Kth smallest element of the matrix ; Driver code
class GFG { public static boolean countLessThanMid ( int mid , int N , int M , int K ) { int count = 0 ; for ( int i = 1 ; i <= Math . min ( N , mid ) ; ++ i ) { count = count + Math . min ( mid / i , M ) ; } if ( count >= K ) return false ; else return true ; } public static int findKthElement ( int N , int M , int K ) { int low = 1 , high = N * M ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; int K = 5 ; System . out . println ( findKthElement ( N , M , K ) ) ; } }
Count of substrings containing only the given character | Java program for the above approach ; Function that finds the count of substrings containing only character C in the string S ; To store total count of substrings ; To store count of consecutive C 's ; Loop through the string ; Increase the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Reset the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Print the count of sub - strings containing only C ; Driver Code
import java . util . * ; class GFG { static void countSubString ( String S , char C ) { int count = 0 ; int conCount = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { char ch = S . charAt ( i ) ; if ( ch == C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; System . out . println ( count ) ; } public static void main ( String s [ ] ) { String S = " geeksforgeeks " ; char C = ' e ' ; countSubString ( S , C ) ; } }