codeparrot/xlcost-text-to-code huggingface dataset:
Split a string into maximum number of unique substrings | C # program for the above approach ; Function to find the maximum count of unique substrings by splitting a string into maximum number of unique substrings ; Utility function to find maximum count of unique substrings by splitting the string ; Stores maximum count of unique substring by splitting the string into substrings ; Iterate over the characters of the string ; Stores prefix substring ; Check if the current substring already exists ; Insert tmp into set ; Recursively call for remaining characters of string ; Remove from the set ; Return answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public int maxUniqueSplit ( String S ) { return maxUnique ( S , new HashSet < String > ( ) ) ; } public int maxUnique ( String S , HashSet < String > set ) { int max = 0 ; for ( int i = 1 ; i <= S . Length ; i ++ ) { String tmp = S . Substring ( 0 , i ) ; if ( ! set . Contains ( tmp ) ) { set . Add ( tmp ) ; max = Math . Max ( max , maxUnique ( S . Substring ( i ) , set ) + 1 ) ; set . Remove ( tmp ) ; } } return max ; } } public class GFG { public static void Main ( String [ ] args ) { Solution st = new Solution ( ) ; String S = " ababccc " ; int res = st . maxUniqueSplit ( S ) ; Console . WriteLine ( res ) ; } }
Minimize length of a given string by removing subsequences forming valid parenthesis | C # program of the above approach ; Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Driver code ; Given string ; Size of the string ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void removeValidBracketSequences ( string str , int N ) { List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; List < int > C = new List < int > ( ) ; bool [ ] vis = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ( ' ) { A . Add ( i ) ; } else if ( str [ i ] == ' { ' ) { B . Add ( i ) ; } else if ( str [ i ] == ' [ ' ) { C . Add ( i ) ; } else if ( str [ i ] == ' ) ' && ( A . Count > 0 ) ) { vis [ A [ A . Count - 1 ] ] = false ; A . RemoveAt ( A . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' } ' && ( B . Count > 0 ) ) { vis [ B [ B . Count - 1 ] ] = false ; B . RemoveAt ( B . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ] ' && ( C . Count > 0 ) ) { vis [ C [ C . Count - 1 ] ] = false ; C . RemoveAt ( C . Count - 1 ) ; vis [ i ] = false ; } } for ( int i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) Console . Write ( str [ i ] ) ; } } static void Main ( ) { string str = " ( ( ) { ( ) ( { } ) " ; int N = str . Length ; removeValidBracketSequences ( str , N ) ; } }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | C # program for the above approach ; Function to update frequency of a character in Fenwick tree ; Update frequency of ( idx + ' a ' ) ; Update i ; Function to find the frequency of a character ( idx + ' a ' ) in range [ 1 , i ] ; Stores frequency of character , ( idx + ' a ' ) in range [ 1 , i ] ; Update ans ; Update i ; Function to find difference between maximum and minimum frequency of a character in given range ; BIT [ i , j ] : Stores frequency of ( i + ' a ' ) If j is a power of 2 , then it stores the frequency ( i + ' a ' ) of from [ 1 , j ] ; Stores length of String ; Iterate over the characters of the String ; Update the frequency of s [ i ] in fenwick tree ; Stores count of queries ; Iterate over all the queries ; Stores maximum frequency of a character in range [ l , r ] ; Stores minimum frequency of a character in range [ l , r ] ; Iterate over all possible characters ; Stores frequency of ( j + ' a ' ) in range [ 1 , r ] ; Stores frequency of ( j + ' a ' ) in range [ 1 , l - 1 ] ; Update mx ; If a character ( i + ' a ' ) present in range [ l , r ] ; Update mn ; Print the difference between max and min freq ; Driver Code ; Given String ; Given queries ; Function Call
using System ; public class GFG { static void update ( int [ , ] BIT , int idx , int i , int val ) { while ( i < 10005 ) { BIT [ idx , i ] += val ; i = i + ( i & ( - i ) ) ; } } static int query ( int [ , ] BIT , int idx , int i ) { int ans = 0 ; while ( i > 0 ) { ans += BIT [ idx , i ] ; i = i - ( i & ( - i ) ) ; } return ans ; } static void maxDiffFreq ( String s , int [ , ] queries ) { int [ , ] BIT = new int [ 26 , 10005 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { update ( BIT , s [ i ] - ' a ' , i + 1 , 1 ) ; } int Q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < Q ; ++ i ) { int mx = 0 ; int mn = int . MaxValue ; int l = queries [ i , 0 ] ; int r = queries [ i , 1 ] ; for ( int j = 0 ; j < 26 ; j ++ ) { int p = query ( BIT , j , r ) ; int q = query ( BIT , j , l - 1 ) ; mx = Math . Max ( mx , p - q ) ; if ( p > 0 ) { mn = Math . Min ( mn , p - q ) ; } } Console . Write ( mx - mn + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { String S = " abaabac " ; int [ , ] queries = { { 2 , 6 } , { 1 , 7 } } ; maxDiffFreq ( S , queries ) ; } }
Make all strings from a given array equal by replacing minimum number of characters | C # program to implement the above approach ; Function to find the minimum count of operations required to make all Strings equal by replacing characters of Strings ; Stores minimum count of operations required to make all Strings equal ; Stores length of the String ; hash [ i , j ] : Stores frequency of character i present at j - th index of all Strings ; Traverse the array [ ] arr ; Iterate over characters of current String ; Update frequency of arr [ i , j ] ; Traverse hash [ , ] array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
using System ; class GFG { static int minOperation ( String [ ] arr , int N ) { int cntMinOP = 0 ; int M = arr [ 0 ] . Length ; int [ , ] hash = new int [ 256 , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] , j ] ++ ; } } for ( int i = 0 ; i < M ; i ++ ) { int Sum = 0 ; int Max = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j , i ] ; Max = Math . Max ( Max , hash [ j , i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } public static void Main ( String [ ] args ) { String [ ] arr = { " abcd " , " bcde " , " cdef " } ; int N = arr . Length ; Console . Write ( minOperation ( arr , N ) + " STRNEWLINE " ) ; } }
Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | C # program for the above approach ; Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ; Store the starting index of the substring ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void countStrings ( String s , int k ) { int ans = 1 ; int len = s . Length ; int flag = 0 ; int start_ind = 0 ; for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( s [ i ] - '0' + s [ i + 1 ] - '0' == k && flag == 0 ) { flag = 1 ; start_ind = i ; } if ( flag == 1 && s [ i ] - '0' + s [ i + 1 ] - '0' != k ) { flag = 0 ; if ( ( i - start_ind + 1 ) % 2 != 0 ) ans *= ( i - start_ind + 1 - 1 ) / 2 + 1 ; } } if ( flag == 1 && ( len - start_ind ) % 2 != 0 ) ans *= ( len - start_ind ) / 2 + 1 ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = "313" ; int K = 4 ; countStrings ( S , K ) ; } }
Smallest number whose product with N has sum of digits equal to that of N | C # program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
using System ; using System . Globalization ; class GFG { static void find_num ( string n ) { int ans = 0 ; char [ ] digitsOfN = n . ToCharArray ( ) ; int sumOfDigitsN = 0 ; foreach ( char c in digitsOfN ) { sumOfDigitsN += Int32 . Parse ( Char . ToString ( c ) ) ; } for ( int x = 11 ; x > 0 ; x ++ ) { int newNum = x * Int32 . Parse ( n ) ; int tempSumDigits = 0 ; string str = newNum . ToString ( ) ; char [ ] temp = str . ToCharArray ( ) ; foreach ( char c in temp ) { tempSumDigits += Int32 . Parse ( Char . ToString ( c ) ) ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { string N = "3029" ; find_num ( N ) ; } }
Print all distinct strings from a given array | C # program to implement the above approach ; Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver code ; Stores length of the array
using System ; using System . Collections . Generic ; class GFG { static void findDisStr ( List < string > arr , int N ) { HashSet < string > DistString = new HashSet < string > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! DistString . Contains ( arr [ i ] ) ) { DistString . Add ( arr [ i ] ) ; } } foreach ( string a in DistString ) { Console . Write ( a + " ▁ " ) ; } } public static void Main ( String [ ] args ) { List < String > arr = new List < string > ( new [ ] { " Geeks " , " For " , " Geeks " , " Code " , " Coder " } ) ; int N = arr . Count ; findDisStr ( arr , N ) ; } }
Reverse substrings of given string according to specified array indices | C # program for the above approach ; Function to perform the reversal operation on the given String ; Size of String ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated String ; Swap char of a string ; Driver Code ; Given String str ; Given array of reversing index ; Function Call
using System ; class GFG { static String modifyString ( int [ ] A , String str , int K ) { int N = str . Length ; int [ ] count = new int [ N + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) { count [ A [ i ] ] ++ ; } for ( int i = 1 ; i <= N / 2 ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; if ( ( count [ i ] & 1 ) > 0 ) { str = swap ( str , i - 1 , N - i ) ; } } return str ; } static String swap ( String str , int i , int j ) { char [ ] ch = str . ToCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } public static void Main ( String [ ] args ) { String str = " abcdef " ; int [ ] arr = { 1 , 2 , 3 } ; int K = arr . Length ; Console . Write ( modifyString ( arr , str , K ) ) ; } }
Convert given Float value to equivalent Fraction | C # program for the above approach ; Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . Count is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given String str ; Function Call
using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findFraction ( String s ) { String be_deci = " " , af_deci = " " , reccu = " " ; bool x = true , y = false , z = false ; for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] == ' . ' ) { x = false ; y = true ; continue ; } if ( s [ i ] == ' ( ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s [ i ] ; if ( y ) af_deci += s [ i ] ; if ( z ) { for ( ; i < s . Length && s [ i ] != ' ) ' ; ++ i ) reccu += s [ i ] ; break ; } } int num_be_deci = Int32 . Parse ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . Length != 0 ) num_af_deci = Int32 . Parse ( af_deci ) ; int numr = num_be_deci * ( int ) Math . Pow ( 10 , af_deci . Length ) + num_af_deci ; int deno = ( int ) Math . Pow ( 10 , af_deci . Length ) ; if ( reccu . Length == 0 ) { int gd = __gcd ( numr , deno ) ; Console . Write ( numr / gd + " ▁ / ▁ " + deno / gd ) ; } else { int reccu_num = Int32 . Parse ( reccu ) ; int numr1 = numr * ( int ) Math . Pow ( 10 , reccu . Length ) + reccu_num ; int deno1 = deno * ( int ) Math . Pow ( 10 , reccu . Length ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; Console . Write ( res_numr / gd + " ▁ / ▁ " + res_deno / gd ) ; } } public static void Main ( String [ ] args ) { String str = "23.98(231 ) " ; findFraction ( str ) ; } }
Lexicographically largest string possible for a given cost of appending characters | C # Program to implement the above approach ; Function to find the lexicographically largest String possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest String ; Backtrack if solution not found ; Find the lexicographically largest String excluding the current character ; Function to print the lexicographically largest String generated ; Function call ; Stores the String ; Print the lexicographically largest String formed ; Driver code ; Cost of adding each alphabet ; Cost of generating the String
using System ; using System . Collections . Generic ; class GFG { static bool lexi_largest_String ( int [ ] a , int i , int sum , List < int > v ) { if ( sum < 0 ) return false ; if ( sum == 0 ) return true ; if ( i < 0 ) return false ; v . Add ( i ) ; if ( lexi_largest_String ( a , i , sum - a [ i ] , v ) ) return true ; v . RemoveAt ( v . Count - 1 ) ; return lexi_largest_String ( a , i - 1 , sum , v ) ; } static void generateString ( int [ ] a , int sum ) { List < int > v = new List < int > ( ) ; lexi_largest_String ( a , 25 , sum , v ) ; String s = " " ; for ( int j = 0 ; j < v . Count ; j ++ ) s += ( char ) ( v [ j ] + ' a ' ) ; Console . Write ( s + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 } ; int sum = 236 ; generateString ( a , sum ) ; } }
Count of distinct permutations of every possible length of given string | C # implementation of the above approach ; Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given String ; Function to find the total number of combinations possible ; Driver Code
using System ; class GFG { static int fact ( int a ) { int i , f = 1 ; for ( i = 2 ; i <= a ; i ++ ) f = f * i ; return f ; } static int permute ( int n , int r ) { int ans = 0 ; ans = ( fact ( n ) / fact ( n - r ) ) ; return ans ; } static int findPermutations ( int n ) { int sum = 0 , P ; for ( int r = 1 ; r <= n ; r ++ ) { P = permute ( n , r ) ; sum = sum + P ; } return sum ; } public static void Main ( String [ ] args ) { String str = " xz " ; int n ; n = str . Length ; Console . Write ( findPermutations ( n ) ) ; } }
Cycles of length n in an undirected and connected graph | C # program to calculate cycles of length n in a given graph ; Number of vertices ; mark the vertex vert as visited ; if the path of length ( n - 1 ) is found ; mark vert as un - visited to make it usable again ; Check if vertex vert end with vertex start ; For searching every possible path of length ( n - 1 ) ; DFS for searching path by decreasing length by 1 ; marking vert as unvisited to make it usable again ; Count cycles of length N in an undirected and connected graph . ; all vertex are marked un - visited initially . ; Searching for cycle by using v - n + 1 vertices ; ith vertex is marked as visited and will not be visited again ; Driver code
using System ; class GFG { public static int V = 5 ; static int count = 0 ; static void DFS ( int [ , ] graph , bool [ ] marked , int n , int vert , int start ) { marked [ vert ] = true ; if ( n == 0 ) { marked [ vert ] = false ; if ( graph [ vert , start ] == 1 ) { count ++ ; return ; } else return ; } for ( int i = 0 ; i < V ; i ++ ) if ( ! marked [ i ] && graph [ vert , i ] == 1 ) DFS ( graph , marked , n - 1 , i , start ) ; marked [ vert ] = false ; } static int countCycles ( int [ , ] graph , int n ) { bool [ ] marked = new bool [ V ] ; for ( int i = 0 ; i < V - ( n - 1 ) ; i ++ ) { DFS ( graph , marked , n - 1 , i , i ) ; marked [ i ] = true ; } return count / 2 ; } public static void Main ( ) { int [ , ] graph = { { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } } ; int n = 4 ; Console . WriteLine ( " Total ▁ cycles ▁ of ▁ length ▁ " + n + " ▁ are ▁ " + countCycles ( graph , n ) ) ; } }
String hashing using Polynomial rolling hash function | C # implementation of the Polynomial Rolling Hash Function ; Function to calculate the hash of a String ; P and M ; Loop to calculate the hash value by iterating over the elements of String ; Driver Code ; Given Strings
using System ; class GFG { static long polynomialRollingHash ( String str ) { int p = 31 ; int m = ( int ) ( 1e9 + 9 ) ; long power_of_p = 1 ; long hash_val = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { hash_val = ( hash_val + ( str [ i ] - ' a ' + 1 ) * power_of_p ) % m ; power_of_p = ( power_of_p * p ) % m ; } return hash_val ; } public static void Main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " geeks " ; Console . Write ( " Hash ▁ of ▁ ' " + str1 + " ' ▁ = ▁ " + polynomialRollingHash ( str1 ) ) ; } }
Find first non | C # implementation to find the first non - repeating element of the string using Linked List ; Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class FirstNonRepeatingElement { static void firstNonRepElement ( string str ) { LinkedList < char > list = new LinkedList < char > ( ) ; list . AddLast ( str [ 0 ] ) ; for ( int i = 1 ; i < str . Length ; i ++ ) { if ( list . Contains ( str [ i ] ) ) list . Remove ( str [ i ] ) ; else list . AddLast ( str [ i ] ) ; } Console . Write ( list . First . Value ) ; } public static void Main ( string [ ] args ) { string str = " geeksforgeeks " ; firstNonRepElement ( str ) ; } }
DFA that begins with ' a ' but does not contain substring ' aab ' | C # code for the above DFA ; Function for state A transition ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state B ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for transition state C ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state D ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state Q transition ; In dead state it shows string not accepted ; Driver code ; Take string input ; Call stateA to check the input
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void stateA ( string n ) { if ( n [ 0 ] == ' a ' ) { stateB ( n . Substring ( 1 ) ) ; } else { stateQ ( n ) ; } } static void stateB ( string n ) { if ( n . Length == 0 ) { Console . Write ( " Accepted " ) ; } else { if ( n [ 0 ] == ' a ' ) stateC ( n . Substring ( 1 ) ) ; else stateD ( n . Substring ( 1 ) ) ; } } static void stateC ( string n ) { if ( n . Length == 0 ) Console . Write ( " Accepted " ) ; else { if ( n [ 0 ] == ' a ' ) stateC ( n . Substring ( 1 ) ) ; else stateQ ( n ) ; } } static void stateD ( string n ) { if ( n . Length == 0 ) Console . Write ( " Accepted " ) ; else { if ( n [ 0 ] == ' a ' ) { stateB ( n . Substring ( 1 ) ) ; } else { stateD ( n . Substring ( 1 ) ) ; } } } static void stateQ ( string n ) { Console . Write ( " Not ▁ Accepted " ) ; } public static void Main ( string [ ] args ) { string n = " aaaba " ; stateA ( n ) ; } }
Count of substrings of length K with exactly K distinct characters | C # program to find the count of k length substrings with k distinct characters using sliding window ; Function to return the required count of substrings ; Store the count ; Store the count of distinct characters in every window ; Store the frequency of the first K length substring ; Increase frequency of i - th character ; If K distinct characters exist ; Traverse the rest of the substring ; Increase the frequency of the last character of the current substring ; Decrease the frequency of the first character of the previous substring ; If the character is not present in the current substring ; If the count of distinct characters is 0 ; Return the count ; Driver code ; string str ; integer K ; Print the count of K length substrings with k distinct characters
using System ; using System . Collections . Generic ; class GFG { public static int countSubstrings ( string str , int K ) { int N = str . Length ; int answer = 0 ; Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( ! map . ContainsKey ( str [ i ] ) ) { map [ str [ i ] ] = 1 ; } else { map [ str [ i ] ] ++ ; } } if ( map . Count == K ) answer ++ ; for ( int i = K ; i < N ; i ++ ) { if ( ! map . ContainsKey ( str [ i ] ) ) { map [ str [ i ] ] = 1 ; } else { map [ str [ i ] ] ++ ; } map [ str [ i - K ] ] -- ; if ( map [ str [ i - K ] ] == 0 ) { map . Remove ( str [ i - K ] ) ; } if ( map . Count == K ) { answer ++ ; } } return answer ; } public static void Main ( string [ ] args ) { string str = " aabcdabbcdc " ; int K = 3 ; Console . Write ( countSubstrings ( str , K ) ) ; } }