codeparrot/xlcost-text-to-code huggingface dataset:
Longest substring with count of 1 s more than 0 s | C # program to find largest substring having count of 1 s more than count count of 0 s . ; Function to find longest substring having count of 1 s more than count of 0 s . ; To store sum . ; To store first occurrence of each sum value . ; To store maximum length . ; To store current substring length . ; Add 1 if current character is 1 else subtract 1. ; If sum is positive , then maximum length substring is bin [ 0. . i ] ; If sum is negative , then maximum length substring is bin [ j + 1. . i ] , where sum of substring bin [ 0. . j ] is sum - 1. ; Make entry for this sum value in hash table if this value is not present . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLongestSub ( String bin ) { int n = bin . Length , i ; int sum = 0 ; Dictionary < int , int > prevSum = new Dictionary < int , int > ( ) ; int maxlen = 0 ; int currlen ; for ( i = 0 ; i < n ; i ++ ) { if ( bin [ i ] == '1' ) sum ++ ; else sum -- ; if ( sum > 0 ) { maxlen = i + 1 ; } else if ( sum <= 0 ) { if ( prevSum . ContainsKey ( sum - 1 ) ) { currlen = i - ( prevSum [ sum - 1 ] == 0 ? 1 : prevSum [ sum - 1 ] ) ; maxlen = Math . Max ( maxlen , currlen ) ; } } if ( ! prevSum . ContainsKey ( sum ) ) prevSum . Add ( sum , i ) ; } return maxlen ; } public static void Main ( String [ ] args ) { String bin = "1010" ; Console . WriteLine ( findLongestSub ( bin ) ) ; } }
Average of ASCII values of characters of a given string | C # code to find average of ASCII characters ; Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code
using System ; class GFG { public static int averageValue ( String s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { sum_char += ( int ) s [ i ] ; } return sum_char / s . Length ; } public static void Main ( ) { String s = " GeeksforGeeks " ; Console . Write ( averageValue ( s ) ) ; } }
Check if two same sub | C # Pprogram to Check if similar subsequences exist or not ; Function to check if similar subsequences occur in a string or not ; iterate and count the frequency ; counting frequency of the letters ; check if frequency is more than once of any character ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool check ( String s , int l ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] >= 2 ) return true ; } return false ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; int l = s . Length ; if ( check ( s , l ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Find the winner of a game where scores are given as a binary string | C # program for predicting winner ; function for winner prediction ; increase count ; check losing condition ; check winning condition ; check tie on n - 1 point ; increase count ; check for 2 point lead ; condition of lost ; condition of win ; Driver Code
using System ; class GFG { public static void predictWinner ( string score , int n ) { int [ ] count = new int [ 2 ] ; int i ; for ( i = 0 ; i < score . Length ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( count [ 0 ] == n && count [ 1 ] < n - 1 ) { Console . Write ( " GEEKS ▁ lost " ) ; return ; } if ( count [ 1 ] == n && count [ 0 ] < n - 1 ) { Console . Write ( " GEEKS ▁ won " ) ; return ; } if ( count [ 0 ] == n - 1 && count [ 1 ] == n - 1 ) { count [ 0 ] = 0 ; count [ 1 ] = 0 ; break ; } } for ( i ++ ; i < score . Length ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( Math . Abs ( count [ 0 ] - count [ 1 ] ) == 2 ) { if ( count [ 0 ] > count [ 1 ] ) { Console . Write ( " GEEKS ▁ lost " ) ; } else { Console . Write ( " GEEKS ▁ won " ) ; } return ; } } } public static void Main ( string [ ] args ) { string score = "1001010101111011101111" ; int n = 15 ; predictWinner ( score , n ) ; } }
Number of Counterclockwise shifts to make a string palindrome | C # program to find counter clockwise shifts to make string palindrome . ; Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code .
using System ; class GFG { static bool isPalindrome ( String str , int l , int r ) { while ( l < r ) { if ( str [ l ] != str [ r ] ) return false ; l ++ ; r -- ; } return true ; } static int CyclicShifts ( String str ) { int n = str . Length ; int left = 0 ; int right = n - 1 ; str = str + str ; int cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } public static void Main ( String [ ] args ) { String str = " bccbbaab " ; Console . WriteLine ( CyclicShifts ( str ) ) ; } }
Find a string such that every character is lexicographically greater than its immediate next character | C # program to print a String in reverse alphabetical order upto given number ; Function that prints the required String ; Find modulus with 26 ; Print extra characters required ; Print the given reverse String countOfStr times ; Driver Code ; Initialize a String in reverse order
using System ; public class GFG { static String printString ( int n , String str ) { String str2 = " " ; int extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( int i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) { str2 += str [ i ] ; } } int countOfStr = n / 26 ; for ( int i = 1 ; i <= countOfStr ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { str2 += str [ j ] ; } } return str2 ; } public static void Main ( ) { int n = 30 ; String str = " zyxwvutsrqponmlkjihgfedcba " ; Console . Write ( printString ( n , str ) ) ; } }
Longest Common Prefix Matching | Set | C # Program to find the longest common prefix ; A Utility Function to find the common prefix between first and last strings ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; sorts the N set of strings ; prints the common prefix of the first and the last String of the set of strings ; Driver Code
using System ; class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { string result = " " ; int n1 = str1 . Length , n2 = str2 . Length ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) break ; result += ( str1 [ i ] ) ; } return ( result ) ; } static void commonPrefix ( String [ ] arr , int n ) { Array . Sort ( arr ) ; Console . Write ( commonPrefixUtil ( arr [ 0 ] , arr [ n - 1 ] ) ) ; } public static void Main ( ) { String [ ] arr = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . Length ; commonPrefix ( arr , n ) ; } }
Square of large number represented as String | C # program to multiply two numbers represented as strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; generate the result string ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static String multiply ( String num1 , String num2 ) { int n1 = num1 . Length ; int n2 = num2 . Length ; if ( n1 == 0 n2 == 0 ) return "0" ; int [ ] result = new int [ n1 + n2 ] ; int i_n1 = 0 ; int i_n2 = 0 ; int i = 0 ; for ( i = n1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n_1 = num1 [ i ] - '0' ; i_n2 = 0 ; for ( int j = n2 - 1 ; j >= 0 ; j -- ) { int n_2 = num2 [ j ] - '0' ; int sum = n_1 * n_2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } i = result . Length - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == - 1 ) return "0" ; String s = " " ; while ( i >= 0 ) s += ( result [ i -- ] ) . ToString ( ) ; return s ; } public static void Main ( String [ ] args ) { String str1 = "454545454545454545" ; Console . WriteLine ( multiply ( str1 , str1 ) ) ; } }
Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | C # program to convert number to the format [ count ] [ digit ] at every step ; Function to perform every step ; perform N steps ; Traverse in the string ; for last digit ; recur for current string ; Driver Code
using System ; class GFG { public static void countDigits ( string st , int n ) { if ( n > 0 ) { int cnt = 1 , i ; string st2 = " " ; for ( i = 1 ; i < st . Length ; i ++ ) { if ( st [ ( i ) ] == st [ ( i - 1 ) ] ) cnt ++ ; else { st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; cnt = 1 ; } } st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; countDigits ( st2 , -- n ) ; } else Console . Write ( st ) ; } public static void Main ( ) { string num = "123" ; int n = 3 ; countDigits ( num , n ) ; } }
Check if the given string of words can be formed from words present in the dictionary | C # program to check if a sentence can be formed from a given set of words . ; Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; adding all words in map ; search in map for all words in the sentence ; all words of sentence are present ; Driver Code ; Calling function to check if words are present in the dictionary or not
using System ; using System . Collections . Generic ; class GFG { static Boolean match_words ( String [ ] dictionary , String [ ] sentence , int n , int m ) { Dictionary < String , int > mp = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( dictionary [ i ] ) ) { mp [ dictionary [ i ] ] = mp [ dictionary [ i ] ] + 1 ; } else { mp . Add ( dictionary [ i ] , 1 ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( mp . ContainsKey ( sentence [ i ] ) && mp [ sentence [ i ] ] > 0 ) mp [ sentence [ i ] ] = mp [ sentence [ i ] ] - 1 ; else return false ; } return true ; } public static void Main ( String [ ] args ) { String [ ] dictionary = { " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " } ; int n = dictionary . Length ; String [ ] sentence = { " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " , " geeks " } ; int m = sentence . Length ; if ( match_words ( dictionary , sentence , n , m ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Print all 3 digit repeating numbers in a very large number | C # program to print 3 digit repeating numbers ; function to print 3 digit repeating numbers ; Hashmap to store the frequency of a 3 digit number ; first three digit number ; if key already exists increase value by 1 ; Output the three digit numbers with frequency > 1 ; Driver Code ; Input string ; Calling Function
using System ; using System . Collections . Generic ; class GFG { static void printNum ( String s ) { int i = 0 , val = 0 ; Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; val = ( s [ 0 ] - '0' ) * 100 + ( s [ 1 ] - '0' ) * 10 + ( s [ 2 ] - '0' ) ; hm . Add ( val , 1 ) ; for ( i = 3 ; i < s . Length ; i ++ ) { val = ( val % 100 ) * 10 + s [ i ] - '0' ; if ( hm . ContainsKey ( val ) ) { hm [ val ] = hm [ val ] + 1 ; } else { hm . Add ( val , 1 ) ; } } foreach ( KeyValuePair < int , int > en in hm ) { int key = en . Key ; int value = en . Value ; if ( value > 1 ) Console . WriteLine ( key + " ▁ - ▁ " + value + " ▁ times " ) ; } } public static void Main ( String [ ] args ) { String input = "123412345123456" ; printNum ( input ) ; } }
Count occurrences of a substring recursively | Recursive C # program for counting number of substrings ; Recursive function to count the number of occurrences of " hi " in str . ; Base Case ; Recursive Case Checking if the first substring matches ; Otherwise , return the count from the remaining index ; Driver Code
using System ; class GFG { static int countSubstrig ( String str1 , String str2 ) { int n1 = str1 . Length ; int n2 = str2 . Length ; if ( n1 == 0 n1 < n2 ) return 0 ; if ( str1 . Substring ( 0 , n2 ) . Equals ( str2 ) ) return countSubstrig ( str1 . Substring ( n2 - 1 ) , str2 ) + 1 ; return countSubstrig ( str1 . Substring ( n2 - 1 ) , str2 ) ; } public static void Main ( ) { string str1 = " geeksforgeeks " , str2 = " geeks " ; Console . Write ( countSubstrig ( str1 , str2 ) ) ; Console . Write ( " STRNEWLINE " ) ; str1 = " hikakashi " ; str2 = " hi " ; Console . Write ( countSubstrig ( str1 , str2 ) ) ; } }
Check if a binary string contains all permutations of length k | C # Program to Check If a String Contains All Binary Codes of Size K ; Unordered map of type string ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool hasAllcodes ( string s , int k ) { HashSet < string > us = new HashSet < string > ( ) ; for ( int i = 0 ; i + k <= s . Length ; i ++ ) { us . Add ( s . Substring ( i , k ) ) ; } return us . Count == 1 << k ; } static void Main ( ) { string s = "00110110" ; int k = 2 ; if ( hasAllcodes ( s , k ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Add n binary strings | C # program to add n binary strings ; This function adds two binary strings and return result as a third string ; Initialize result ; Initialize digit sum ; Traverse both strings starting from last characters ; Compute sum of last digits and carry ; If current digit sum is 1 or 3 , add 1 to result ; Compute carry ; Move to next digits ; function to add n binary strings ; Driver code
using System ; class GFG { static String addBinaryUtil ( String a , String b ) { String result = " " ; int s = 0 ; int i = a . Length - 1 , j = b . Length - 1 ; while ( i >= 0 j >= 0 s == 1 ) { s += ( ( i >= 0 ) ? a [ i ] - '0' : 0 ) ; s += ( ( j >= 0 ) ? b [ j ] - '0' : 0 ) ; result = s % 2 + result ; s /= 2 ; i -- ; j -- ; } return result ; } static String addBinary ( String [ ] arr , int n ) { String result = " " ; for ( int i = 0 ; i < n ; i ++ ) { result = addBinaryUtil ( result , arr [ i ] ) ; } return result ; } public static void Main ( String [ ] args ) { String [ ] arr = { "1" , "10" , "11" } ; int n = arr . Length ; Console . WriteLine ( addBinary ( arr , n ) ) ; } }
Reverse String according to the number of words | C # program to reverse string according to the number of words ; Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
using System ; class GFG { static void reverse ( char [ ] str , int start , int end ) { char temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } static void reverseletter ( char [ ] str , int start , int end ) { int wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ▁ ' ) { continue ; } while ( wend <= end && str [ wend ] != ' ▁ ' ) { wend ++ ; } wend -- ; reverse ( str , wstart , wend ) ; } } public static void Main ( String [ ] args ) { char [ ] str = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " . ToCharArray ( ) ; reverseletter ( str , 0 , str . Length - 1 ) ; Console . Write ( " { 0 } " , String . Join ( " " , str ) ) ; } }
Minimum bit changes in Binary Circular array to reach a index | C # program to find direction with minimum flips ; finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driver code
using System ; class GFG { static void minimumFlip ( String s , int x , int y ) { s = s + s ; bool isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } int valClockwise = 0 ; char cur = s [ x ] ; for ( int i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } int valAnticlockwise = 0 ; cur = s [ y ] ; x += s . Length ; for ( int i = y ; i < x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) { Console . WriteLine ( " Clockwise ▁ " + valClockwise ) ; } else { Console . WriteLine ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } } else if ( ! isOpposite ) { Console . WriteLine ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } else { Console . WriteLine ( " Clockwise ▁ " + valClockwise ) ; } } static void swap ( int a , int b ) { int c = a ; a = b ; b = c ; } public static void Main ( String [ ] args ) { int x = 0 , y = 8 ; String s = "000110" ; minimumFlip ( s , x , y ) ; } }
Prefixes with more a than b | C # code to count the prefixes with more a than b ; Function to count prefixes ; calculating for string S ; count == 0 or when N == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; Driver Code
using System ; class GFG { static int prefix ( String k , int n ) { int a = 0 , b = 0 , count = 0 ; int i = 0 ; int len = k . Length ; for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' a ' ) a ++ ; if ( k [ i ] == ' b ' ) b ++ ; if ( a > b ) { count ++ ; } } if ( count == 0 n == 1 ) { Console . WriteLine ( count ) ; return 0 ; } if ( count == len a - b == 0 ) { Console . WriteLine ( count * n ) ; return 0 ; } int n2 = n - 1 , count2 = 0 ; while ( n2 != 0 ) { for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' a ' ) a ++ ; if ( k [ i ] == ' b ' ) b ++ ; if ( a > b ) { count2 ++ ; } } count += count2 ; n2 -- ; if ( count2 == 0 ) break ; if ( count2 == len ) { count += ( n2 * count2 ) ; break ; } count2 = 0 ; } return count ; } public static void Main ( ) { string S = " aba " ; int N = 2 ; Console . WriteLine ( prefix ( S , N ) ) ; S = " baa " ; N = 3 ; Console . WriteLine ( prefix ( S , N ) ) ; } }
Pairs whose concatenation contain all digits | C # Program to find number of pairs whose concatenation contains all digits from 0 to 9. ; Function to return number of pairs whose concatenation contain all digits from 0 to 9 ; making the mask for each of the number . ; for each of the possible pair which can make OR value equal to 1023 ; finding the count of pair from the given numbers . ; Driver Code
using System ; class GFG { static readonly int N = 20 ; static int countPair ( String [ ] str , int n ) { int [ ] cnt = new int [ 1 << 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { int mask = 0 ; for ( int j = 0 ; j < str [ i ] . Length ; ++ j ) mask |= ( 1 << ( str [ i ] [ j ] - '0' ) ) ; cnt [ mask ] ++ ; } int ans = 0 ; for ( int m1 = 0 ; m1 <= 1023 ; m1 ++ ) for ( int m2 = 0 ; m2 <= 1023 ; m2 ++ ) if ( ( m1 m2 ) == 1023 ) { ans += ( ( m1 == m2 ) ? ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) : ( cnt [ m1 ] * cnt [ m2 ] ) ) ; } return ans / 2 ; } public static void Main ( String [ ] args ) { int n = 5 ; String [ ] str = { "129300455" , "5559948277" , "012334556" , "56789" , "123456879" } ; Console . Write ( countPair ( str , n ) + " STRNEWLINE " ) ; } }
Count binary strings with twice zeros in first half | C # program for finding number of binary strings number of '0' in first half is double the number of '0' in second half of string ; pre define some constant ; global values for pre computation ; function to print number of required string ; calculate answer using proposed algorithm ; main function
using System ; class GFG { static long mod = 1000000007 ; static long max = 1001 ; static long [ , ] nCr = new long [ 1003 , 1003 ] ; static void preComputeCoeff ( ) { for ( int i = 0 ; i < max ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i , j ] = 1 ; else nCr [ i , j ] = ( nCr [ i - 1 , j - 1 ] + nCr [ i - 1 , j ] ) % mod ; } } } static long computeStringCount ( int N ) { int n = N / 2 ; long ans = 0 ; for ( int i = 2 ; i <= n ; i += 2 ) ans = ( ans + ( ( nCr [ n , i ] * nCr [ n , i / 2 ] ) % mod ) ) % mod ; return ans ; } public static void Main ( ) { preComputeCoeff ( ) ; int N = 3 ; Console . Write ( computeStringCount ( N ) ) ; } }
Count of ' GFG ' Subsequences in the given string | C # Program to find the " GFG " subsequence in the given string ; Print the count of " GFG " subsequence in the string ; Traversing the given string ; If the character is ' G ' , increment the count of ' G ' , increase the result and update the array . ; If the character is ' F ' , increment the count of ' F ' and update the array . ; Ignore other character . ; Driver code
using System ; class GFG { static void countSubsequence ( string s , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s [ i ] ) { case ' G ' : cntG ++ ; result += C ; break ; case ' F ' : cntF ++ ; C += cntG ; break ; default : continue ; } } Console . WriteLine ( result ) ; } public static void Main ( ) { string s = " GFGFG " ; int n = s . Length ; countSubsequence ( s , n ) ; } }
Lexicographical Maximum substring of string | C # program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; Driver code
using System ; public class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . Length ; ++ i ) { if ( mx . CompareTo ( str . Substring ( i ) ) <= 0 ) { mx = str . Substring ( i ) ; } } return mx ; } public static void Main ( ) { String str = " ababaa " ; Console . WriteLine ( LexicographicalMaxString ( str ) ) ; } }
Lexicographical Maximum substring of string | C # program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code
using System ; using System . Collections . Generic ; class GFG { static string LexicographicalMaxString ( string str ) { char maxchar = ' a ' ; List < int > index = new List < int > ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] >= maxchar ) { maxchar = str [ i ] ; index . Add ( i ) ; } } string maxstring = " " ; for ( int i = 0 ; i < index . Count ; i ++ ) { if ( str . Substring ( index [ i ] ) . CompareTo ( maxstring ) > 0 ) { maxstring = str . Substring ( index [ i ] ) ; } } return maxstring ; } static public void Main ( ) { string str = " acbacbc " ; Console . Write ( LexicographicalMaxString ( str ) ) ; } }