codeparrot/xlcost-text-to-code huggingface dataset:
Number of Binary Strings of length N with K adjacent Set Bits | C # program to find the number of Bit Strings of length N with K adjacent set bits ; Function to find the number of Bit Strings of length N with K adjacent set bits ; Base Case when we form bit string of length n ; if f ( bit string ) = k , count this way ; Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset ; set the bit at currentIndex ; unset the bit at currentIndex ; Driver Code ; total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )
using System ; class GFG { static int waysToKAdjacentSetBits ( int n , int k , int currentIndex , int adjacentSetBits , int lastBit ) { if ( currentIndex == n ) { if ( adjacentSetBits == k ) return 1 ; return 0 ; } int noOfWays = 0 ; if ( lastBit == 1 ) { noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits + 1 , 1 ) ; noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } else if ( lastBit != 1 ) { noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 1 ) ; noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } return noOfWays ; } public static void Main ( ) { int n = 5 , k = 2 ; int totalWays = waysToKAdjacentSetBits ( n , k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( n , k , 1 , 0 , 0 ) ; Console . WriteLine ( " Number ▁ of ▁ ways ▁ = ▁ " + totalWays ) ; } }
Sum of products of all combination taken ( 1 to n ) at a time | C # Program to find sum of all combination takne ( 1 to N ) at a time using dynamic programming ; find the postfix sum array ; modify the array such that we don 't  have to compute the products which  are obtained before ; finding sum of all combination taken 1 to N at a time ; sum taken 1 at time is simply sum of 1 - N ; for sum of products for all combination ; finding postfix array ; sum of products taken i + 1 at a time ; modify the array for overlapping problem ; Driver 's Code ; storing numbers from 1 to N ; calling allCombination
using System ; class GFG { static void postfix ( int [ ] a , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { a [ i - 1 ] = a [ i - 1 ] + a [ i ] ; } } static void modify ( int [ ] a , int n ) { for ( int i = 1 ; i < n ; i ++ ) { a [ i - 1 ] = i * a [ i ] ; } } static void allCombination ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i ; } Console . WriteLine ( " f ( 1 ) ▁ - - > ▁ " + sum ) ; for ( int i = 1 ; i < n ; i ++ ) { postfix ( a , n - i + 1 ) ; sum = 0 ; for ( int j = 1 ; j <= n - i ; j ++ ) { sum += ( j * a [ j ] ) ; } Console . WriteLine ( " f ( " + ( i + 1 ) + " ) ▁ - - > ▁ " + sum ) ; modify ( a , n ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = i + 1 ; } allCombination ( a , n ) ; } }
Count ways to reach the nth stair using step 1 , 2 or 3 | Program to find n - th stair using step size 1 or 2 or 3. ; Returns count of ways to reach n - th stair using 1 or 2 or 3 steps . ; Driver function
using System ; public class GfG { public static int findStep ( int n ) { if ( n == 1 n == 0 ) return 1 ; else if ( n == 2 ) return 2 ; else return findStep ( n - 3 ) + findStep ( n - 2 ) + findStep ( n - 1 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( findStep ( n ) ) ; } }
Partition problem | DP | A recursive C # solution for partition problem ; A utility function that returns true if there is a subset of arr [ ] with sun equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of the elements in array ; If sum is odd , there cannot be two subsets with equal sum ; Find if there is subset with sum equal to half of total sum ; Driver code ; Function call
using System ; class GFG { static bool isSubsetSum ( int [ ] arr , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( arr [ n - 1 ] > sum ) return isSubsetSum ( arr , n - 1 , sum ) ; return isSubsetSum ( arr , n - 1 , sum ) || isSubsetSum ( arr , n - 1 , sum - arr [ n - 1 ] ) ; } static bool findPartition ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; return isSubsetSum ( arr , n , sum / 2 ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 5 , 9 , 12 } ; int n = arr . Length ; if ( findPartition ( arr , n ) == true ) Console . Write ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ " + " subsets ▁ of ▁ equal ▁ sum " ) ; else Console . Write ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ " + " two ▁ subsets ▁ of ▁ equal ▁ sum " ) ; } }
Partition problem | DP | A Dynamic Programming based C # program to partition problem ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of all elements ; Initialize the part array as 0 ; Fill the partition table in bottom up manner ; The element to be included in the sum cannot be greater than the sum ; Check if sum - arr [ i ] could be formed from a subset using elements before index i ; Driver code ; Function call
using System ; class GFG { static bool findPartiion ( int [ ] arr , int n ) { int sum = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; bool [ ] part = new bool [ sum / 2 + 1 ] ; for ( i = 0 ; i <= sum / 2 ; i ++ ) { part [ i ] = false ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = sum / 2 ; j >= arr [ i ] ; j -- ) { if ( part [ j - arr [ i ] ] == true j == arr [ i ] ) part [ j ] = true ; } } return part [ sum / 2 ] ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 2 , 3 , 2 } ; int n = 6 ; if ( findPartiion ( arr , n ) == true ) Console . WriteLine ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ " + " subsets ▁ of ▁ equal ▁ sum " ) ; else Console . WriteLine ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ " + " two ▁ subsets ▁ of ▁ equal ▁ sum " ) ; } }
Check if it is possible to reach ( X , Y ) from ( 1 , 1 ) by given steps | C # program for the above approach ; Function to find the gcd of two numbers ; Base case ; Recurse ; Function to print the answer ; GCD of X and Y ; If GCD is power of 2 ; Driver code ; Given X and Y ; Function call
using System ; class GFG { public static int gcd ( int a , int b ) { if ( a < b ) { int t = a ; a = b ; b = t ; } if ( a % b == 0 ) return b ; return gcd ( b , a % b ) ; } static void printAnswer ( int x , int y ) { int val = gcd ( x , y ) ; if ( ( val & ( val - 1 ) ) == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int x = 4 ; int y = 7 ; printAnswer ( x , y ) ; } }
Find the element in the matrix generated by given rules | C # implementation of the above approach ; Function to return the element in the rth row and cth column from the required matrix ; Condition for lower half of matrix ; Condition if element is in first row ; Starting element of AP in row r ; Common difference of AP in row r ; Position of element to find in AP in row r ; Driver Code
using System ; class GFG { static int getElement ( int N , int r , int c ) { if ( r > c ) return 0 ; if ( r == 1 ) { return c ; } int a = ( r + 1 ) * ( int ) ( Math . Pow ( 2 , ( r - 2 ) ) ) ; int d = ( int ) ( Math . Pow ( 2 , ( r - 1 ) ) ) ; c = c - r ; int element = a + d * c ; return element ; } public static void Main ( String [ ] args ) { int N = 4 , R = 3 , C = 4 ; Console . WriteLine ( getElement ( N , R , C ) ) ; } }
Find smallest number formed by inserting given digit | C # program for the above approach ; Function to insert X in N and return the minimum value string ; Variable to store length of string N ; Variable to denote the position where X must be added ; If the given string N represent a negative value ; X must be place at the last index where is greater than N [ i ] ; For positive numbers , X must be placed at the last index where it is smaller than N [ i ] ; Insert X at that position ; return string ; Driver code ; given input ; function call
using System ; class GFG { static String MinValue ( string number , int x ) { int length = number . Length ; int position = length + 1 ; if ( number [ 0 ] == ' - ' ) { for ( int i = number . Length - 1 ; i >= 1 ; -- i ) { if ( ( number [ i ] - 48 ) < x ) { position = i ; } } } else { for ( int i = number . Length - 1 ; i >= 0 ; -- i ) { if ( ( number [ i ] - 48 ) > x ) { position = i ; } } } number = number . Substring ( 0 , position ) + x + number . Substring ( position , number . Length ) ; return number . ToString ( ) ; } public static void Main ( ) { string number = "89" ; int x = 1 ; Console . WriteLine ( MinValue ( number , x ) ) ; } }
Check if Decimal representation of given Binary String is divisible by K or not | C # program for above approach ; Function to check the binary number is divisible by K ; Array poweroftwo will store pow ( 2 , i ) % k ; Initializing the first element in Array ; Storing every pow ( 2 , i ) % k value in the array ; To store the remaining ; Iterating till N ; If current bit is 1 ; Updating rem ; If completely divisible ; If not Completely divisible ; Driver Code ; Given Input ; length of string s ; Function Call
using System ; class GFG { public static String divisibleByk ( String s , int n , int k ) { int [ ] poweroftwo = new int [ n ] ; poweroftwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k ; } int rem = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ n - i - 1 ] == '1' ) { rem += ( poweroftwo [ i ] ) ; rem %= k ; } } if ( rem == 0 ) { return " Yes " ; } else return " No " ; } public static void Main ( String [ ] args ) { String s = "1010001" ; int k = 9 ; int n = s . Length ; Console . Write ( divisibleByk ( s , n , k ) ) ; } }
Split a Binary String such that count of 0 s and 1 s in left and right substrings is maximum | C # program to implement the above approach ; Function to find the maximum sum of count of 0 s in the left subString and count of 1 s in the right subString by splitting the String ; Stores count of 1 s the in binary String ; Traverse the binary String ; If current character is '1' ; Update cntOne ; Stores count of 0 s ; Stores count of 1 s ; Stores maximum sum of count of 0 s and 1 s by splitting the String ; Traverse the binary String ; If current character is '0' ; Update zero ; If current character is '1' ; Update one ; Update res ; Driver Code
using System ; public class GFG { static int maxSumbySplittingString ( String str , int N ) { int cntOne = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '1' ) { cntOne ++ ; } } int zero = 0 ; int one = 0 ; int res = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( str [ i ] == '0' ) { zero ++ ; } else { one ++ ; } res = Math . Max ( res , zero + cntOne - one ) ; } return res ; } public static void Main ( String [ ] args ) { String str = "00111" ; int N = str . Length ; Console . Write ( maxSumbySplittingString ( str , N ) ) ; } }
Count removal of pairs required to be empty all Balanced Parenthesis subsequences | C # program to implement the above approach ; Function to find the maximum count of pairs required to be removed such that subsequence of string does not contain any valid parenthesis ; Stores count of pairs of balanced parenthesis ; Stores count of curly balanced parenthesis ; Stores count of small balanced parenthesis ; Stores count of square balanced parenthesis ; Iterate over characters of the string ; Update cntCurly ; Update cntSml ; Update cntSqr ; Update cntCurly ; Update cntPairs ; Update cntSml ; Update cntPairs ; Update cntSml ; Update cntPairs ; Driver Code ; Given String ; Function call
using System ; class GFG { static void cntBalancedParenthesis ( String s , int N ) { int cntPairs = 0 ; int cntCurly = 0 ; int cntSml = 0 ; int cntSqr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' { ' ) { cntCurly ++ ; } else if ( s [ i ] == ' ( ' ) { cntSml ++ ; } else if ( s [ i ] == ' [ ' ) { cntSqr ++ ; } else if ( s [ i ] == ' } ' && cntCurly > 0 ) { cntCurly -- ; cntPairs ++ ; } else if ( s [ i ] == ' ) ' && cntSml > 0 ) { cntSml -- ; cntPairs ++ ; } else if ( s [ i ] == ' ] ' && cntSqr > 0 ) { cntSqr -- ; cntPairs ++ ; } } Console . WriteLine ( cntPairs ) ; } static public void Main ( ) { String s = " { ( } ) " ; int N = s . Length ; cntBalancedParenthesis ( s , N ) ; } }
Count of strings that does not contain Arc intersection | C # program for the above approach ; Function to check if there is arc intersection or not ; Traverse the String S ; Insert all the elements in the stack one by one ; Extract the top element ; Pop out the top element ; Check if the top element is same as the popped element ; Otherwise ; If the stack is empty ; Function to check if there is arc intersection or not for the given array of Strings ; Stores count of String not having arc intersection ; Iterate through array ; Length of every String ; Function Call ; Print the desired count ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int arcIntersection ( String S , int len ) { Stack < char > stk = new Stack < char > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { stk . Push ( S [ i ] ) ; if ( stk . Count >= 2 ) { char temp = stk . Peek ( ) ; stk . Pop ( ) ; if ( stk . Peek ( ) == temp ) { stk . Pop ( ) ; } else { stk . Push ( temp ) ; } } } if ( stk . Count == 0 ) return 1 ; return 0 ; } static void countString ( String [ ] arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int len = arr [ i ] . Length ; count += arcIntersection ( arr [ i ] , len ) ; } Console . Write ( count + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { "0101" , "0011" , "0110" } ; int N = arr . Length ; countString ( arr , N ) ; } }
Check if decimal representation of Binary String is divisible by 9 or not | C # program to implement the above approach ; Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary Strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary String ; Traverse the binary String ; Stores 3 consecutive characters of the binary String ; Append octal representation of temp ; Function to check if binary String is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the String oct ; Update oddSum ; Traverse the String oct ; Update evenSum ; Stores octal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static String ConvertequivalentBase8 ( String S ) { Dictionary < String , char > mp = new Dictionary < String , char > ( ) ; mp . Add ( "000" , '0' ) ; mp . Add ( "001" , '1' ) ; mp . Add ( "010" , '2' ) ; mp . Add ( "011" , '3' ) ; mp . Add ( "100" , '4' ) ; mp . Add ( "101" , '5' ) ; mp . Add ( "110" , '6' ) ; mp . Add ( "111" , '7' ) ; int N = S . Length ; if ( N % 3 == 2 ) { S = "0" + S ; } else if ( N % 3 == 1 ) { S = "00" + S ; } N = S . Length ; String oct = " " ; for ( int i = 0 ; i < N ; i += 3 ) { String temp = S . Substring ( 0 , N ) ; if ( mp . ContainsKey ( temp ) ) oct += mp [ temp ] ; } return oct ; } static String binString_div_9 ( String S , int N ) { String oct = " " ; oct = ConvertequivalentBase8 ( S ) ; int oddSum = 0 ; int evenSum = 0 ; int M = oct . Length ; for ( int i = 0 ; i < M ; i += 2 ) oddSum += ( oct [ i ] - '0' ) ; for ( int i = 1 ; i < M ; i += 2 ) { evenSum += ( oct [ i ] - '0' ) ; } int Oct_9 = 11 ; if ( Math . Abs ( oddSum - evenSum ) % Oct_9 == 0 ) { return " Yes " ; } return " No " ; } public static void Main ( String [ ] args ) { String S = "1010001" ; int N = S . Length ; Console . WriteLine ( binString_div_9 ( S , N ) ) ; } }
Minimum cost to remove the spaces between characters of a String by rearranging the characters | C # program to gather characters of a string in minimum cost ; Function to calculate the minimum cost ; Stores the minimum cost ; Stores the count of characters found ; Stores the count of blank spaces found ; Stores the count of total characters ; If the count of characters is equal to 1 ; Iterate over the string ; Consider the previous character together with current character ; If not together already ; Add the cost to group them together ; Increase count of characters found ; Otherwise ; Increase count of spaces found ; Return the total cost obtained ; Driver Code
using System ; class GFG { static int min_cost ( String S ) { int cost = 0 ; int F = 0 ; int B = 0 ; int count = 0 ; foreach ( char c in S . ToCharArray ( ) ) if ( c == ' ▁ ' ) count ++ ; int n = S . Length - count ; if ( n == 1 ) return cost ; foreach ( char inn in S . ToCharArray ( ) ) { if ( inn != ' ▁ ' ) { if ( B != 0 ) { cost += Math . Min ( n - F , F ) * B ; B = 0 ; } F += 1 ; } else { B += 1 ; } } return cost ; } public static void Main ( String [ ] args ) { String S = " ▁ @ TABSYMBOL $ " ; Console . WriteLine ( min_cost ( S ) ) ; } }
Minimize cost to replace all the vowels of a given String by a single vowel | C # program for the above approach ; Function that return true if the given character is a vowel ; Function to return the minimum cost to convert all the vowels of a string to a single one ; Stores count of respective vowels ; Iterate through the string ; If a vowel is encountered ; Calculate the cost ; Return the minimum cost ; Driver code
using System ; class GFG { static bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } static int minCost ( String S ) { int cA = 0 ; int cE = 0 ; int cI = 0 ; int cO = 0 ; int cU = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( isVowel ( S [ i ] ) ) { cA += Math . Abs ( S [ i ] - ' a ' ) ; cE += Math . Abs ( S [ i ] - ' e ' ) ; cI += Math . Abs ( S [ i ] - ' i ' ) ; cO += Math . Abs ( S [ i ] - ' o ' ) ; cU += Math . Abs ( S [ i ] - ' u ' ) ; } } return Math . Min ( Math . Min ( Math . Min ( Math . Min ( cA , cE ) , cI ) , cO ) , cU ) ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; Console . WriteLine ( minCost ( S ) ) ; } }
Generate a string whose all K | C # program to generate a string whose substrings of length K concatenates to form given strings ; Function to return the required required string ; Iterate the given string ; Append the first character of every substring of length K ; Consider all characters from the last substring ; Driver code
using System ; class GFG { public static void decode_String ( String str , int K ) { String ans = " " ; for ( int i = 0 ; i < str . Length ; i += K ) ans += str [ i ] ; for ( int i = str . Length - ( K - 1 ) ; i < str . Length ; i ++ ) ans += str [ i ] ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int K = 3 ; String str = " abcbcscsesesesd " ; decode_String ( str , K ) ; } }
Lexicographically smallest K | C # program to find lexicographically smallest K - length substring containing maximum number of vowels ; Function that prints the lexicographically smallest K - length substring containing maximum number of vowels ; Store the length of the string ; Initialize a prefix sum array ; Loop through the string to create the prefix sum array ; Store 1 at the index if it is a vowel ; Otherwise , store 0 ; Process the prefix array ; Initialize the variable to store maximum count of vowels ; Initialize the variable to store substring with maximum count of vowels ; Loop through the prefix array ; Store the current count of vowels ; Update the result if current count is greater than maximum count ; Update lexicographically smallest substring if the current count is equal to the maximum count ; Return the result ; Driver Code
using System ; class GFG { static string maxVowelSubString ( string str , int K ) { int N = str . Length ; int [ ] pref = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' a ' str [ i ] == ' e ' str [ i ] == ' i ' str [ i ] == ' o ' str [ i ] == ' u ' ) pref [ i ] = 1 ; else pref [ i ] = 0 ; if ( i != 0 ) pref [ i ] += pref [ i - 1 ] ; } int maxCount = pref [ K - 1 ] ; string res = str . Substring ( 0 , K ) ; for ( int i = K ; i < N ; i ++ ) { int currCount = pref [ i ] - pref [ i - K ] ; if ( currCount > maxCount ) { maxCount = currCount ; res = str . Substring ( i - K + 1 , K ) ; } else if ( currCount == maxCount ) { string temp = str . Substring ( i - K + 1 , K ) ; if ( string . Compare ( temp , res ) == - 1 ) res = temp ; } } return res ; } public static void Main ( ) { string str = " ceebbaceeffo " ; int K = 3 ; Console . Write ( maxVowelSubString ( str , K ) ) ; } }
Decode the string encoded with the given algorithm | C # implementation of the approach ; Function to decode and print the original String ; To store the decoded String ; Getting the mid element ; Storing the first element of the String at the median position ; If the length is even then store the second element also ; k represents the number of characters that are already stored in the c [ ] ; If String length is odd ; If it is even ; Print the decoded String ; Driver code
using System ; class GFG { static void decodeStr ( String str , int len ) { char [ ] c = new char [ len ] ; int med , pos = 1 , k ; if ( len % 2 == 1 ) med = len / 2 ; else med = len / 2 - 1 ; c [ med ] = str [ 0 ] ; if ( len % 2 == 0 ) c [ med + 1 ] = str [ 1 ] ; if ( len % 2 == 1 ) k = 1 ; else k = 2 ; for ( int i = k ; i < len ; i += 2 ) { c [ med - pos ] = str [ i ] ; if ( len % 2 == 1 ) c [ med + pos ] = str [ i + 1 ] ; else c [ med + pos + 1 ] = str [ i + 1 ] ; pos ++ ; } for ( int i = 0 ; i < len ; i ++ ) Console . Write ( c [ i ] ) ; } public static void Main ( String [ ] args ) { String str = " ofrsgkeeeekgs " ; int len = str . Length ; decodeStr ( str , len ) ; } }
Count of distinct characters in a substring by given range for Q queries | C # program for the naive approach ; Counter to count distinct char ; Initializing frequency array to count characters as the appear in subString S [ L : R ] ; Iterating over S [ L ] to S [ R ] ; Incrementing the count of s [ i ] character in frequency array ; If frequency of any character is > 0 then increment the counter ; Driver code
using System ; class GFG { static void findCount ( String s , int L , int R ) { int distinct = 0 ; int [ ] frequency = new int [ 26 ] ; for ( int i = L ; i <= R ; i ++ ) { frequency [ s [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( frequency [ i ] > 0 ) distinct ++ ; } Console . Write ( distinct + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeksisa " + " computerscienceportal " ; int queries = 3 ; int [ , ] Q = { { 0 , 10 } , { 15 , 18 } , { 12 , 20 } } ; for ( int i = 0 ; i < queries ; i ++ ) findCount ( s , Q [ i , 0 ] , Q [ i , 1 ] ) ; } }