codeparrot/xlcost-text-to-code huggingface dataset:
Acronym words | C # implementation of the approach ; Function to return the number of strings that can be an acronym for other strings ; Frequency array to store the frequency of the first character of every string in the array ; To store the count of required strings ; Current word ; Frequency array to store the frequency of each of the character of the current string ; Check if the frequency of every character in the current string is <= its value in freq [ ] ; First character of the current string ; Driver code
using System ; class GFG { static int count_acronym ( int n , string [ ] arr ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] [ 0 ] - ' a ' ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string st = arr [ i ] ; int [ ] num = new int [ 26 ] ; for ( int j = 0 ; j < st . Length ; j ++ ) num [ st [ j ] - ' a ' ] ++ ; bool flag = true ; for ( int j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } int x = st [ 0 ] - ' a ' ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } public static void Main ( ) { string [ ] arr = { " abc " , " bcad " , " cabd " , " cba " , " dzzz " } ; int n = arr . Length ; Console . WriteLine ( count_acronym ( n , arr ) ) ; } }
Sub | C # implementation of the approach ; Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; To store frequency of each character ; Sub - strings of length 26 ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code
using System ; class GFG { static bool valid ( int [ ] cnt ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( cnt [ i ] >= 2 ) return false ; } return true ; } static string getGoodString ( string ss , int n ) { char [ ] s = ss . ToCharArray ( ) ; if ( n < 26 ) return " - 1" ; int [ ] cnt = new int [ 27 ] ; for ( int i = 25 ; i < n ; i ++ ) { for ( int j = i ; j >= i - 25 ; j -- ) { if ( s [ j ] != ' ? ' ) cnt [ ( ( int ) s [ j ] - ( int ) ' a ' ) ] ++ ; } if ( valid ( cnt ) ) { int cur = 0 ; while ( cnt [ cur ] > 0 ) cur ++ ; for ( int j = i - 25 ; j <= i ; j ++ ) { if ( s [ j ] == ' ? ' ) { s [ j ] = ( char ) ( cur + ( int ) ( ' a ' ) ) ; cur ++ ; while ( cnt [ cur ] > 0 ) cur ++ ; } } return new String ( s ) ; } } return " - 1" ; } static void Main ( ) { string s = " abcdefghijkl ? nopqrstuvwxy ? " ; int n = s . Length ; Console . WriteLine ( getGoodString ( s , n ) ) ; } }
Modify the string by swapping continuous vowels or consonants | C # implementation of the above approach ; Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code
using System ; class GFG { static bool isVowel ( char c ) { c = char . ToLower ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static String swapRepeated ( char [ ] str ) { for ( int i = 0 ; i < str . Length - 1 ; i ++ ) { char c = ( char ) 0 ; if ( ( isVowel ( str [ i ] ) && isVowel ( str [ i + 1 ] ) ) || ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i + 1 ] ) ) ) { c = str [ i ] ; str [ i ] = str [ i + 1 ] ; str [ i + 1 ] = c ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . WriteLine ( swapRepeated ( str . ToCharArray ( ) ) ) ; } }
Find the lexicographically largest palindromic Subsequence of a String | C # program to find the largest palindromic subsequence ; Function to find the largest palindromic subsequence ; Find the largest character ; Append all occurrences of largest character to the resultant string ; Driver Code
using System ; class GFG { static string largestPalinSub ( string s ) { string res = " " ; char mx = s [ 0 ] ; for ( int i = 1 ; i < s . Length ; i ++ ) mx = ( char ) Math . Max ( ( int ) mx , ( int ) s [ i ] ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( s [ i ] == mx ) res += s [ i ] ; return res ; } public static void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( largestPalinSub ( s ) ) ; } }
Generate lexicographically smallest string of 0 , 1 and 2 with adjacent swaps allowed | C # implementation of the approach ; Function to print the required string ; count number of 1 s ; To check if the all the 1 s have been used or not ; Print all the 1 s if any 2 is encountered ; If str [ i ] = 0 or str [ i ] = 2 ; If 1 s are not printed yet ; Driver code
using System ; class GFG { static void printString ( char [ ] str , int n ) { int ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) ones ++ ; bool used = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '2' && ! used ) { used = true ; for ( int j = 0 ; j < ones ; j ++ ) Console . Write ( "1" ) ; } if ( str [ i ] != '1' ) Console . Write ( str [ i ] ) ; } if ( ! used ) for ( int j = 0 ; j < ones ; j ++ ) Console . Write ( "1" ) ; } public static void Main ( String [ ] args ) { String str = "100210" ; int n = str . Length ; printString ( str . ToCharArray ( ) , n ) ; } }
K length words that can be formed from given characters without repetition | C # implementation of the approach ; Function to return the required count ; To store the count of distinct characters in str ; Traverse str character by character ; If current character is appearing for the first time in str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code
using System ; class solution { static int findPermutation ( string str , int k ) { bool [ ] has = new bool [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = false ; int cnt = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ! has [ str [ i ] - ' a ' ] ) { cnt ++ ; has [ str [ i ] - ' a ' ] = true ; } } int ans = 1 ; for ( int i = 2 ; i <= cnt ; i ++ ) ans *= i ; for ( int i = cnt - k ; i > 1 ; i -- ) ans /= i ; return ans ; } public static void Main ( ) { string str = " geeksforgeeks " ; int k = 4 ; Console . WriteLine ( findPermutation ( str , k ) ) ; } }
Find the number in a range having maximum product of the digits | C # Program to find the number in a range having maximum product of the digits ; Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings string a = l . ToString ( ) ; ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code
using System ; class GFG { static int product ( int x ) { int prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x /= 10 ; } return prod ; } static int findNumber ( int l , int r ) { string b = r . ToString ( ) ; int ans = r ; for ( int i = 0 ; i < b . Length ; i ++ ) { if ( b [ i ] == '0' ) continue ; char [ ] curr = b . ToCharArray ( ) ; curr [ i ] = ( char ) ( ( ( int ) ( curr [ i ] - ( int ) '0' ) - 1 ) + ( int ) ( '0' ) ) ; for ( int j = i + 1 ; j < curr . Length ; j ++ ) curr [ j ] = '9' ; int num = 0 ; for ( int j = 0 ; j < curr . Length ; j ++ ) num = num * 10 + ( curr [ j ] - '0' ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } static void Main ( ) { int l = 1 , r = 10 ; Console . WriteLine ( findNumber ( l , r ) ) ; l = 51 ; r = 62 ; Console . WriteLine ( findNumber ( l , r ) ) ; } }
Concatenate suffixes of a String | C # implementation of the approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
using System ; class GFG { static void printExpansion ( String str ) { for ( int i = ( int ) str . Length - 1 ; i >= 0 ; i -- ) { String subStr = str . Substring ( i ) ; Console . Write ( subStr ) ; } } static public void Main ( String [ ] args ) { String str = " geeks " ; printExpansion ( str ) ; } }
Construct a binary string following the given constraints | C # implementation of the approach ; Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code
using System ; class GFG { static void constructBinString ( int a , int b , int x ) { int d , i ; d = x / 2 ; if ( x % 2 == 0 && x / 2 != a ) { d -- ; Console . Write ( "0" ) ; a -- ; } for ( i = 0 ; i < d ; i ++ ) Console . Write ( "10" ) ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { Console . Write ( "1" ) ; } for ( i = 0 ; i < a ; i ++ ) { Console . Write ( "0" ) ; } } public static void Main ( ) { int a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ; } }
Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | C # implementation of the above approach ; Function to match whether there are always n consecutive b ' s ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code
using System ; public class GFG { static bool matchPattern ( string s ) { int count = 0 ; int n = s . Length ; int i = 0 ; while ( i < n ) { while ( i < n && s [ i ] == ' a ' ) { count ++ ; i ++ ; } while ( i < n && s [ i ] == ' b ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } public static void Main ( ) { string s = " bb " ; if ( matchPattern ( s ) == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Length of longest consecutive ones by at most one swap in a Binary String | C # program to find length of longest consecutive ones by at most one swap in a Binary String ; Function to calculate the length of the longest consecutive 1 's ; To count all 1 's in the string ; To store cumulative 1 's ; Counting cumulative 1 's from left ; If 0 then start new cumulative one from that i ; perform step 3 of the approach ; step 3 ; Driver Code ; string
using System ; class GFG { static int maximum_one ( string s , int n ) { int cnt_one = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) cnt_one ++ ; } int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; if ( s [ 0 ] == '1' ) left [ 0 ] = 1 ; else left [ 0 ] = 0 ; if ( s [ n - 1 ] == '1' ) right [ n - 1 ] = 1 ; else right [ n - 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) left [ i ] = left [ i - 1 ] + 1 ; else left [ i ] = 0 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) right [ i ] = right [ i + 1 ] + 1 ; else right [ i ] = 0 ; } int cnt = 0 , max_cnt = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( s [ i ] == '0' ) { int sum = left [ i - 1 ] + right [ i + 1 ] ; if ( sum < cnt_one ) cnt = sum + 1 ; else cnt = sum ; max_cnt = Math . Max ( max_cnt , cnt ) ; cnt = 0 ; } } return max_cnt ; } public static void Main ( ) { string s = "111011101" ; Console . WriteLine ( maximum_one ( s , s . Length ) ) ; } }
Maximum length substring with highest frequency in a string | C # program to find maximum occurred substring of a string ; Function to return maximum occurred substring of a string ; Size of the string ; To store maximum frequency ; To store string which has maximum frequency ; Return substring which has maximum frequency ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static string MaxFreq ( string str ) { int n = str . Length ; Dictionary < string , int > m = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string sp = " " ; for ( int j = i ; j < n ; j ++ ) { sp += str [ j ] ; if ( m . ContainsKey ( sp ) ) { m [ sp ] ++ ; } else { m [ sp ] = 1 ; } } } int maxi = 0 ; string s = " " ; foreach ( KeyValuePair < string , int > i in m ) { if ( i . Value > maxi ) { maxi = i . Value ; s = i . Key ; } else if ( i . Value == maxi ) { string ss = i . Key ; if ( ss . Length > s . Length ) s = ss ; } } return s ; } public static void Main ( string [ ] args ) { string str = " ababecdecd " ; Console . Write ( MaxFreq ( str ) ) ; } }
Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | C # program to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only ; Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver program
using System ; class GFG { static int maxFreq ( string s , int a , int b ) { int [ ] fre = new int [ 10 ] ; int n = s . Length ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } for ( int i = 0 ; i < n ; i ++ ) fre [ s [ i ] - '0' ] ++ ; if ( fre [ a ] == 0 && fre [ b ] == 0 ) return - 1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } public static void Main ( ) { int a = 4 , b = 7 ; string s = "47744" ; Console . WriteLine ( maxFreq ( s , a , b ) ) ; } }
Minimum steps to convert one binary string to other only using negation | C # implementation of the above approach ; Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code
using System ; class GFG { static void convert ( int n , String a , String b ) { int [ ] l = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) l [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) l [ i ] = 1 ; } int cc = 0 ; int vl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( l [ i ] == 0 ) { if ( vl != 0 ) cc += 1 ; vl = 0 ; } else vl += 1 ; } if ( vl != 0 ) cc += 1 ; Console . WriteLine ( cc ) ; } static public void Main ( ) { String a = "101010" ; String b = "110011" ; int n = a . Length ; convert ( n , a , b ) ; } }
Generate a sequence with the given operations | C # Implementation of above approach ; function to find minimum required permutation ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void StringMatch ( String s ) { int lo = 0 , hi = s . Length , len = s . Length ; List < int > ans = new List < int > ( ) ; for ( int x = 0 ; x < len ; x ++ ) { if ( s [ x ] == ' I ' ) { ans . Add ( lo ) ; lo += 1 ; } else { ans . Add ( hi ) ; hi -= 1 ; } } ans . Add ( lo ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] ) ; if ( i != ans . Count - 1 ) Console . Write ( " , " ) ; } Console . Write ( " ] " ) ; } public static void Main ( String [ ] args ) { String S = " IDID " ; StringMatch ( S ) ; } }
Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | C # program to find no of ways to swap bits of s1 so that bitwise OR of s1 and s2 changes ; Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code
using System ; class GFG { static int countWays ( string s1 , string s2 , int n ) { int a , b , c , d ; a = b = c = d = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s2 [ i ] == '0' ) { if ( s1 [ i ] == '0' ) { c ++ ; } else { d ++ ; } } else { if ( s1 [ i ] == '0' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } public static void Main ( ) { int n = 5 ; string s1 = "01011" ; string s2 = "11001" ; Console . WriteLine ( countWays ( s1 , s2 , n ) ) ; } }
Find the player who rearranges the characters to get a palindrome string first | C # program to print the winner of the game ; Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner
using System ; class GfG { static int returnWinner ( String s , int l ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 != 0 ) cnt ++ ; } if ( ( cnt == 0 ) || ( cnt & 1 ) == 1 ) return 1 ; else return 2 ; } public static void Main ( String [ ] args ) { String s = " abaaab " ; int l = s . Length ; int winner = returnWinner ( s , l ) ; Console . WriteLine ( " Player - " + winner ) ; } }
Maximum sum and product of the M consecutive digits in a number | C # implementation of the above approach ; Function to find the maximum product ; Driver code
using System ; class GFG { static void maxProductSum ( string str , int m ) { int n = str . Length ; int maxProd = int . MinValue , maxSum = int . MinValue ; for ( int i = 0 ; i <= n - m ; i ++ ) { int product = 1 , sum = 0 ; for ( int j = i ; j < m + i ; j ++ ) { product = product * ( str [ j ] - '0' ) ; sum = sum + ( str [ j ] - '0' ) ; } maxProd = Math . Max ( maxProd , product ) ; maxSum = Math . Max ( maxSum , sum ) ; } Console . WriteLine ( " Maximum ▁ Product ▁ = ▁ " + maxProd ) ; Console . Write ( " STRNEWLINE Maximum ▁ Sum ▁ = ▁ " + maxSum ) ; } public static void Main ( ) { string str = "3605356297" ; int m = 3 ; maxProductSum ( str , m ) ; } }
Replace all consonants with nearest vowels in a string | C # program to replace all consonants with nearest vowels in a string ; Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the bsolute difference of ascii value is considered ; when s [ i ] is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
using System ; public class Solution { static bool isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) return false ; return true ; } static String replacingConsonants ( String s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( isVowel ( s [ i ] ) ) continue ; else { if ( s [ i ] > ' a ' && s [ i ] < ' e ' ) { if ( Math . Abs ( s [ i ] - ' a ' ) > Math . Abs ( s [ i ] - ' e ' ) ) s = s . Substring ( 0 , i ) + ' e ' + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + ' a ' + s . Substring ( i + 1 ) ; } else if ( s [ i ] > ' e ' && s [ i ] < ' i ' ) { if ( Math . Abs ( s [ i ] - ' e ' ) > Math . Abs ( s [ i ] - ' i ' ) ) s = s . Substring ( 0 , i ) + ' i ' + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + ' e ' + s . Substring ( i + 1 ) ; } else if ( s [ i ] > ' i ' && s [ i ] < ' o ' ) { if ( Math . Abs ( s [ i ] - ' i ' ) > Math . Abs ( s [ i ] - ' o ' ) ) s = s . Substring ( 0 , i ) + ' o ' + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + ' i ' + s . Substring ( i + 1 ) ; } else if ( s [ i ] > ' o ' && s [ i ] < ' u ' ) { if ( Math . Abs ( s [ i ] - ' o ' ) > Math . Abs ( s [ i ] - ' u ' ) ) s = s . Substring ( 0 , i ) + ' u ' + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + ' o ' + s . Substring ( i + 1 ) ; } else if ( s [ i ] > ' u ' ) s = s . Substring ( 0 , i ) + ' u ' + s . Substring ( i + 1 ) ; } } return s ; } public static void Main ( ) { String s = " geeksforgeeks " ; Console . WriteLine ( replacingConsonants ( s ) ) ; } }
Find time taken for signal to reach all positions in a string | C # program to Find time taken for signal to reach all positions in a string ; Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code
using System ; class GFG { static int maxLength ( string s , int n ) { int right = 0 , left = 0 ; int coun = 0 , max_length = int . MinValue ; s = s + '1' ; for ( int i = 0 ; i <= n ; i ++ ) { if ( s [ i ] == ' o ' ) coun ++ ; else { if ( coun > max_length ) { right = 0 ; left = 0 ; if ( s [ i ] == ' x ' ) right = 1 ; if ( ( ( i - coun ) > 0 ) && ( s [ i - coun - 1 ] == ' x ' ) ) left = 1 ; coun = ( int ) Math . Ceiling ( ( double ) coun / ( right + left ) ) ; max_length = Math . Max ( max_length , coun ) ; } coun = 0 ; } } return max_length ; } public static void Main ( ) { string s = " oooxoooooooooxooo " ; int n = s . Length ; Console . Write ( maxLength ( s , n ) ) ; } }
Lexicographically largest string formed from the characters in range L and R | C # program to print the lexicographically largest String that can be formed from the characters in range L and R ; Function to return the lexicographically largest String ; hash array ; make 0 - based indexing ; iterate and count frequencies of character ; ans String ; iterate in frequency array ; add til all characters are added ; Driver Code
using System ; class GFG { static String printLargestString ( String s , int l , int r ) { int [ ] freq = new int [ 26 ] ; l -- ; r -- ; for ( int i = Math . Min ( l , r ) ; i <= Math . Max ( l , r ) ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } String ans = " " ; for ( int i = 25 ; i >= 0 ; i -- ) { while ( freq [ i ] > 0 ) { ans += ( char ) ( ' a ' + i ) ; freq [ i ] -- ; } } return ans ; } public static void Main ( ) { String s = " striver " ; int l = 3 , r = 5 ; Console . Write ( printLargestString ( s , l , r ) ) ; } }
Arrange a binary string to get maximum value within a range of indices | C # implementation of the approach ; Storing the count of 1 's in the String ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged String ; if after maximizing the ranges any 1 is left then we maximize the String lexicographically . ; Driver Code
using System ; class GFG { static void arrange ( String s ) { int cc = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) cc ++ ; } int [ ] a = new int [ s . Length + 1 ] ; int [ , ] qq = { { 2 , 3 } , { 5 , 5 } } ; int n = qq . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = qq [ i , 0 ] , r = qq [ i , 1 ] ; l -- ; r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } int len_a = a . Length ; for ( int i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } int [ ] zz = new int [ s . Length ] ; for ( int i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( int i = 0 ; i < s . Length ; i ++ ) Console . Write ( zz [ i ] ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { String str = "11100" ; arrange ( str ) ; } }