codeparrot/xlcost-text-to-code huggingface dataset:
Final string after performing given operations | C # implementation of the approach ; Function to return the modified String ; Count number of ' x ' ; } Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Code
using System ; class GFG { static string printFinalString ( string s ) { int i , n ; n = s . Length ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' x ' ) { x ++ ; else { y ++ ; } } string finalString = " " ; if ( x > y ) { for ( i = 0 ; i < x - y ; i ++ ) { finalString += " x " ; } } else { for ( i = 0 ; i < y - x ; i ++ ) { finalString += " y " ; } } return finalString ; } public static void Main ( ) { string s = " xxyyxyy " ; Console . WriteLine ( printFinalString ( s ) ) ; } }
String which when repeated exactly K times gives a permutation of S | C # program to find a string which when repeated exactly k times gives a permutation of the given string ; Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
using System ; class GFG { static String K_String ( String s , int k ) { int n = s . Length ; int [ ] fre = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) fre [ s [ i ] - ' a ' ] ++ ; String str = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { int x = fre [ i ] / k ; while ( x != 0 ) { str += ( char ) ( i + ' a ' ) ; x -- ; } } else { return " - 1" ; } } return str ; } public static void Main ( String [ ] args ) { String s = " aabb " ; int k = 2 ; Console . WriteLine ( K_String ( s , k ) ) ; } }
Minimum deletions from string to reduce it to string with at most 2 unique characters | C # implementation of the above approach ; Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver program to test above functions
using System ; public class GFG { static int check ( string s ) { int i , j ; int [ ] fr = new int [ 26 ] ; int n = s . Length ; for ( i = 0 ; i < n ; i ++ ) { char x = s [ i ] ; fr [ x - ' a ' ] += 1 ; } int minimum = int . MaxValue ; for ( i = 0 ; i < 26 ; i ++ ) { for ( j = i + 1 ; j < 26 ; j ++ ) { int z = fr [ i ] + fr [ j ] ; minimum = Math . Min ( minimum , n - z ) ; } } return minimum ; } public static void Main ( ) { string s = " geeksforgeeks " ; Console . Write ( check ( s ) ) ; } }
Count and Print the alphabets having ASCII value not in the range [ l , r ] | C # implementation of the above approach ; Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int CountCharacters ( string str , int l , int r ) { int cnt = 0 ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str [ i ] && str [ i ] <= r ) ) { cnt ++ ; if ( ! m . ContainsKey ( str [ i ] ) ) { m [ str [ i ] ] = 0 ; Console . Write ( str [ i ] + " ▁ " ) ; } m [ str [ i ] ] ++ ; } } return cnt ; } public static void Main ( string [ ] args ) { string str = " geeksforgeeks " ; int l = 102 , r = 111 ; Console . Write ( " Characters ▁ with ▁ ASCII ▁ values " + " not ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ STRNEWLINE in " + " the ▁ given ▁ string ▁ are : ▁ " ) ; Console . WriteLine ( " and count is " CountCharacters ( str , l , r ) ) ; } }
Count the number of words having sum of ASCII values less than and greater than k | C # implementation of the above approach ; Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; else Add the ascii value to sum ; Handling the Last word separately ; Driver code
using System ; class GFG { static void CountWords ( String str , int k ) { int sum = 0 ; int NumberOfWords = 0 ; int counter = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; ++ i ) { if ( str [ i ] == ' ▁ ' ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } { sum += str [ i ] ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } Console . WriteLine ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ " + " of ▁ ASCII ▁ values ▁ less ▁ than ▁ k ▁ = ▁ " + counter ) ; Console . WriteLine ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ " + " ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = ▁ " + ( NumberOfWords - counter ) ) ; } public static void Main ( String [ ] args ) { String str = " Learn ▁ how ▁ to ▁ code " ; int k = 400 ; CountWords ( str , k ) ; } }
Rearrange a string in the form of integer sum followed by the minimized character | C # implementation of above approach ; function to return maximum volume ; separate digits and alphabets ; change digit sum to string ; change alphabet sum to string ; concatenate sum to alphabets string ; Driver code
using System ; public class Solution { static String separateChar ( String str ) { int n = str . Length , digitSum = 0 ; int alphabetSum = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) { digitSum += ( int ) ( str [ i ] - '0' ) ; } else { alphabetSum += str [ i ] - ' a ' + 1 ; alphabetSum %= 26 ; } } String sumStr = " " + ( digitSum ) ; char alphabetStr = ( char ) ( alphabetSum + ' a ' - 1 ) ; sumStr += alphabetStr ; return sumStr ; } public static void Main ( ) { String str = "3652adyz3423" ; Console . WriteLine ( separateChar ( str ) ) ; } }
Find the count of palindromic sub | C # program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; driver program
using System ; public class GFG { readonly static int MAX_CHAR = 26 ; static int countPalindrome ( String str ) { int n = str . Length ; int sum = 0 ; int [ ] hashTable = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] != 0 ) { sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } } return sum ; } public static void Main ( ) { String str = " ananananddd " ; Console . Write ( countPalindrome ( str ) ) ; } }
Minimum number of elements to be removed so that pairwise consecutive elements are same | C # implementation of above approach ; Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
using System ; public class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { i ++ ; } else { count ++ ; } } return count ; } public static void Main ( ) { String str = "44522255" ; Console . WriteLine ( countConsecutive ( str ) ) ; } }
Smallest odd digits number not less than N | C # program to print the smallest integer not less than N with all odd digits ; function to check if all digits are odd of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 == 0 ) if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
using System ; class GFG { static int check_digits ( int n ) { while ( n != 0 ) { return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) == 1 ) return i ; } static void Main ( ) { int N = 2397 ; Console . WriteLine ( smallest_number ( N ) ) ; } }
Smallest odd digits number not less than N | C # program to print the smallest integer not less than N with all odd digits ; function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
using System ; public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int digit = s [ i ] - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; } num = num * 10 + ( s [ index ] - '0' + 1 ) ; for ( int i = index + 1 ; i < s . Length ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void Main ( ) { int N = 2397 ; Console . WriteLine ( smallestNumber ( N ) ) ; } }
Count and Print the alphabets having ASCII value in the range [ l , r ] | C # implementation of the above approach ; Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code
using System ; class GFG { static int CountCharacters ( string str , int l , int r ) { int cnt = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( l <= str [ i ] && str [ i ] <= r ) { cnt ++ ; Console . Write ( str [ i ] + " ▁ " ) ; } } return cnt ; } public static void Main ( ) { string str = " geeksforgeeks " ; int l = 102 , r = 111 ; Console . Write ( " Characters ▁ with ▁ ASCII ▁ values " + " ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ are ▁ STRNEWLINE " ) ; Console . Write ( " and count is " CountCharacters ( str , l , r ) ) ; } }
Minimum steps to remove substring 010 from a binary string | C # program to calculate steps to remove substring 010 from a binary string ; Function to find the minimum steps ; substring "010" found ; Driver code ; Get the binary string ; Find the minimum steps
using System ; class GFG { static int minSteps ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . Length - 2 ; i ++ ) { if ( ( ( int ) str [ i ] ) == '0' ) { if ( str [ i + 1 ] == '1' ) { if ( str [ i + 2 ] == '0' ) { count ++ ; i += 2 ; } } } } return count ; } public static void Main ( ) { string str = "0101010" ; Console . Write ( minSteps ( str ) ) ; } }
XOR of Prime Frequencies of Characters in a String | C # program to find XOR of Prime Frequencies of Characters in a String ; Function to create Sieve to check primes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void SieveOfEratosthenes ( Boolean [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int xorOfPrime ( char [ ] s ) { Boolean [ ] prime = new Boolean [ 100005 ] ; for ( int i = 0 ; i < 100005 ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , 10005 ) ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { var v = mp [ s [ i ] ] + 1 ; mp . Remove ( s [ i ] ) ; mp . Add ( s [ i ] , v ) ; } else { mp . Add ( s [ i ] , 1 ) ; } } int result = 0 ; int flag = 0 ; foreach ( KeyValuePair < char , int > entry in mp ) { if ( prime [ entry . Value ] ) { result ^= entry . Value ; flag = 1 ; } } if ( flag != 1 ) return - 1 ; return result ; } public static void Main ( String [ ] args ) { char [ ] s = " gggggeeekkkks " . ToCharArray ( ) ; Console . WriteLine ( xorOfPrime ( s ) ) ; } }
Count of alphabets having ASCII value less than and greater than k | C # implementation of the above approach ; Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code
using System ; class GFG { static int CountCharacters ( String str , int k ) { int cnt = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( ( ( int ) str [ i ] ) < k ) cnt ++ ; } return cnt ; } public static void Main ( ) { String str = " GeeksForGeeks " ; int k = 90 ; int count = CountCharacters ( str , k ) ; Console . WriteLine ( " Characters ▁ with ▁ ASCII ▁ values " + " less ▁ than ▁ K ▁ are ▁ " + count ) ; Console . WriteLine ( " Characters ▁ with ▁ ASCII ▁ values ▁ greater " + " than ▁ or ▁ equal ▁ to ▁ K ▁ are ▁ " + ( str . Length - count ) ) ; } }
De Bruijn sequence | Set 1 | C # implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
using System ; using System . Collections . Generic ; class GFG { static HashSet < String > seen = new HashSet < String > ( ) ; static List < int > edges = new List < int > ( ) ; static void dfs ( String node , int k , String A ) { for ( int i = 0 ; i < k ; ++ i ) { String str = node + A [ i ] ; if ( ! seen . Contains ( str ) ) { seen . Add ( str ) ; dfs ( str . Substring ( 1 ) , k , A ) ; edges . Add ( i ) ; } } } static String deBruijn ( int n , int k , String A ) { seen . Clear ( ) ; edges . Clear ( ) ; String startingNode = strings ( n - 1 , A [ 0 ] ) ; dfs ( startingNode , k , A ) ; String S = " " ; int l = ( int ) Math . Pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A [ edges [ i ] ] ; S += startingNode ; return S ; } private static String strings ( int n , char charAt ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } public static void Main ( String [ ] args ) { int n = 3 , k = 2 ; String A = "01" ; Console . Write ( deBruijn ( n , k , A ) ) ; } }
Number of words in a camelcase sequence | C # code to find the count of words in a CamelCase sequence ; Function to find the count of words in a CamelCase sequence ; Driver code
using System ; class GFG { static int countWords ( String str ) { int count = 1 ; for ( int i = 1 ; i < str . Length - 1 ; i ++ ) { if ( str [ i ] >= 65 && str [ i ] <= 90 ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { String str = " geeksForGeeks " ; Console . Write ( countWords ( str ) ) ; } }
Check whether frequency of characters in a string makes Fibonacci Sequence | C # program to check whether frequency of characters in a string makes Fibonacci Sequence ; Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; List to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static String isFibonacci ( String s ) { int i = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { var val = mp [ s [ i ] ] ; mp . Remove ( s [ i ] ) ; mp . Add ( s [ i ] , val + 1 ) ; } else { mp . Add ( s [ i ] , 1 ) ; } } List < int > v = new List < int > ( ) ; int n = mp . Count ; int a = 1 , b = 1 ; int c ; v . Add ( a ) ; v . Add ( b ) ; for ( i = 0 ; i < n - 2 ; i ++ ) { v . Add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value != v [ i ] ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void Main ( String [ ] args ) { String s = " abeeedd " ; Console . WriteLine ( isFibonacci ( s ) ) ; } }
Lexicographically smallest string formed by removing at most one character | C # program to find the lexicographically smallest String by removing at most one character ; Function to return the smallest String ; iterate the String ; first point where s [ i ] > s [ i + 1 ] ; append the String without i - th character in it ; leave the last character ; Driver Code
using System ; class GFG { static String smallest ( String s ) { int l = s . Length ; String ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s [ j ] ; } } return ans ; } } ans = s . Substring ( 0 , l - 1 ) ; return ans ; } public static void Main ( ) { String s = " abcda " ; Console . Write ( smallest ( s ) ) ; } }
Arrangement of the characters of a word such that all vowels are at odd places | C # program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
using System ; class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } static int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countPermutations ( String str ) { int even = ( int ) Math . Floor ( ( double ) ( str . Length / 2 ) ) ; int odd = str . Length - even ; int ways = 0 ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ ( int ) ( str [ i ] - ' a ' ) ] ++ ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . Length - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } static void Main ( ) { String str = " geeks " ; Console . WriteLine ( countPermutations ( str ) ) ; } }
Replace all consonants with nearest vowels in a string | C # program to replace all consonants with nearest vowels in a string ; Function to replace consonant with nearest vowels ; Driver code
using System ; public class solution { static String replacingConsonants ( String s ) { String str = " aaaeeeeiiiiioooooouuuuuuuu " ; char [ ] st = s . ToCharArray ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { int index = st [ i ] - ' a ' ; st [ i ] = str [ index ] ; } String str1 = new String ( st ) ; return str1 ; } public static void Main ( ) { String s = " geeksforgeeks " ; Console . WriteLine ( replacingConsonants ( s ) ) ; } }
Replace consonants with next immediate consonants alphabetically in a String | C # program of above approach ; Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
using System ; class GFG { static bool isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) { return false ; } return true ; } static String replaceConsonants ( char [ ] s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' z ' ) { s [ i ] = ' b ' ; } else { s [ i ] = ( char ) ( s [ i ] + 1 ) ; if ( isVowel ( s [ i ] ) ) { s [ i ] = ( char ) ( s [ i ] + 1 ) ; } } } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . WriteLine ( replaceConsonants ( s . ToCharArray ( ) ) ) ; } }
Remove characters from string that appears strictly less than K times | C # program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
using System ; class GFG { readonly static int MAX_CHAR = 26 ; static String removeChars ( String str , int k ) { int [ ] hash = new int [ MAX_CHAR ] ; int n = str . Length ; for ( int i = 0 ; i < n ; ++ i ) { hash [ str [ i ] - ' a ' ] ++ ; } String res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str [ i ] - ' a ' ] >= k ) { res += str [ i ] ; } } return res ; } static public void Main ( ) { String str = " geeksforgeeks " ; int k = 2 ; Console . WriteLine ( removeChars ( str , k ) ) ; } }
Count changes in Led Lights to display digits one by one | C # program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code
using System ; class GFG { public static int countOnOff ( string n ) { int [ ] Led = new int [ ] { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 } ; int len = n . Length ; int sum = Led [ n [ 0 ] - '0' ] ; for ( int i = 1 ; i < len ; i ++ ) { sum = sum + Math . Abs ( Led [ n [ i ] - '0' ] - Led [ n [ i - 1 ] - '0' ] ) ; } return sum ; } public static void Main ( string [ ] args ) { string n = "082" ; Console . WriteLine ( countOnOff ( n ) ) ; } }
Program to check if all characters have even frequency | C # implementation of the approach ; creating a frequency array ; Finding length of s ; counting frequency of all characters ; checking if any odd frequency is there or not ; Driver Code
using System ; class GFG { static Boolean check ( String s ) { int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < s . Length ; i ++ ) { freq [ ( s [ i ] ) - 97 ] += 1 ; } for ( int i = 0 ; i < freq . Length ; i ++ ) { if ( freq [ i ] % 2 == 1 ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String s = " abaccaba " ; if ( check ( s ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Maximum Consecutive Zeroes in Concatenated Binary String | C # program to find maximum number of consecutive zeroes after concatenating a binary string ; returns the maximum size of a substring consisting only of zeroes after k concatenation ; stores the maximum length of the required substring ; if the current character is 0 ; stores maximum length of current substrings with zeroes ; if the whole string is filled with zero ; computes the length of the maximal prefix which contains only zeroes ; computes the length of the maximal suffix which contains only zeroes ; if more than 1 concatenations are to be made ; Driver code
using System ; class GFG { static int max_length_substring ( string st , int n , int k ) { int max_len = 0 ; int len = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( st [ i ] == '0' ) len ++ ; else len = 0 ; max_len = Math . Max ( max_len , len ) ; } if ( max_len == n ) return n * k ; int pref = 0 , suff = 0 ; for ( int i = 0 ; st [ i ] == '0' ; ++ i , ++ pref ) ; for ( int i = n - 1 ; st [ i ] == '0' ; -- i , ++ suff ) ; if ( k > 1 ) max_len = Math . Max ( max_len , pref + suff ) ; return max_len ; } public static void Main ( string [ ] args ) { int n = 6 ; int k = 3 ; string st = "110010" ; int ans = max_length_substring ( st , n , k ) ; Console . WriteLine ( ans ) ; } }