codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of chairs required to ensure that every worker is seated at any instant | C # program to implement the above approach ; Function to find the minimum number of chairs required to ensure that every worker is seated at any time ; Stores the number of chairs required ; Pointer to iterate ; Stores minimum number of chairs required ; Iterate over every character ; If character is ' E ' ; Increase the count ; Otherwise ; Update maximum value of count obtained at any given time ; Return mini ; Driver code ; Given String ; Function call to find the minimum number of chairs
using System ; using System . Collections . Generic ; class GFG { static int findMinimumChairs ( string s ) { int count = 0 ; int i = 0 ; int mini = Int32 . MinValue ; while ( i < s . Length ) { if ( s [ i ] == ' E ' ) count ++ ; else count -- ; mini = Math . Max ( count , mini ) ; i ++ ; } return mini ; } public static void Main ( ) { string s = " EELEE " ; Console . WriteLine ( findMinimumChairs ( s ) ) ; } }
Modify string by inserting characters such that every K | C # program for the above approach ; Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void fillString ( string str , int k ) { char [ ] s = str . ToCharArray ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp [ i % k ] = s [ i ] ; } } for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! mp . ContainsKey ( i % k ) ) { Console . WriteLine ( - 1 ) ; return ; } s [ i ] = ( char ) mp [ i % k ] ; } Console . WriteLine ( new string ( s ) ) ; } static void Main ( ) { string S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; } }
Rearrange a string S1 such that another given string S2 is not its subsequence | C # program for the above approach ; Function to rearrange characters in string S1 such that S2 is not a subsequence of it ; Store the frequencies of characters of string s2 ; Traverse the string s2 ; Update the frequency ; Find the number of unique characters in s2 ; Increment unique by 1 if the condition satisfies ; Check if the number of unique characters in string s2 is 1 ; Store the unique character frequency ; Store occurence of it in s1 ; Find count of that character in the string s1 ; Increment count by 1 if that unique character is same as current character ; If count count_in_s1 is less than count_in_s2 , then print s1 and return ; Otherwise , there is no possible arrangement ; Checks if any character in s2 is less than its next character ; Iterate the string , s2 ; If s [ i ] is greater than the s [ i + 1 ] ; Set inc to 0 ; If inc = 1 , print s1 in decreasing order ; Otherwise , print s1 in increasing order ; Driver code
using System ; class GFG { static void rearrangeString ( char [ ] s1 , char [ ] s2 ) { int [ ] cnt = new int [ 26 ] ; for ( int i = 0 ; i < s2 . Length ; i ++ ) cnt [ s2 [ i ] - ' a ' ] ++ ; int unique = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( cnt [ i ] != 0 ) unique ++ ; if ( unique == 1 ) { int count_in_s2 = s2 . Length ; int count_in_s1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) if ( s1 [ i ] == s2 [ 0 ] ) count_in_s1 ++ ; if ( count_in_s1 < count_in_s2 ) { Console . Write ( new string ( s1 ) ) ; return ; } Console . Write ( - 1 ) ; } else { int inc = 1 ; for ( int i = 0 ; i < s2 . Length - 1 ; i ++ ) if ( s2 [ i ] > s2 [ i + 1 ] ) inc = 0 ; if ( inc == 1 ) { Array . Sort ( s1 ) ; Array . Reverse ( s1 ) ; Console . Write ( new string ( s1 ) ) ; } else { Array . Sort ( s1 ) ; Console . Write ( new string ( s1 ) ) ; } } } static void Main ( ) { char [ ] s1 = " abcd " . ToCharArray ( ) ; char [ ] s2 = " ab " . ToCharArray ( ) ; rearrangeString ( s1 , s2 ) ; } }
Check if a string can be emptied by removing all subsequences of the form "10" | C # program for the above approach ; Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isReducible ( string str ) { int N = str . Length ; List < char > s = new List < char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '1' ) s . Add ( str [ i ] ) ; else if ( s . Count > 0 ) s . RemoveAt ( s . Count - 1 ) ; else return false ; } if ( s . Count == 0 ) { return true ; } else { return false ; } } static void Main ( ) { string str = "11011000" ; if ( isReducible ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimize flips required such that string does not any pair of consecutive 0 s | C # program for the above approach ; Function to find minimum flips required such that a String does not contain any pair of consecutive 0 s ; Stores minimum count of flips ; Iterate over the characters of the String ; If two consecutive characters are equal to '0' ; Update S [ i + 1 ] ; Update cntOp ; Driver Code
using System ; class GFG { static int cntMinOperation ( char [ ] S , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] == '0' && S [ i + 1 ] == '0' ) { S [ i + 1 ] = '1' ; cntOp += 1 ; } } return cntOp ; } public static void Main ( string [ ] args ) { string S = "10001" ; int N = S . Length ; Console . WriteLine ( cntMinOperation ( S . ToCharArray ( ) , N ) ) ; } }
Rearrange a string to maximize the minimum distance between any pair of vowels | C # program for the above approach ; Function to rearrange the String such that the minimum distance between any of vowels is maximum . ; Store vowels and consonants ; Iterate over the characters of String ; If current character is a vowel ; If current character is a consonant ; Stores count of vowels and consonants respectively ; Stores the resultant String ; Stores count of consonants appended into ans ; Append vowel to ans ; Append consonants ; Append consonant to ans ; Update temp ; Remove the taken elements of consonant ; Return final ans ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static String solution ( string s ) { List < char > vowel = new List < char > ( ) ; List < char > consonant = new List < char > ( ) ; foreach ( char i in s . ToCharArray ( ) ) { if ( i == ' a ' i == ' e ' i == ' i ' i == ' o ' i == ' u ' ) { vowel . Add ( i ) ; } else { consonant . Add ( i ) ; } } int Nc , Nv ; Nv = vowel . Count ; Nc = consonant . Count ; int M = Nc / ( Nv - 1 ) ; string ans = " " ; int consotnant_till = 0 ; foreach ( char i in vowel ) { ans += i ; int temp = 0 ; for ( int j = consotnant_till ; j < Math . Min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant [ j ] ; temp ++ ; } consotnant_till += temp ; } return ans ; } static public void Main ( ) { String str = " aaaabbbcc " ; Console . WriteLine ( solution ( str ) ) ; } }
Lexicographically smallest string possible by performing K operations on a given string | C # program to implement the above approach ; Function to find the lexicographically smallest possible string by performing K operations on string S ; Store the size of string , s ; Check if k >= n , if true , convert every character to ' a ' ; Iterate in range [ 0 , n - 1 ] using i ; When k reaches 0 , break the loop ; If current character is ' a ' , continue ; Otherwise , iterate in the range [ i + 1 , n - 1 ] using j ; Check if s [ j ] > s [ i ] ; If true , set s [ j ] = s [ i ] and break out of the loop ; Check if j reaches the last index ; Update S [ i ] ; Decrement k by 1 ; Print string ; Driver code ; Given String , s ; Given k ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void smallestlexicographicstring ( char [ ] s , int k ) { int n = s . Length ; if ( k >= n ) { for ( int i = 0 ; i < n ; i ++ ) { s [ i ] = ' a ' ; } Console . Write ( s ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) { if ( k == 0 ) { break ; } if ( s [ i ] == ' a ' ) continue ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( s [ j ] > s [ i ] ) { s [ j ] = s [ i ] ; break ; } else if ( j == n - 1 ) s [ j ] = s [ i ] ; } s [ i ] = ' a ' ; k -- ; } Console . Write ( s ) ; } static void Main ( ) { char [ ] s = ( " geeksforgeeks " ) . ToCharArray ( ) ; int k = 6 ; smallestlexicographicstring ( s , k ) ; } }
Minimize removal of non | C # program for the above approach ; Function to find minimum count of steps required ot make String S an empty String ; Stores count of occurences ' ( ' ; Stores count of occurences ' ) ' ; Traverse the String , str ; If current character is ' ( ' ; Update count_1 ; Update count_2 ; If all the characters are same , then print - 1 ; If the count of occurence of ' ) ' and ' ( ' are same then print 0 ; If length of String is Odd ; Driver Code ; Given String ; Size of the String ; Function Call
using System ; class GFG { static void canReduceString ( String S , int N ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ( ' ) { count_1 ++ ; } else { count_2 ++ ; } } if ( count_1 == 0 count_2 == 0 ) { Console . Write ( " - 1" + " STRNEWLINE " ) ; } else if ( count_1 == count_2 ) { Console . Write ( "0" + " STRNEWLINE " ) ; } else if ( N % 2 != 0 ) { Console . Write ( " - 1" ) ; } else { Console . Write ( Math . Abs ( count_1 - count_2 ) / 2 ) ; } } public static void Main ( String [ ] args ) { String S = " ) ) ) ( ( ( " ; int N = S . Length ; canReduceString ( S , N ) ; } }
Program to construct a DFA which accepts the language L = { aN | N â ‰¥ 1 } | C # program for the above approach ; Function to check whether the String S satisfy the given DFA or not ; Stores the count of characters ; Iterate over the range [ 0 , N ] ; Count and check every element for ' a ' ; If String matches with DFA ; If not matches ; Driver Code ; Function Call
using System ; class GFG { static void isAcceptedDFA ( String s , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' a ' ) count ++ ; } if ( count == N && count != 0 ) { Console . Write ( " Accepted " ) ; } else { Console . Write ( " Not ▁ Accepted " ) ; } } public static void Main ( String [ ] args ) { String S = " aaaaa " ; isAcceptedDFA ( S , S . Length ) ; } }
Maximize palindromic strings of length 3 possible from given count of alphabets | C # program for the above approach ; Function to count maximum number of palindromic String of length 3 ; Stores the readonly count of palindromic Strings ; Traverse the array ; Increment res by arr [ i ] / 3 , i . e forming String of only i + ' a ' character ; Store remainder ; Increment c1 by one , if current frequency is 1 ; Increment c2 by one , if current frequency is 2 ; Count palindromic Strings of length 3 having the character at the ends different from that present in the middle ; Update c1 and c2 ; Increment res by 2 * c2 / 3 ; Finally print the result ; Driver Code ; Given array ; Function Call
using System ; public class GFG { static void maximum_pallindromic ( int [ ] arr ) { int res = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { res += arr [ i ] / 3 ; arr [ i ] = arr [ i ] % 3 ; if ( arr [ i ] == 1 ) c1 ++ ; else if ( arr [ i ] == 2 ) c2 ++ ; } res += Math . Min ( c1 , c2 ) ; int t = Math . Min ( c1 , c2 ) ; c1 -= t ; c2 -= t ; res += 2 * ( c2 / 3 ) ; c2 %= 3 ; res += c2 / 2 ; Console . Write ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; maximum_pallindromic ( arr ) ; } }
Find the winner of game of repeatedly removing the first character to empty given string | C # program to implement the above approach ; Function to find the winner of a game of repeatedly removing the first character to empty a String ; Store characters of each String of the array [ ] arr ; Stores count of Strings in [ ] arr ; Traverse the array [ ] arr ; Stores length of current String ; Traverse the String ; Insert arr [ i , j ] ; 1 st Player starts the game ; Stores the player number for the next turn ; Remove 1 st character of current String ; Update player number for the next turn ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void find_Winner ( String [ ] arr , int N ) { List < char > [ ] Q = new List < char > [ N ] ; for ( int i = 0 ; i < Q . Length ; i ++ ) Q [ i ] = new List < char > ( ) ; int M = arr . Length ; for ( int i = 0 ; i < M ; i ++ ) { int len = arr [ i ] . Length ; for ( int j = 0 ; j < len ; j ++ ) { Q [ i ] . Add ( arr [ i ] [ j ] ) ; } } int player = 0 ; while ( Q [ player ] . Count > 0 ) { int nextPlayer = Q [ player ] [ 0 ] - '0' - 1 ; Q [ player ] . RemoveAt ( 0 ) ; player = nextPlayer ; } Console . Write ( " Player ▁ " + ( player + 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; String [ ] arr = { "323" , "2" , "2" } ; find_Winner ( arr , N ) ; } }
Longest Substring that can be made a palindrome by swapping of characters | C # program for the above approach ; Function to find the Longest substring that can be made a palindrome by swapping of characters ; Initialize dp array of size 1024 ; Initializeing dp array with length of s ; Initializing mask and res ; Traverse the string ; Find the mask of the current character ; Finding the length of the longest substring in s which is a palindrome for even count ; Finding the length of the longest substring in s which is a palindrome for one odd count ; Finding maximum length of substring having one odd count ; dp [ mask ] is minimum of current i and dp [ mask ] ; Return longest length of the substring which forms a palindrome with swaps ; Driver Code ; Input String ; Function Call
using System ; class GFG { public static int longestSubstring ( string s ) { int [ ] dp = new int [ 1024 ] ; for ( int i = 0 ; i < 1024 ; ++ i ) { dp [ i ] = s . Length ; } int res = 0 , mask = 0 ; dp [ 0 ] = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { mask = mask ^ ( 1 << ( s [ i ] - '0' ) ) ; res = Math . Max ( res , i - dp [ mask ] ) ; for ( int j = 0 ; j < 10 ; j ++ ) { res = Math . Max ( res , i - dp [ mask ^ ( 1 << j ) ] ) ; } dp [ mask ] = Math . Min ( dp [ mask ] , i ) ; } return res ; } public static void Main ( string [ ] args ) { string s = "3242415" ; Console . WriteLine ( longestSubstring ( s ) ) ; } }
Convert given string to a valid mobile number | C # program for the above approach ; Function to print the valid and formatted phone number ; Length of given String ; Store digits in temp ; Iterate given M ; If any digit , append it to temp ; Find new length of String ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given String ; Function Call
using System ; class GFG { static void Validate ( string M ) { int len = M . Length ; string temp = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( Char . IsDigit ( M [ i ] ) ) temp += M [ i ] ; } int nwlen = temp . Length ; if ( nwlen != 10 ) { Console . Write ( " Invalid STRNEWLINE " ) ; return ; } string res = " " ; string x = temp . Substring ( 0 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 3 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 6 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 9 , 1 ) ; res += " ( " + x + " ) " ; Console . WriteLine ( res ) ; } public static void Main ( string [ ] args ) { string M = "91 ▁ 234rt5%34*0 ▁ 3" ; Validate ( M ) ; } }
Modulus of two Hexadecimal Numbers | C # program to implement the above approach ; Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void hexaModK ( String N , String k ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( char i = '0' ; i <= '9' ; i ++ ) { map . Add ( i , i - '0' ) ; } map . Add ( ' A ' , 10 ) ; map . Add ( ' B ' , 11 ) ; map . Add ( ' C ' , 12 ) ; map . Add ( ' D ' , 13 ) ; map . Add ( ' E ' , 14 ) ; map . Add ( ' F ' , 15 ) ; long m = long . Parse ( k ) ; long Base = 1 ; long ans = 0 ; for ( int i = N . Length - 1 ; i >= 0 ; i -- ) { long n = map [ N [ i ] ] % m ; ans = ( ans + ( Base % m * n % m ) % m ) % m ; Base = ( Base % m * 16 % m ) % m ; } Console . WriteLine ( ans . ToString ( " X " ) ) ; } public static void Main ( String [ ] args ) { String n = "3E8" ; String k = "13" ; hexaModK ( n , k ) ; } }
Print all combinations generated by characters of a numeric string which does not exceed N | C # program for the above approach ; Store the current sequence of s ; Store the all the required sequences ; Function to print all sequences of S satisfying the required condition ; Print all Strings in the set ; Function to generate all sequences of String S that are at most N ; Iterate over String s ; Push ith character to combination ; Convert the String to number ; Check if the condition is true ; Push the current String to the readonly set of sequences ; Recursively call function ; Backtrack to its previous state ; Driver Code ; Function Call ; Print required sequences
using System ; using System . Collections . Generic ; class GFG { static String combination = " " ; static SortedSet < String > combinations = new SortedSet < String > ( ) ; static void printSequences ( SortedSet < String > combinations ) { foreach ( String s in combinations ) { Console . Write ( s + " ▁ " ) ; } } static void generateCombinations ( String s , int n ) { for ( int i = 0 ; i < s . Length ; i ++ ) { combination += ( s [ i ] ) ; long x = Int32 . Parse ( combination ) ; if ( x <= n ) { combinations . Add ( combination ) ; generateCombinations ( s , n ) ; } combination = combination . Substring ( 0 , combination . Length - 1 ) ; } } public static void Main ( String [ ] args ) { String S = "124" ; int N = 100 ; generateCombinations ( S , N ) ; printSequences ( combinations ) ; } }
Count Distinct Strings present in an array using Polynomial rolling hash function | C # program to implement the above approach ; Function to find the hash value of a string ; Traverse the string ; Update hash_val ; Update mul ; Return hash_val of str ; Function to find the count of distinct strings present in the given array ; Store the hash values of the strings ; Traverse the array ; Stores hash value of arr [ i ] ; Sort hash [ ] array ; Stores count of distinct strings in the array ; Traverse hash [ ] array ; Update cntElem ; Driver Code
using System ; class GFG { static int compute_hash ( string str ) { int p = 31 ; int MOD = ( int ) 1e9 + 7 ; int hash_val = 0 ; int mul = 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char ch = str [ i ] ; hash_val = ( hash_val + ( ch - ' a ' + 1 ) * mul ) % MOD ; mul = ( mul * p ) % MOD ; } return hash_val ; } static int distinct_str ( string [ ] arr , int n ) { int [ ] hash = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ i ] = compute_hash ( arr [ i ] ) ; } Array . Sort ( hash ) ; int cntElem = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( hash [ i ] != hash [ i - 1 ] ) { cntElem ++ ; } } return cntElem ; } public static void Main ( String [ ] args ) { string [ ] arr = { " abcde " , " abcce " , " abcdf " , " abcde " } ; int N = arr . Length ; Console . WriteLine ( distinct_str ( arr , N ) ) ; } }
Remove characters from given string whose frequencies are a Prime Number | C # program for the above approach ; Function to perform the seive of eratosthenes algorithm ; Initialize all entries in prime [ ] as true ; Initialize 0 and 1 as non prime ; Traversing the prime array ; If i is prime ; All multiples of i must be marked false as they are non prime ; Function to remove characters which have prime frequency in the String ; Length of the String ; Create a bool array prime ; Sieve of Eratosthenes ; Stores the frequency of character ; Storing the frequencies ; New String that will be formed ; Removing the characters which have prime frequencies ; If the character has prime frequency then skip ; Else concatenate the character to the new String ; Print the modified String ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void SieveOfEratosthenes ( bool [ ] prime , int n ) { for ( int i = 0 ; i <= n ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = 2 ; i * j <= n ; j ++ ) { prime [ i * j ] = false ; } } } } static void removePrimeFrequencies ( char [ ] s ) { int n = s . Length ; bool [ ] prime = new bool [ n + 1 ] ; SieveOfEratosthenes ( prime , n ) ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( m . ContainsKey ( s [ i ] ) ) { m [ s [ i ] ] ++ ; } else { m . Add ( s [ i ] , 1 ) ; } } String new_String = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( prime [ m [ s [ i ] ] ] ) continue ; new_String += s [ i ] ; } Console . Write ( new_String ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; removePrimeFrequencies ( str . ToCharArray ( ) ) ; } }
Rearrange string such that no pair of adjacent characters are of the same type | C # program to implement the above approach ; Function to rearrange given alphanumeric String such that no two adjacent characters are of the same type ; Stores alphabets and digits ; Store the alphabets and digits separately in the Strings ; Stores the count of alphabets and digits ; If respective counts differ by 1 ; Desired arrangement not possible ; Stores the indexes ; Check if first character should be alphabet or digit ; Place alphabets and digits alternatively ; If current character needs to be alphabet ; If current character needs to be a digit ; Flip flag for alternate arrangement ; Return resultant String ; Driver Code ; Given String ; Function Call
using System ; class GFG { static String rearrange ( String s ) { String s1 = " " , s2 = " " , ans = " " ; char [ ] s3 = s . ToCharArray ( ) ; foreach ( char x in s3 ) { if ( x >= ' a ' && x <= ' z ' ) s1 += x ; else s2 += x ; } int n = s1 . Length ; int m = s2 . Length ; if ( Math . Abs ( n - m ) > 1 ) return " - 1" ; int i = 0 , j = 0 , k = 0 ; int flag = ( n >= m ) ? 1 : 0 ; while ( i < n && j < m ) { if ( flag != 0 ) ans += s1 [ i ++ ] ; else ans += s2 [ j ++ ] ; if ( flag == 1 ) flag = 0 ; else flag = 1 ; } return ans ; } public static void Main ( String [ ] args ) { String str = " geeks2020" ; Console . Write ( rearrange ( str ) + " STRNEWLINE " ) ; } }
Find value after N operations to remove N characters of string S with given constraints | C # program for the above approach ; Function to find the value after N operations to remove all the N characters of String S ; Iterate till N ; Remove character at ind and decrease n ( size of String ) ; Increase count by ind + 1 ; Driver Code ; Given String str ; Function call
using System ; class GFG { static void charactersCount ( String str , int n ) { int count = 0 ; while ( n > 0 ) { char cur = str [ 0 ] ; int ind = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( str [ j ] < cur ) { cur = str [ j ] ; ind = j ; } } str = str . Substring ( 0 , ind ) + str . Substring ( ind + 1 ) ; n -- ; count += ind + 1 ; } Console . Write ( count + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " aabbc " ; int n = 5 ; charactersCount ( str , n ) ; } }