codeparrot/xlcost-text-to-code huggingface dataset:
Minimum and Maximum values of an expression with * and + | C # program to get maximum and minimum values of an expression ; Utility method to check whether a character is operator or not ; method prints minimum and maximum value obtainable from an expression ; store operator and numbers in different vectors ; storing last number in vector ; initializing minval and maxval 2D array ; initializing main diagonal by num values ; looping similar to matrix chain multiplication and updating both 2D arrays ; if current operator is ' + ' , updating tmp variable by addition ; if current operator is ' * ' , updating tmp variable by multiplication ; updating array values by tmp variables ; last element of first row will store the result ; Driver code to test above methods
using System ; using System . Collections . Generic ; public class GFG { static bool isOperator ( char op ) { return ( op == ' + ' op == ' * ' ) ; } static void printMinAndMaxValueOfExp ( string exp ) { List < int > num = new List < int > ( ) ; List < char > opr = new List < char > ( ) ; string tmp = " " ; for ( int i = 0 ; i < exp . Length ; i ++ ) { if ( isOperator ( exp [ i ] ) ) { opr . Add ( exp [ i ] ) ; num . Add ( int . Parse ( tmp ) ) ; tmp = " " ; } else { tmp += exp [ i ] ; } } num . Add ( int . Parse ( tmp ) ) ; int len = num . Count ; int [ , ] minVal = new int [ len , len ] ; int [ , ] maxVal = new int [ len , len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { minVal [ i , j ] = Int32 . MaxValue ; maxVal [ i , j ] = 0 ; if ( i == j ) { minVal [ i , j ] = maxVal [ i , j ] = num [ i ] ; } } } for ( int L = 2 ; L <= len ; L ++ ) { for ( int i = 0 ; i < len - L + 1 ; i ++ ) { int j = i + L - 1 ; for ( int k = i ; k < j ; k ++ ) { int minTmp = 0 , maxTmp = 0 ; if ( opr [ k ] == ' + ' ) { minTmp = minVal [ i , k ] + minVal [ k + 1 , j ] ; maxTmp = maxVal [ i , k ] + maxVal [ k + 1 , j ] ; } else if ( opr [ k ] == ' * ' ) { minTmp = minVal [ i , k ] * minVal [ k + 1 , j ] ; maxTmp = maxVal [ i , k ] * maxVal [ k + 1 , j ] ; } if ( minTmp < minVal [ i , j ] ) minVal [ i , j ] = minTmp ; if ( maxTmp > maxVal [ i , j ] ) maxVal [ i , j ] = maxTmp ; } } } Console . Write ( " Minimum ▁ value ▁ : ▁ " + minVal [ 0 , len - 1 ] + " , ▁ Maximum ▁ value ▁ : ▁ " + maxVal [ 0 , len - 1 ] ) ; } static public void Main ( ) { string expression = "1 + 2*3 + 4*5" ; printMinAndMaxValueOfExp ( expression ) ; } }
Matrix Chain Multiplication | DP | C # code for naive recursive implementation that simply follows the above optimal substructure property ; Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; place parenthesis at different places between first and last matrix , recursively calculate count of multiplications for each parenthesis placement and return the minimum count ; Return minimum count ; Driver code
using System ; class GFG { static int MatrixChainOrder ( int [ ] p , int i , int j ) { if ( i == j ) return 0 ; int min = int . MaxValue ; for ( int k = i ; k < j ; k ++ ) { int count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count ; } return min ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 3 } ; int n = arr . Length ; Console . Write ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , 1 , n - 1 ) ) ; } }
Numbers formed by flipping common set bits in two given integers | C # program to implement the above approach ; Function to flip bits of A and B which are set in both of them ; Clear the bits of A which are set in both A and B ; Clear the bits of B which are set in both A and B ; Print updated A and B ; Driver Code
using System ; class GFG { static void flipBitsOfAandB ( int A , int B ) { A = A ^ ( A & B ) ; B = B ^ ( A & B ) ; Console . Write ( A + " ▁ " + B ) ; } public static void Main ( String [ ] args ) { int A = 10 , B = 20 ; flipBitsOfAandB ( A , B ) ; } }
Sum of Hamming difference of consecutive numbers from 0 to N | Set 2 | C # Program to implement the above approach ; Function to calculate and return the hamming distance between all consecutive numbers from 0 to N ; Driver Code
using System ; class GFG { static int TotalHammingDistance ( int n ) { int i = 1 , sum = 0 ; while ( n / i > 0 ) { sum = sum + n / i ; i = i * 2 ; } return sum ; } public static void Main ( ) { int N = 9 ; Console . Write ( TotalHammingDistance ( N ) ) ; } }
Sum of all divisors from 1 to N | Set 3 | C # program to implement the above approach ; Function to find the sum of all divisors of all numbers from 1 to N ; Stores the sum ; Marks the last point of occurence with same count ; Calculate the sum ; Return the result ; Driver Code
using System ; class GFG { static readonly int m = 1000000007 ; static void solve ( long n ) { long s = 0 ; for ( int l = 1 ; l <= n ; ) { int r = ( int ) ( n / ( Math . Floor ( ( double ) n / l ) ) ) ; int x = ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; int y = ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; int p = ( int ) ( ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } Console . Write ( ( s + m ) % m ) ; } public static void Main ( String [ ] args ) { long n = 12 ; solve ( n ) ; } }
Minimize number of cuts required to break N length stick into N unit length sticks | C # program to find minimum time required to split a stick of N length into unit pieces ; Function to return the minimum time required to split stick of N into length into unit pieces ; Return the minimum unit of time required ; Driver Code
using System ; class GFG { static int min_time_to_cut ( int N ) { if ( N == 0 ) return 0 ; return ( int ) Math . Ceiling ( Math . Log ( N ) / Math . Log ( 2 ) ) ; } public static void Main ( ) { int N = 100 ; Console . Write ( min_time_to_cut ( N ) ) ; } }
Count of distinct pair sum between two 1 to N value Arrays | C # implementation to count of distinct pair sum between two Array with values 1 to N ; Function to find the distinct sums ; Set to store distinct sums ; Inserting every sum ; Returning distinct sums ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findDistinctSums ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { s . Add ( i + j ) ; } } return s . Count ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . Write ( findDistinctSums ( N ) ) ; } }
Print Triangle separated pattern | C # program to print the triangle separated pattern using star and slash character ; Function to print pattern recursively ; Base Case ; Conditions to print slash ; Condition to print forword slash ; Condition to print backward slash ; Else print ' * ' ; Recursive call for rows ; Recursive call for changing the rows ; Driver Code ; Function Call
using System ; class GFG { static int printPattern ( int i , int j , int n ) { if ( j >= n ) { return 0 ; } if ( i >= n ) { return 1 ; } if ( j == i j == n - 1 - i ) { if ( i == n - 1 - j ) { Console . Write ( " / " ) ; } else { Console . Write ( " \ \" ) ; } } else { Console . Write ( " * " ) ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } Console . WriteLine ( ) ; return printPattern ( i + 1 , 0 , n ) ; } public static void Main ( String [ ] args ) { int N = 9 ; printPattern ( 0 , 0 , N ) ; } }
Find starting index for every occurrence of given array B in array A using Z | C # implementation for pattern searching in an array using Z - Algorithm ; Function to calculate Z - Array ; Loop to calculate Z - Array ; Outside the Z - box ; Inside Z - box ; Helper function to merge two arrays and create a single array ; Array to store merged array ; Copying array B ; Adding a separator ; Copying array A ; Calling Z - function ; Function to help compute the Z array ; Printing indexes where array B occur ; Driver Code
using System ; class GfG { private static int [ ] zArray ( int [ ] arr ) { int [ ] z ; int n = arr . Length ; z = new int [ n ] ; int r = 0 , l = 0 ; for ( int k = 1 ; k < n ; k ++ ) { if ( k > r ) { r = l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } else { int k1 = k - l ; if ( z [ k1 ] < r - k + 1 ) z [ k ] = z [ k1 ] ; else { l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } } } return z ; } private static int [ ] mergeArray ( int [ ] A , int [ ] B ) { int n = A . Length ; int m = B . Length ; int [ ] z ; int [ ] c = new int [ n + m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) c [ i ] = B [ i ] ; c [ m ] = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) c [ m + i + 1 ] = A [ i ] ; z = zArray ( c ) ; return z ; } private static void findZArray ( int [ ] A , int [ ] B , int n ) { int flag = 0 ; int [ ] z ; z = mergeArray ( A , B ) ; for ( int i = 0 ; i < z . Length ; i ++ ) { if ( z [ i ] == n ) { Console . Write ( ( i - n - 1 ) + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) { Console . WriteLine ( " Not ▁ Found " ) ; } } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 2 , 3 , 2 } ; int [ ] B = { 2 , 3 } ; int n = B . Length ; findZArray ( A , B , n ) ; } }
Check if a string can be repeated to make another string | C # implementation of the approach ; Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times ; Driver code
using System ; class GfG { static int getCount ( String a , String b ) { if ( b . Length % a . Length != 0 ) return - 1 ; int count = b . Length / a . Length ; String str = " " ; for ( int i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str . Equals ( b ) ) return count ; return - 1 ; } public static void Main ( String [ ] args ) { String a = " geeks " ; String b = " geeksgeeks " ; Console . WriteLine ( getCount ( a , b ) ) ; } }
Check if a string can be formed from another string using given constraints | C # program to Check if a given String can be formed from another String using given constraints ; Function to check if S2 can be formed of S1 ; length of Strings ; hash - table to store count ; store count of each character ; traverse and check for every character ; if the character of s2 is present in s1 ; if the character of s2 is not present in S1 , then check if previous two ASCII characters are present in S1 ; Driver Code ; Calling function to check
using System ; using System . Collections . Generic ; class GFG { static bool check ( String S1 , String S2 ) { int n1 = S1 . Length ; int n2 = S2 . Length ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( mp . ContainsKey ( ( int ) S1 [ i ] ) ) { mp [ ( int ) S1 [ i ] ] = mp [ ( int ) S1 [ i ] ] + 1 ; } else { mp . Add ( ( int ) S1 [ i ] , 1 ) ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( mp . ContainsKey ( ( int ) S2 [ i ] ) ) { mp [ ( int ) S2 [ i ] ] = mp [ ( int ) S2 [ i ] ] - 1 ; } else if ( mp . ContainsKey ( S2 [ i ] - 1 ) && mp . ContainsKey ( S2 [ i ] - 2 ) ) { mp [ S2 [ i ] - 1 ] = mp [ S2 [ i ] - 1 ] - 1 ; mp [ S2 [ i ] - 2 ] = mp [ S2 [ i ] - 2 ] - 1 ; } else { return false ; } } return true ; } public static void Main ( String [ ] args ) { String S1 = " abbat " ; String S2 = " cat " ; if ( check ( S1 , S2 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Count of occurrences of a "1(0 + ) 1" pattern in a string | C # program to calculate number of times the pattern occurred in given string ; Returns count of occurrences of "1(0 + ) 1" int str . ; int count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver Code
using System ; class GFG { public static int countPattern ( string str ) { int len = str . Length ; bool oneSeen = false ; for ( int i = 0 ; i < len ; i ++ ) { char getChar = str [ i ] ; if ( getChar == '1' && oneSeen == true ) { if ( str [ i - 1 ] == '0' ) { count ++ ; } } if ( getChar == '1' && oneSeen == false ) { oneSeen = true ; } if ( getChar != '0' && str [ i ] != '1' ) { oneSeen = false ; } } return count ; } public static void Main ( string [ ] args ) { string str = "100001abc101" ; Console . WriteLine ( countPattern ( str ) ) ; } }
Convert given Strings into T by replacing characters in between strings any number of times | c # program for the above approach ; Function to check if it possible to make all the strings equal to the string T ; Stores the frequency of all the strings in the array arr [ ] ; Stores the frequency of the string T ; Iterate over the characters of the string T ; Iterate in the range [ 0 , N - 1 ] ; Iterate over the characters of the string arr [ i ] ; If freqT [ i ] is 0 and freqS [ i ] is not 0 ; If freqS [ i ] is 0 and freqT [ i ] is not 0 ; If freqS [ i ] is not freqT [ i ] * N ; Otherwise , return " Yes " ; Driver Code
using System ; public class GFG { static string checkIfPossible ( int N , string [ ] arr , string T ) { int [ ] freqS = new int [ 256 ] ; int [ ] freqT = new int [ 256 ] ; foreach ( char ch in T . ToCharArray ( ) ) { freqT [ ch - ' a ' ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { foreach ( char ch in arr [ i ] . ToCharArray ( ) ) { freqS [ ch - ' a ' ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( freqT [ i ] == 0 && freqS [ i ] != 0 ) { return " No " ; } else if ( freqS [ i ] == 0 && freqT [ i ] != 0 ) { return " No " ; } else if ( freqT [ i ] != 0 && freqS [ i ] != ( freqT [ i ] * N ) ) { return " No " ; } } return " Yes " ; } public static void Main ( string [ ] args ) { string [ ] arr = { " abc " , " abb " , " acc " } ; string T = " abc " ; int N = arr . Length ; Console . WriteLine ( checkIfPossible ( N , arr , T ) ) ; } }
Count of groups of consecutive 1 s in a given Binary String | C # program for the above approach ; Function to find the number of the groups of 1 s only in the binary string ; Stores number of groups of 1 s ; Initialization of the stack ; Traverse the string S ; If S [ i ] is '1' ; Otherwise ; If st is empty ; If st is not empty ; Return answer ; Driver code ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int groupsOfOnes ( string S , int N ) { int count = 0 ; Stack < int > st = new Stack < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) st . Push ( 1 ) ; else { if ( st . Count > 0 ) { count ++ ; while ( st . Count > 0 ) { st . Pop ( ) ; } } } } if ( st . Count > 0 ) count ++ ; return count ; } public static void Main ( ) { string S = "100110111" ; int N = S . Length ; Console . Write ( groupsOfOnes ( S , N ) ) ; } }
Palindromic strings of length 3 possible by using characters of a given string | C # program for the above approach ; Function to print all palindromic strings of length 3 that can be formed using characters of string S ; Stores the count of character ; Traverse the string S ; Stores all palindromic strings ; Iterate over the charchaters over the range [ ' a ' , ' z ' ] ; If Hash [ ch ] is equal to 2 ; Iterate over the characters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; Push the s into the set st ; If Hash [ i ] is greater than or equal to 3 ; Iterate over charchaters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; If Hash [ j ] is positive ; Push s into the set st ; Iterate over the set ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void generatePalindrome ( string S ) { Dictionary < char , int > Hash = new Dictionary < char , int > ( ) ; foreach ( char ch in S ) { if ( Hash . ContainsKey ( ch ) ) Hash [ ch ] ++ ; else Hash . Add ( ch , 1 ) ; } HashSet < string > st = new HashSet < string > ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( Hash . ContainsKey ( i ) && Hash [ i ] == 2 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { string s = " " ; if ( Hash . ContainsKey ( j ) && i != j ) { s += i ; s += j ; s += i ; st . Add ( s ) ; } } } if ( Hash . ContainsKey ( i ) && Hash [ i ] >= 3 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { string s = " " ; if ( Hash . ContainsKey ( j ) ) { s += i ; s += j ; s += i ; st . Add ( s ) ; } } } } foreach ( string ans in st ) { Console . WriteLine ( ans ) ; } } public static void Main ( ) { string S = " ddabdac " ; generatePalindrome ( S ) ; } }
Count occurrences of substring X before every occurrence of substring Y in a given string | C # program for the above approach ; Function to count occurrences of the string Y in the string S for every occurrence of X in S ; Stores the count of occurrences of X ; Stores the lengths of the three strings ; Traverse the string S ; If the current substring is Y , then increment the value of count by 1 ; If the current substring is X , then print the count ; Driver Code
using System ; public class GFG { static void countOccurrences ( string S , string X , string Y ) { int count = 0 ; int N = S . Length , A = X . Length ; int B = Y . Length ; int P = Math . Min ( A , Math . Min ( N , B ) ) ; for ( int i = 0 ; i < N - P + 1 ; i ++ ) { if ( S . Substring ( i , Math . Min ( N , B ) ) . Equals ( Y ) ) count ++ ; if ( S . Substring ( i , Math . Min ( N , A ) ) . Equals ( X ) ) Console . Write ( count + " ▁ " ) ; } } public static void Main ( string [ ] args ) { string S = " abcdefdefabc " ; string X = " abc " ; string Y = " def " ; countOccurrences ( S , X , Y ) ; } }
Program to construct DFA for Regular Expression C ( A + B ) + | C # program for the above approach ; Function to find whether the given String is Accepted by the DFA ; If n <= 1 , then print No ; To count the matched characters ; Check if the first character is C ; Traverse the rest of String ; If character is A or B , increment count by 1 ; If the first character is not C , print - 1 ; If all characters matches ; Driver Code
using System ; class GFG { static void DFA ( string str , int N ) { if ( N <= 1 ) { Console . Write ( " No " ) ; return ; } int count = 0 ; if ( str [ 0 ] == ' C ' ) { count ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( str [ i ] == ' A ' str [ i ] == ' B ' ) count ++ ; else break ; } } else { Console . Write ( " No " ) ; return ; } if ( count == N ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } static public void Main ( ) { string str = " CAABBAAB " ; int N = str . Length ; DFA ( str , N ) ; } }
Minimum and maximum number of digits required to be removed to make a given number divisible by 3 | C # program for the above approach ; Function to find the maximum and minimum number of digits to be removed to make str divisible by 3 ; Convert the string into array of digits ; Count of 0 s , 1 s , and 2 s ; Traverse the array ; Find the sum of digits % 3 ; Cases to find minimum number of digits to be removed ; Cases to find maximum number of digits to be removed ; Driver code ; Function Call
using System ; class GFG { static void minMaxDigits ( string str , int N ) { int [ ] arr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) arr [ i ] = ( str [ i ] - '0' ) % 3 ; int zero = 0 , one = 0 , two = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero ++ ; if ( arr [ i ] == 1 ) one ++ ; if ( arr [ i ] == 2 ) two ++ ; } int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum = ( sum + arr [ i ] ) % 3 ; } if ( sum == 0 ) { Console . Write ( 0 + " ▁ " ) ; } if ( sum == 1 ) { if ( ( one != 0 ) && ( N > 1 ) ) Console . Write ( 1 + " ▁ " ) ; else if ( two > 1 && N > 2 ) Console . Write ( 2 + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } if ( sum == 2 ) { if ( two != 0 && N > 1 ) Console . Write ( 1 + " ▁ " ) ; else if ( one > 1 && N > 2 ) Console . Write ( 2 + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } if ( zero > 0 ) Console . Write ( N - 1 + " ▁ " ) ; else if ( one > 0 && two > 0 ) Console . Write ( N - 2 + " ▁ " ) ; else if ( one > 2 two > 2 ) Console . Write ( N - 3 + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } public static void Main ( ) { string str = "12345" ; int N = str . Length ; minMaxDigits ( str , N ) ; } }
Minimum replacements required to obtain a K | C # program for the above approach ; Function to find the minimum number of changes to make the String K - periodic and palindrome ; Initialize ans with 0 ; Iterate from 0 to ( K + 1 ) / 2 ; Store frequency of character ; Iterate through all indices , i , i + K , i + 2 k ... . and store the frequency of character ; Increase the frequency of current character ; Iterate through all indices K - i , 2 K - i , 3 K - i ... . and store the frequency of character ; If K is odd & i is samw as K / 2 , break the loop ; Increase the frequency of current character ; Find the maximum frequency of a character among all visited characters ; If K is odd and i is same as K / 2 then , only N / K characters is visited ; Otherwise N / K * 2 characters has visited ; Return the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int findMinimumChanges ( int N , int K , char [ ] S ) { int ans = 0 ; for ( int i = 0 ; i < ( K + 1 ) / 2 ; i ++ ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int j = i ; j < N ; j += K ) { if ( mp . ContainsKey ( S [ j ] ) ) { mp [ S [ j ] ] ++ ; } else { mp . Add ( S [ j ] , 1 ) ; } } for ( int j = N - i - 1 ; j >= 0 ; j -= K ) { if ( K % 2 == 1 && i == K / 2 ) break ; if ( mp . ContainsKey ( S [ j ] ) ) { mp [ S [ j ] ] ++ ; } else { mp . Add ( S [ j ] , 1 ) ; } } int curr_max = int . MinValue ; foreach ( KeyValuePair < char , int > p in mp ) { curr_max = Math . Max ( curr_max , p . Value ) ; } if ( ( K % 2 == 1 ) && i == K / 2 ) ans += ( N / K - curr_max ) ; else ans += ( N / K * 2 - curr_max ) ; } return ans ; } public static void Main ( String [ ] args ) { String S = " aabbcbbcb " ; int N = S . Length ; int K = 3 ; Console . Write ( findMinimumChanges ( N , K , S . ToCharArray ( ) ) ) ; } }
Check if a String contains any index with more than K active characters | C # program to implement the above approach ; Function to check if any index contains more than K active characters ; Store the last occurrence of each character in the map . ; Stores the active characters ; Insert the character ; If the size of set exceeds K ; Remove the character from set if i is the last index of the current character ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String checkString ( String s , int K ) { int n = s . Length ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) mp [ s [ i ] ] = i ; else mp . Add ( s [ i ] , i ) ; } int f = 0 ; HashSet < char > st = new HashSet < char > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { st . Add ( s [ i ] ) ; if ( st . Count > K ) { f = 1 ; break ; } if ( mp [ s [ i ] ] == i ) st . Remove ( s [ i ] ) ; } return ( f == 1 ? " Yes " : " No " ) ; } public static void Main ( String [ ] args ) { String s = " aabbcdca " ; int k = 2 ; Console . WriteLine ( checkString ( s , k ) ) ; } }
Count the number of strings in an array whose distinct characters are less than equal to M | C # implementation to count the number of strings in the array whose distinct characters is less than or equal to M ; Function to count the strings whose distinct characters count is less than M ; Loop to iterate over all the strings of the array ; Distinct characters in the String with the help of set ; Checking if its less than or equal to M ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void distinct ( string [ ] S , int M ) { int count = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { HashSet < char > set = new HashSet < char > ( ) ; for ( int j = 0 ; j < S [ i ] . Length ; j ++ ) { if ( ! set . Contains ( S [ i ] [ j ] ) ) set . Add ( S [ i ] [ j ] ) ; } int c = set . Count ; if ( c <= M ) count += 1 ; } Console . Write ( count ) ; } public static void Main ( string [ ] args ) { string [ ] S = { " HERBIVORES " , " AEROPLANE " , " GEEKSFORGEEKS " } ; int M = 7 ; distinct ( S , M ) ; } }