codeparrot/xlcost-text-to-code huggingface dataset:
Convert a Mobile Numeric Keypad sequence to equivalent sentence | C # program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
using System ; public class GFG { static void printSentence ( string S ) { string [ ] nums = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char [ ] str = S . ToCharArray ( ) ; int i = 0 ; while ( i < str . Length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . Length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . Length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { Console . Write ( nums [ str [ i ] - 48 ] [ count % 4 ] ) ; } else { Console . Write ( nums [ str [ i ] - 48 ] [ count % 3 ] ) ; } i ++ ; } } public static void Main ( string [ ] args ) { string str = "234" ; printSentence ( str ) ; } }
Lexicographically largest string with sum of characters equal to N | C # program for the above approach ; Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
using System ; public class GFG { static string getString ( int N ) { string ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += ( char ) ( N + ' a ' - 1 ) ; return ans ; } public static void Main ( string [ ] args ) { int N = 30 ; Console . WriteLine ( getString ( N ) ) ; } }
Length of all prefixes that are also the suffixes of given string | C # program for the above approach ; Function to find the length of all prefixes of the given string that are also suffixes of the same string ; Stores the prefix string ; Traverse the string S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countSamePrefixSuffix ( string s , int n ) { string prefix = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix += s [ i ] ; string suffix = s . Substring ( n - 1 - i , i + 1 ) ; if ( prefix == suffix ) { Console . Write ( prefix . Length + " ▁ " ) ; } } } public static void Main ( ) { string S = " ababababab " ; int N = S . Length ; countSamePrefixSuffix ( S , N ) ; } }
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | C # program for the above approach ; Function to count newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void countStringPairs ( string [ ] a , int n ) { int ans = 0 ; Dictionary < string , int > s = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . ContainsKey ( a [ i ] ) ) s . Add ( a [ i ] , 0 ) ; s [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { char [ ] p = ( a [ i ] ) . ToCharArray ( ) ; char [ ] q = ( a [ j ] ) . ToCharArray ( ) ; if ( p [ 0 ] != q [ 0 ] ) { char t = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = t ; if ( ! s . ContainsKey ( new string ( p ) ) && ! s . ContainsKey ( new string ( q ) ) ) { ans ++ ; } } } } Console . WriteLine ( ans ) ; } static public void Main ( ) { string [ ] arr = { " good " , " bad " , " food " } ; int N = arr . Length ; countStringPairs ( arr , N ) ; } }
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | C # program for the above approach ; Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code
using System ; class GFG { static void minimumMoves ( string s , int n , int k ) { if ( n % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } int countOpen = 0 , countClose = 0 ; foreach ( char ch in s . ToCharArray ( ) ) if ( ch == ' ( ' ) countOpen ++ ; else if ( ch == ' ) ' ) countClose ++ ; if ( countOpen != countClose ) { Console . WriteLine ( " No " ) ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { string S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . Length , K ) ; } }
Partition a string into palindromic strings of at least length 2 with every character present in a single string | C # program for the above approach ; Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver code
using System ; class GFG { static void checkPalindrome ( string s ) { int [ ] a = new int [ 26 ] ; int o = 0 , e = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) a [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 && a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) Console . WriteLine ( " Yes " ) ; else { o = o - e ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 && a [ i ] % 2 == 1 && a [ i ] > 2 ) { int k = o ; o = o - a [ i ] / 2 ; if ( o > 0 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } } static void Main ( ) { string S = " abbbaddzcz " ; checkPalindrome ( S ) ; } }
Maximize time by replacing ' _ ' in a given 24 Hour format time | C # program for the above approach ; Function to find the maximum time possible by replacing each ' _ ' with any digit ; If the first character is ' _ ' ; If s [ 1 ] is ' _ ' or s [ 1 ] is less than 4 ; Update s [ 0 ] as 2 ; Otherwise , update s [ 0 ] = 1 ; If s [ 1 ] is equal to ' _ ' ; If s [ 0 ] is equal to '2' ; Otherwise ; If S [ 3 ] is equal to ' _ ' ; If s [ 4 ] is equal to ' _ ' ; Print the modified string ; Driver Code
using System ; class GFG { static void maximumTime ( string str ) { char [ ] s = str . ToCharArray ( ) ; if ( s [ 0 ] == ' _ ' ) { if ( ( s [ 1 ] == ' _ ' ) || ( s [ 1 ] >= '0' && s [ 1 ] < '4' ) ) { s [ 0 ] = '2' ; } else { s [ 0 ] = '1' ; } } if ( s [ 1 ] == ' _ ' ) { if ( s [ 0 ] == '2' ) { s [ 1 ] = '3' ; } else { s [ 1 ] = '9' ; } } if ( s [ 3 ] == ' _ ' ) { s [ 3 ] = '5' ; } if ( s [ 4 ] == ' _ ' ) { s [ 4 ] = '9' ; } for ( int i = 0 ; i < s . Length ; i ++ ) Console . Write ( s [ i ] ) ; } static public void Main ( ) { string S = "0 _ : 4 _ " ; maximumTime ( S ) ; } }
Nearest power of 2 of frequencies of each digit of a given number | C # program for the above approach ; Function to find the nearest power of 2 for all frequencies in the Map freq ; Traverse the Map ; Calculate log of the current array element ; Find the nearest power of 2 for the current frequency ; Function to find nearest power of 2 for frequency of each digit of num ; Length of string ; Stores the frequency of each character in the string ; Traverse the string S ; Function call to generate nearest power of 2 for each frequency ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void nearestPowerOfTwoUtil ( Dictionary < char , int > freq ) { foreach ( KeyValuePair < char , int > entry in freq ) { char key = entry . Key ; Console . Write ( key + " ▁ - > ▁ " ) ; int lg = ( int ) ( Math . Log ( freq [ key ] / Math . Log ( 2 ) ) ) ; int a = ( int ) Math . Pow ( 2 , lg ) ; int b = ( int ) Math . Pow ( 2 , lg + 1 ) ; if ( ( freq [ key ] - a ) < ( b - freq [ key ] ) ) { Console . Write ( a + " STRNEWLINE " ) ; } else { Console . Write ( b + " STRNEWLINE " ) ; } } } static void nearestPowerOfTwo ( string S ) { int N = S . Length ; Dictionary < char , int > freq = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( S [ i ] ) ) freq [ S [ i ] ] += 1 ; else freq [ S [ i ] ] = 1 ; } nearestPowerOfTwoUtil ( freq ) ; } public static void Main ( ) { string N = "16333331163" ; nearestPowerOfTwo ( N ) ; } }
Smallest string divisible by two given strings | C # program for above approach ; Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code
using System ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } static void findSmallestString ( string s , string t ) { int n = s . Length , m = t . Length ; int l = lcm ( n , m ) ; string s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 == t1 ) Console . WriteLine ( s1 ) ; else Console . WriteLine ( - 1 ) ; } public static void Main ( String [ ] args ) { string S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; } }
Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | C # program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
using System ; class GFG { static int minRemovals ( string str , int N ) { int ans = 0 ; bool X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { ans ++ ; while ( str [ i ] == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == '0' && str [ N - 1 ] == '0' ) { ans -- ; } return ans ; } public static void Main ( ) { string S = "11010001" ; int N = S . Length ; Console . Write ( minRemovals ( S , N ) ) ; } }
Minimum removals required to place all 0 s before 1 s in a Binary String | C # program for above approach ; Function to count minimum removals required to arrange all 0 s before 1 s ; Count the occurences of 0 in s ; Size of the string ; Stores the minimum number of removals required ; Iterate over each of the characters in the string s ; If the i - th character is found to be '0' ; Store the minimum of res and right_0 + left_1 in res ; Return the final result ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int minimumDeletions ( string s ) { int right_0 = ( int ) s . Split ( '0' ) . Length - 1 ; int left_1 = 0 ; int n = s . Length ; int res = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = Math . Min ( res , right_0 + left_1 ) ; } return res ; } public static void Main ( String [ ] args ) { string s = "001101" ; int count = minimumDeletions ( s ) ; Console . WriteLine ( count ) ; } }
Minimum substring flips required to convert a Binary String to another | C # program for the above approach ; Function to count the minimum number of reversals required to make the given binary strings s1 and s2 same ; Stores the minimum count of reversal of substrings required ; If the length of the strings are not the same then return - 1 ; Iterate over each character ; If s1 [ i ] is not equal to s2 [ i ] ; Iterate until s1 [ i ] != s2 [ i ] ; Increment answer by 1 ; Return the resultant count of reversal of substring required ; Driver Code ; Function Call
using System ; class GFG { static int canMakeSame ( string s1 , string s2 ) { int ans = 0 ; if ( s1 . Length != s2 . Length ) { return - 1 ; } int N = s1 . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { while ( i < s1 . Length && s1 [ i ] != s2 [ i ] ) { i ++ ; } ans ++ ; } } return ans ; } public static void Main ( string [ ] args ) { string S1 = "100001" ; string S2 = "110111" ; Console . Write ( canMakeSame ( S1 , S2 ) ) ; } }
Count intervals that intersects with a given meeting time | C # implementation of the above approach ; Function to convert a time in 24 hour format to an equivalent integer ; Removes " : " at 3 rd position ; Calculate hours ; Stores the time in 24 hours format ; If time is in " AM " ; If hh is equal to 12 ; If time is in " PM " ; If hh is equal to 12 ; Return time ; Function to count number of intervals in which p lies ; Stores the count ; Stores the integer value of 24 hours time format of P ; Traverse the array ; Stores the integer value of 24 hours time format of arr [ i , 0 ] ; Stores the integer value of 24 hours time format of arr [ i , 1 ] ; If M lies within the [ L , R ] ; Increment ans by 1 ; Return ans ; Driver Code
using System ; class GFG { static int convert ( String str ) { str = str . Substring ( 0 , 2 ) + str . Substring ( 3 ) ; int h1 = ( int ) str [ 1 ] - '0' ; int h2 = ( int ) str [ 0 ] - '0' ; int hh = ( h2 * 10 + h1 % 10 ) ; int time = 0 ; if ( str [ 5 ] == ' A ' ) { if ( hh == 12 ) time += Int32 . Parse ( str . Substring ( 2 , 2 ) ) ; else { time += Int32 . Parse ( str . Substring ( 0 , 2 ) ) ; } } else { if ( hh == 12 ) { time += Int32 . Parse ( str . Substring ( 0 , 4 ) ) ; } else { time += Int32 . Parse ( str . Substring ( 0 , 4 ) ) ; time += 1200 ; } } return time ; } static int countOverlap ( String [ , ] arr , int n , String p ) { int ans = 0 ; int M = convert ( p ) ; for ( int i = 0 ; i < n ; i ++ ) { int L = convert ( arr [ i , 0 ] ) ; int R = convert ( arr [ i , 1 ] ) ; if ( ( L <= M && M <= R ) || ( M >= R && M <= L ) ) ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { String [ , ] arr = new String [ , ] { { "12:00 : AM " , "11:55 : PM " } , { "12:01 : AM " , "11:50 : AM " } , { "12:30 : AM " , "12:00 : PM " } , { "11:57 : AM " , "11:59 : PM " } } ; String P = "12:01 : PM " ; int N = arr . GetLength ( 0 ) ; Console . WriteLine ( countOverlap ( arr , N , P ) ) ; } }
Count points which are revisited while following the path specified by a given string | C # program for the above approach ; Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int count ( String S , int X , int Y ) { int N = S . Length ; int temp_x = 0 , temp_y = 0 ; int count = 0 ; HashSet < String > s = new HashSet < String > ( ) ; s . Add ( ( X + " # " + Y ) ) ; for ( int i = 0 ; i < N ; i ++ ) { temp_x = X ; temp_y = Y ; if ( S [ i ] == ' U ' ) { X ++ ; } else if ( S [ i ] == ' D ' ) { X -- ; } else if ( S [ i ] == ' R ' ) { Y ++ ; } else { Y -- ; } if ( s . Contains ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ) { count ++ ; } else { s . Add ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ; } } return count ; } public static void Main ( String [ ] args ) { String S = " RDDUDL " ; int X = 0 , Y = 0 ; Console . Write ( count ( S , X , Y ) ) ; } }
Sum of frequencies of characters of a string present in another string | C # program for the above approach ; Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Driver code ; Given strings
using System ; using System . Collections . Generic ; class GFG { static void countTotalFrequencies ( string S1 , string S2 ) { HashSet < char > bset = new HashSet < char > ( ) ; foreach ( char x in S1 ) bset . Add ( x ) ; int count = 0 ; foreach ( char x in S2 ) { if ( bset . Contains ( x ) ) count += 1 ; } Console . Write ( count ) ; } static void Main ( ) { string S1 = " geEksFOR " ; string S2 = " GeEksforgeEKS " ; countTotalFrequencies ( S1 , S2 ) ; } }
Check if characters of a string can be made non | C # program for the above approach ; Function to check if it 's possible  to make the String s non decreasing ; Stores length of String ; Stores the last character that is not ' ? ' ; Traverse the String S ; If current character of the String is ' ? ' ; If s [ i ] is not ' ? ' and is less than C ; Otherwise ; Update C ; Return 1 ; Driver Code
using System ; public class GFG { static int nonDecreasing ( char [ ] s ) { int size = s . Length ; char c = ' a ' ; for ( int i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ? ' ) { continue ; } else if ( ( s [ i ] != ' ? ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } public static void Main ( String [ ] args ) { String S = " abb ? xy ? " ; if ( nonDecreasing ( S . ToCharArray ( ) ) == 1 ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } }
Make a given Binary String non | C # program for the above approach ; Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver code
using System ; class GFG { public static void min_length ( string str ) { int n = str . Length ; int total_zeros = 0 ; int total_ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { total_zeros ++ ; } else { total_ones ++ ; } } int ans = Math . Min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char x = str [ i ] ; if ( x == '0' ) { cur_zeros ++ ; } else { cur_ones ++ ; } ans = Math . Min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { string str = "10011" ; min_length ( str ) ; } }
Check if substrings from three given strings can be concatenated to form a palindrome | C # program for the above approach ; Function to check if subStrings from three given Strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code
using System ; public class GFG { static String make_palindrome ( String S1 , String S2 , String S3 ) { int maskA = 0 , maskC = 0 ; foreach ( char i in S1 . ToCharArray ( ) ) maskA |= ( 1 << ( i - ' a ' ) ) ; foreach ( char i in S3 . ToCharArray ( ) ) maskC |= ( 1 << ( i - ' a ' ) ) ; if ( ( maskA & maskC ) > 0 ) return " YES " ; return " NO " ; } public static void Main ( String [ ] args ) { String S1 = " adcb " , S2 = " bcdb " , S3 = " abe " ; Console . Write ( make_palindrome ( S1 , S2 , S3 ) ) ; } }
Length of longest prefix anagram which are common in given two strings | C # program for the above approach ; Function to check if two arrays are identical or not ; Iterate over the range [ 0 , SIZE ] ; If frequency any character is not same in both the strings ; Otherwise ; Function to find the maximum length of the required string ; Store the count of characters in string str1 ; Store the count of characters in string str2 ; Stores the maximum length ; Minimum length of str1 and str2 ; Increment the count of characters of str1 [ i ] in freq1 [ ] by one ; Increment the count of characters of str2 [ i ] in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally print the ans ; Driver code ; Function Call
using System ; class GFG { static int SIZE = 26 ; static bool longHelper ( int [ ] freq1 , int [ ] freq2 ) { for ( int i = 0 ; i < SIZE ; ++ i ) { if ( freq1 [ i ] != freq2 [ i ] ) { return false ; } } return true ; } static void longCommomPrefixAnagram ( string s1 , string s2 , int n1 , int n2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; int ans = 0 ; int mini_len = Math . Min ( n1 , n2 ) ; for ( int i = 0 ; i < mini_len ; ++ i ) { freq1 [ s1 [ i ] - ' a ' ] ++ ; freq2 [ s2 [ i ] - ' a ' ] ++ ; if ( longHelper ( freq1 , freq2 ) ) { ans = i + 1 ; } } Console . Write ( ans ) ; } static void Main ( ) { string str1 = " abaabcdezzwer " ; string str2 = " caaabbttyh " ; int N = str1 . Length ; int M = str2 . Length ; longCommomPrefixAnagram ( str1 , str2 , N , M ) ; } }
Lexicographic rank of a Binary String | C # program to implement the above approach ; Function to find the rank of a String ; Store the length of the String ; Stores its equivalent decimal value ; Traverse the String ; Store the number of string s of length less than N occurring before the given String ; Store the decimal equivalent number of String bin ; Store the rank in answer ; Print the answer ; Driver Code
using System ; class GFG { static void findRank ( string s ) { int N = s . Length ; string bin = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) bin += "0" ; else bin += "1" ; } int X = 1 << N ; int res = 0 , val = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) res += ( val ) ; val *= 2 ; } int Y = res ; int ans = X + Y - 1 ; Console . Write ( ans ) ; } public static void Main ( ) { string S = "001" ; findRank ( S ) ; } }