codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Binary String can be split into disjoint subsequences which are equal to "010" | C # program for the above approach ; Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver code ; Given string ; Function Call
using System ; class GFG { static bool isPossible ( String s ) { int n = s . Length ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 ; count_1 = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } static public void Main ( ) { String s = "010100" ; if ( isPossible ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Minimum number of bricks that can be intersected | C # program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void leastBricks ( List < List < int > > wall ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int res = 0 ; foreach ( List < int > subList in wall ) { int width = 0 ; for ( int i = 0 ; i < subList . Count - 1 ; i ++ ) { width += subList [ i ] ; if ( map . ContainsKey ( width ) ) map [ width ] ++ ; else map . Add ( width , 1 ) ; res = Math . Max ( res , map [ width ] ) ; } } Console . Write ( wall . Count - res ) ; } public static void Main ( ) { List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 2 , 2 , 1 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 2 } ) ; myList . Add ( new List < int > { 2 , 4 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 1 , 1 } ) ; leastBricks ( myList ) ; } }
Find an N | C # implementation of the above approach ; Function to print the required permutation ; Driver code
using System ; class GFG { static void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int N = 5 ; findPermutation ( N ) ; } }
Maximize count of distinct profits possible by N transactions | C # program for the above approach ; Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Driver Code ; Input ; Function call
using System ; class GFG { static int numberOfWays ( int N , int X , int Y ) { int S1 = ( N - 1 ) * X + Y ; int S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } public static void Main ( ) { int N = 3 ; int X = 13 ; int Y = 15 ; Console . WriteLine ( numberOfWays ( N , X , Y ) ) ; } }
Minimum number of operations required to make a permutation of first N natural numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input
using System ; class GFG { static int MinimumOperations ( int [ ] A , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } public static void Main ( ) { int [ ] A = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = A . Length ; Console . WriteLine ( MinimumOperations ( A , N , K ) ) ; } }
Consecutive Prime numbers greater than equal to given number . | C # program for the above approach ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program
using System ; class GFG { static bool is_prime ( long n ) { if ( n == 1 ) { return false ; } for ( long i = 2 ; i <= ( long ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static void consecutive_primes ( long n ) { long first = - 1 , second = - 1 ; for ( long i = ( long ) Math . Sqrt ( n ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( long i = ( long ) Math . Sqrt ( n ) + 1 ; i <= n / 2 ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { Console . WriteLine ( first + " ▁ " + second ) ; } else { for ( long i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { Console . WriteLine ( second + " ▁ " + i ) ; return ; } } } } public static void Main ( ) { long n = 14 ; consecutive_primes ( n ) ; } }
Reorder characters of a string to valid English representations of digits | C # program to implement the above approach ; Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Driver Code ; Given string , s ; Function Call
using System ; using System . Collections . Generic ; class GFG { static string construct_digits ( string s ) { char [ ] k = { ' z ' , ' w ' , ' u ' , ' x ' , ' g ' , ' h ' , ' o ' , ' f ' , ' v ' , ' i ' } ; string [ ] l = { " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " } ; int [ ] c = { 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 } ; List < string > ans = new List < string > ( ) ; Dictionary < char , int > d = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! d . ContainsKey ( s [ i ] ) ) d [ s [ i ] ] = 0 ; d [ s [ i ] ] += 1 ; } for ( int i = 0 ; i < k . Length ; i ++ ) { int x = 0 ; if ( d . ContainsKey ( k [ i ] ) ) x = d [ k [ i ] ] ; for ( int j = 0 ; j < l [ i ] . Length ; j ++ ) { if ( d . ContainsKey ( l [ i ] [ j ] ) ) d [ l [ i ] [ j ] ] -= x ; } ans . Add ( ( ( c [ i ] ) * x ) . ToString ( ) ) ; } ans . Sort ( ) ; string str = ( String . Join ( " " , ans . ToArray ( ) ) ) ; return str . Replace ( "0" , " " ) ; } public static void Main ( string [ ] args ) { string s = " fviefuro " ; Console . WriteLine ( construct_digits ( s ) ) ; } }
Minimum decrements required to make all pairs of adjacent matrix elements distinct | C # program for the above approach ; Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; Driver Code ; The given matrix ; Function Call to count the minimum number of decrements required
using System ; class GFG { static void countDecrements ( long [ , ] arr ) { int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i , j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i , j ] % 2 ) count_2 ++ ; } } Console . WriteLine ( Math . Min ( count_1 , count_2 ) ) ; } public static void Main ( ) { long [ , ] arr = { { 1 , 2 , 3 } , { 1 , 2 , 3 } , { 1 , 2 , 3 } } ; countDecrements ( arr ) ; } }
Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | C # program for the above approach ; Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code
using System ; class GFG { static void check ( int X , int Y ) { if ( X > 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 1 && Y == 1 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 2 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 3 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( string [ ] args ) { int X = 6 , Y = 8 ; check ( X , Y ) ; } }
Count distinct sum of pairs possible from a given range | C # program for the above approach ; Function to count distinct sum of pairs possible from the range [ L , R ] ; Return the count of distinct sum of pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int distIntegers ( int L , int R ) { return 2 * R - 2 * L + 1 ; } static public void Main ( ) { int L = 3 , R = 8 ; Console . Write ( distIntegers ( L , R ) ) ; } }
Count subarrays having even Bitwise XOR | C # program for the above approach ; Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Driver Code ; Given array ; Stores the size of the array
using System ; class GFG { static void evenXorSubarray ( int [ ] arr , int n ) { int ans = 0 ; int [ ] freq = { 0 , 0 } ; int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; evenXorSubarray ( arr , N ) ; } }
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | C # program for above approach ; Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Driver code ; Given N and X ; Function Call
using System ; public class GFG { static int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < Math . Sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { int a = i ; int b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } public static void Main ( String [ ] args ) { int N = 7 ; int X = 12 ; Console . Write ( countOccurrences ( N , X ) ) ; } }
Maximize array product by changing any array element arr [ i ] to ( | C # program for the above approach ; Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call
using System ; class GFG { static void findArrayWithMaxProduct ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 == 1 ) { int max_element = - 1 ; int index = - 1 ; for ( int i = 0 ; i < N ; i ++ ) if ( Math . Abs ( arr [ i ] ) > max_element ) { max_element = Math . Abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { - 3 , 0 , 1 } ; int N = arr . Length ; findArrayWithMaxProduct ( arr , N ) ; } }
Lexicographically smallest permutation of first N natural numbers having K perfect indices | C # program for the above approach ; Function to print the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
using System ; class GFG { static void findPerfectIndex ( int N , int K ) { int i = 0 ; for ( ; i < K ; i ++ ) { Console . Write ( ( N - K + 1 ) + i + " ▁ " ) ; } for ( ; i < N ; i ++ ) { Console . Write ( i - K + 1 + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 10 , K = 3 ; findPerfectIndex ( N , K ) ; } }
Count pairs having distinct sum from a given range | C # program for the above approach ; Function to count pairs made up of elements from the range [ L , R ] having distinct sum ; Stores the least sum which can be formed by the pairs ; Stores the highest sum which can be formed by the pairs ; Stores the count of pairs having distinct sum ; Print the count of pairs ; Driver Code ; Function call to count the number of pairs having distinct sum in the range [ L , R ]
using System ; public class GFG { static void countPairs ( long L , long R ) { long firstNum = 2 * L ; long lastNum = 2 * R ; long Cntpairs = lastNum - firstNum + 1 ; Console . WriteLine ( Cntpairs ) ; } static public void Main ( ) { long L = 2 , R = 3 ; countPairs ( L , R ) ; } }
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C # program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] A , int n ) { long count = ( n * ( n - 1 ) ) / 2 ; Dictionary < long , long > ump = new Dictionary < long , long > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ump . ContainsKey ( A [ i ] ) ) { ump [ A [ i ] ] ++ ; } else { ump [ A [ i ] ] = 1 ; } } foreach ( KeyValuePair < long , long > it in ump ) { long c = it . Value ; count = count - ( c * ( c - 1 ) ) / 2 ; } Console . WriteLine ( count ) ; } static void Main ( ) { int [ ] A = { 1 , 4 , 7 } ; int N = A . Length ; countPairs ( A , N ) ; } }
Check if a Rook can reach the given destination in a single move | C # program to implement the above approach ; Function to check if it is possible to reach destination in a single move by a rook ; Driver Code ; Given arrays
using System ; class GFG { static string check ( int current_row , int current_col , int destination_row , int destination_col ) { if ( current_row == destination_row ) return " POSSIBLE " ; else if ( current_col == destination_col ) return " POSSIBLE " ; else return " NOT ▁ POSSIBLE " ; } public static void Main ( ) { int current_row = 8 ; int current_col = 8 ; int destination_row = 8 ; int destination_col = 4 ; string output = check ( current_row , current_col , destination_row , destination_col ) ; Console . WriteLine ( output ) ; } }
Append X digits to the end of N to make it divisible by M | C # program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver code ; Stores the number by appending X digits on the right side of N
using System ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void Main ( ) { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; Console . WriteLine ( res ) ; } }
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | C # program for the above approach ; Function to find the minimum moves ; Stores the sum of the array ; Store the maximum element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver code ; Given array ; Size of given array ; Function Call
using System ; class GFG { static void minimumMoves ( int [ ] arr , int N ) { int sum = 0 ; int maxelement = - 1 ; if ( N == 2 ) { Console . Write ( "0" ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . Max ( maxelement , arr [ i ] ) ; } int k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . Max ( maxelement , k ) ; int ans = k * ( N - 1 ) - sum ; Console . WriteLine ( ans ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 7 } ; int N = arr . Length ; minimumMoves ( arr , N ) ; } }
Split first N natural numbers into two subsequences with non | C # program for the above approach ; Function to split 1 to N into two subsequences with non - coprime sums ; Driver code
using System ; class GFG { public static void printSubsequence ( int N ) { Console . Write ( " { ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { Console . Write ( i + " , ▁ " ) ; } Console . WriteLine ( N - 1 + " ▁ } " ) ; Console . Write ( " { ▁ " + N + " ▁ } " ) ; } public static void Main ( string [ ] args ) { int N = 8 ; printSubsequence ( N ) ; } }
Generate an array with product of all subarrays of length exceeding one divisible by K | C # program for the above approach ; Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
using System ; class GFG { public static void array_divisbleby_k ( int N , int K ) { bool flag = false ; int d1 = 0 , d2 = 0 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { Console . Write ( d2 + " ▁ " ) ; } else { Console . Write ( d1 + " ▁ " ) ; } } } else { Console . Write ( - 1 ) ; } } public static void Main ( string [ ] args ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; } }
Minimum characters to be replaced to make a string concatenation of a K | C # program for the above approach ; Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given String ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void addEdge ( List < int > [ ] adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static int cnt = 0 ; static bool [ ] visited ; static void DFS ( int u , List < int > [ ] adj , int [ ] fre , String S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] - ' a ' ] ++ ; for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , fre , S ) ; } } } static void minimumOperations ( String S , int m ) { int V = 100 ; List < int > [ ] adj = new List < int > [ V ] ; int total = 0 , N = S . Length ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( int i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new bool [ V ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { int [ ] fre = new int [ 26 ] ; cnt = 0 ; int maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( int j = 0 ; j < 26 ; j ++ ) maxx = Math . Max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } Console . Write ( total ) ; } public static void Main ( String [ ] args ) { String S = " abaaba " ; int K = 2 ; minimumOperations ( S , K ) ; } }
Replace specified matrix elements such that no two adjacent elements are equal | C # program for the above approach ; Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Driver Code ; Given N and M ; Given matrix ; Function call
using System ; class GFG { public static void print ( char [ , ] arr , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { char a = arr [ i , j ] ; if ( ( i + j ) % 2 == 0 && a == ' F ' ) { arr [ i , j ] = '1' ; } else if ( a == ' F ' ) { arr [ i , j ] = '2' ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( arr [ i , j ] ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 4 , m = 4 ; char [ , ] arr = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } } ; print ( arr , n , m ) ; } }
Lexicographically smallest string formed by removing duplicates | C # program for the above approach ; Function that finds lexicographically smallest string after removing the duplicates from the given string ; Stores the frequency of characters ; Mark visited characters ; Stores count of each character ; Stores the resultant string ; Decrease the count of current character ; If character is not already in answer ; Last character > S [ i ] and its count > 0 ; Mark letter unvisited ; Add s [ i ] in res and mark it visited ; Return the resultant string ; Driver Code ; Given string S ; Function Call
using System ; class GFG { static string removeDuplicateLetters ( string s ) { int [ ] cnt = new int [ 26 ] ; int [ ] vis = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) cnt [ s [ i ] - ' a ' ] ++ ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ s [ i ] - ' a ' ] -- ; if ( vis [ s [ i ] - ' a ' ] == 0 ) { int size = res . Length ; while ( size > 0 && res [ size - 1 ] > s [ i ] && cnt [ res [ size - 1 ] - ' a ' ] > 0 ) { vis [ res [ size - 1 ] - ' a ' ] = 0 ; res = res . Substring ( 0 , size - 1 ) ; size -- ; } res += s [ i ] ; vis [ s [ i ] - ' a ' ] = 1 ; } } return res ; } public static void Main ( ) { string S = " acbc " ; Console . WriteLine ( removeDuplicateLetters ( S ) ) ; } }