codeparrot/xlcost-text-to-code huggingface dataset:
Check whether the vowels in a string are in alphabetical order or not | C # implementation of above approach ; Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not
using System ; class GFG { public static bool areVowelsInOrder ( string s ) { int n = s . Length ; char c = ( char ) 64 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { if ( s [ i ] < c ) { return false ; } else { c = s [ i ] ; } } } return true ; } public static void Main ( string [ ] args ) { string s = " aabbbddeecc " ; if ( areVowelsInOrder ( s ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Rearrange the string to maximize the number of palindromic substrings | C # program to rearrange the string such to maximize the number of palindromic substrings ; Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver code
using System ; class GFG { static String newString ( string s ) { int l = s . Length ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] += 1 ; } string ans = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { ans += ( char ) ( 97 + i ) ; } } return ans ; } public static void Main ( ) { string s = " aabab " ; Console . Write ( newString ( s ) ) ; } }
Program to find remainder when large number is divided by r | C # implementation to find Remainder when a large Number is divided by R ; Function to Return Remainder ; len is variable to store the length of Number string . ; loop that find Remainder ; Return the remainder ; Driver code ; Get the large number as string ; Get the divisor R ; Find and print the remainder
using System ; class GFG { static int Remainder ( String str , int R ) { int len = str . Length ; int Num , Rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { Num = Rem * 10 + ( str [ i ] - '0' ) ; Rem = Num % R ; } return Rem ; } public static void Main ( ) { String str = "13589234356546756" ; int R = 13 ; Console . WriteLine ( Remainder ( str , R ) ) ; } }
Number of balanced bracket subsequence of length 2 and 4 | C # implementation of above approach ; Taking the frequency suffix sum of the number of 2 's  present after every index ; Storing the count of subsequence ; Subsequence of length 2 ; Subsequence of length 4 of type 1 1 2 2 ; Subsequence of length 4 of type 1 2 1 2 ; Driver Code
using System ; class GFG { public static void countWays ( int [ ] a , int n ) { int i , j ; long [ ] suff = new long [ n ] ; if ( a [ n - 1 ] == 2 ) suff [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] == 2 ) suff [ i ] = suff [ i + 1 ] + 1 ; else suff [ i ] = suff [ i + 1 ] ; } long ss = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ss += suff [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && suff [ j ] >= 2 ) { ss += ( suff [ j ] ) * ( suff [ j ] - 1 ) / 2 ; } } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && ( suff [ i ] - suff [ j ] ) >= 1 && suff [ j ] >= 1 ) { ss += ( suff [ i ] - suff [ j ] ) * suff [ j ] ; } } } Console . WriteLine ( ss ) ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 1 , 1 , 2 , 2 } ; int n = 6 ; countWays ( a , n ) ; } }
Count the number of carry operations required to add two numbers | C # implementation of above approach ; Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
using System ; class GFG { static int count_carry ( string a , string b ) { int carry = 0 ; int count = 0 ; int len_a = a . Length , len_b = b . Length ; while ( len_a != 0 len_b != 0 ) { int x = 0 , y = 0 ; if ( len_a > 0 ) { x = a [ len_a - 1 ] - '0' ; len_a -- ; } if ( len_b > 0 ) { y = b [ len_b - 1 ] - '0' ; len_b -- ; } int sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } public static void Main ( ) { string a = "9555" , b = "555" ; int count = count_carry ( a , b ) ; if ( count == 0 ) Console . Write ( "0 STRNEWLINE " ) ; else if ( count == 1 ) Console . Write ( "1 STRNEWLINE " ) ; else Console . Write ( count ) ; } }
Check if a number is in given base or not | C # program to check if given number is in given base or not . ; Allowed base are till 16 ( Hexadecimal ) ; If bas is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Code
using System ; class GFG { static bool isInGivenBase ( String str , int bas ) { if ( bas > 16 ) return false ; else if ( bas <= 10 ) { for ( int i = 0 ; i < str . Length ; i ++ ) if ( ! ( str [ i ] >= '0' && str [ i ] < ( '0' + bas ) ) ) return false ; } else { for ( int i = 0 ; i < str . Length ; i ++ ) if ( ! ( ( str [ i ] >= '0' && str [ i ] < ( '0' + bas ) ) || ( str [ i ] >= ' A ' && str [ i ] < ( ' A ' + bas - 10 ) ) ) ) return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " AF87" ; if ( isInGivenBase ( str , 16 ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find indices of all occurrence of one string in other | C # program to find indices of all occurrences of one String in other . ; Driver code
using System ; class GFG { static void printIndex ( String str , String s ) { bool flag = false ; for ( int i = 0 ; i < str . Length - s . Length + 1 ; i ++ ) { if ( str . Substring ( i , s . Length ) . Equals ( s ) ) { Console . Write ( i + " ▁ " ) ; flag = true ; } } if ( flag == false ) { Console . WriteLine ( " NONE " ) ; } } public static void Main ( String [ ] args ) { String str1 = " GeeksforGeeks " ; String str2 = " Geeks " ; printIndex ( str1 , str2 ) ; } }
Alternatively Merge two Strings in Java | C # code to alternatively merge two strings ; Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver code
using System ; class GFG { static string merge ( string s1 , string s2 ) { string result = " " ; for ( int i = 0 ; i < s1 . Length < s2 . Length ; i ++ ) { if ( i < s1 . Length ) result += s1 [ i ] ; if ( i < s2 . Length ) result += s2 [ i ] ; } return result ; } static void Main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; Console . WriteLine ( merge ( s1 , s2 ) ) ; } }
Maximum occurring character in an input string | Set | C # implementation to find the maximum occurring character in an input string which is lexicographically first ; function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; to store the maximum occurring character ; length of ' str ' ; get frequency of each character of ' str ' ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update ' result ' ; maximum occurring character ; Driver Code
using System ; class GFG { static char getMaxOccurringChar ( string str ) { int [ ] freq = new int [ 26 ] ; int max = - 1 ; char result = ( char ) 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ▁ ' ) { freq [ str [ i ] - ' a ' ] ++ ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( max < freq [ i ] ) { max = freq [ i ] ; result = ( char ) ( i + ' a ' ) ; } } return result ; } public static void Main ( ) { string str = " sample ▁ program " ; Console . WriteLine ( " Maximum ▁ occurring ▁ character ▁ = ▁ " + getMaxOccurringChar ( str ) ) ; } }
Given two numbers as strings , find if one is a power of other | C # program to check if one number is power of other ; Multiply the numbers . It multiplies each digit of second string to each digit of first and stores the result . ; If the digit exceeds 9 , add the cumulative carry to previous digit . ; if all zeroes , return "0" . ; Remove starting zeroes . ; Removes Extra zeroes from front of a string . ; Make sure there are no leading zeroes in the string . ; Making sure that s1 is smaller . If it is greater , we recur we reversed parameters . ; Driver Code
using System ; public class new_file { static bool isGreaterThanEqual ( String s1 , String s2 ) { if ( s1 . Length > s2 . Length ) return true ; if ( s1 . CompareTo ( s2 ) == 0 ) return true ; return false ; } static String multiply ( String s1 , String s2 ) { int n = s1 . Length ; int m = s2 . Length ; int i = 0 ; int [ ] result = new int [ n + m ] ; for ( i = n - 1 ; i >= 0 ; i -- ) for ( int j = m - 1 ; j >= 0 ; j -- ) result [ i + j + 1 ] += ( s1 [ i ] - '0' ) * ( s2 [ j ] - '0' ) ; int size = result . Length ; for ( i = size - 1 ; i > 0 ; i -- ) { if ( result [ i ] >= 10 ) { result [ i - 1 ] += result [ i ] / 10 ; result [ i ] = result [ i ] % 10 ; } } i = 0 ; while ( i < size && result [ i ] == 0 ) i ++ ; if ( i == size ) return "0" ; String temp = " " ; while ( i < size ) { temp += ( result [ i ] ) . ToString ( ) ; i ++ ; } return temp ; } static String removeLeadingZeores ( String s ) { int n = s . Length ; int i = 0 ; while ( i < n && s [ i ] == '0' ) i ++ ; if ( i == n ) return "0" ; String temp = " " ; while ( i < n ) { temp += s [ i ] ; i ++ ; } return temp ; } static bool isPower ( String s1 , String s2 ) { s1 = removeLeadingZeores ( s1 ) ; s2 = removeLeadingZeores ( s2 ) ; if ( s1 == "0" s2 == "0" ) return false ; if ( s1 == "1" && s2 == "1" ) return true ; if ( s1 == "1" s2 == "1" ) return true ; if ( s1 . Length > s2 . Length ) return isPower ( s2 , s1 ) ; String temp = s1 ; while ( ! isGreaterThanEqual ( s1 , s2 ) ) s1 = multiply ( s1 , temp ) ; if ( s1 . CompareTo ( s2 ) == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { String s1 = "374747" , s2 = "52627712618930723" ; if ( isPower ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; s1 = "4099" ; s2 = "2" ; if ( isPower ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check for balanced parentheses in an expression | O ( 1 ) space | C # code to check balanced parentheses with O ( 1 ) space . ; Function1 to match closing bracket ; Function1 to match opening bracket ; Function to check balanced parentheses ; helper variables ; Handling case of opening parentheses ; } Handling case of closing parentheses ; If corresponding matching opening parentheses doesn 't  lie in given interval return 0 ; else continue ; If corresponding closing parentheses doesn 't lie in given interval  return 0 ; if found , now check for each opening and closing parentheses in this interval ; Driver Code
using System ; public class GFG { static int matchClosing ( char [ ] X , int start , int end , char open , char close ) { int c = 1 ; int i = start + 1 ; while ( i <= end ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i ++ ; } return i ; } static int matchingOpening ( char [ ] X , int start , int end , char open , char close ) { int c = - 1 ; int i = end - 1 ; while ( i >= start ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i -- ; } return - 1 ; } static bool isBalanced ( char [ ] X , int n ) { int i , j = 0 , k , x , start , end ; for ( i = 0 ; i < n ; i ++ ) { if ( X [ i ] == ' ( ' ) { j = matchClosing ( X , i , n - 1 , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' { ' ) { j = matchClosing ( X , i , n - 1 , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' [ ' ) { j = matchClosing ( X , i , n - 1 , ' [ ' , ' ] ' ) ; } else { if ( X [ i ] == ' ) ' ) { j = matchingOpening ( X , 0 , i , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' } ' ) { j = matchingOpening ( X , 0 , i , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' ] ' ) { j = matchingOpening ( X , 0 , i , ' [ ' , ' ] ' ) ; } if ( j < 0 j >= i ) { return false ; } continue ; } if ( j >= n j < 0 ) { return false ; } start = i ; end = j ; for ( k = start + 1 ; k < end ; k ++ ) { if ( X [ k ] == ' ( ' ) { x = matchClosing ( X , k , end , ' ( ' , ' ) ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ) ' ) { x = matchingOpening ( X , start , k , ' ( ' , ' ) ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' { ' ) { x = matchClosing ( X , k , end , ' { ' , ' } ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' } ' ) { x = matchingOpening ( X , start , k , ' { ' , ' } ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' [ ' ) { x = matchClosing ( X , k , end , ' [ ' , ' ] ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ] ' ) { x = matchingOpening ( X , start , k , ' [ ' , ' ] ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } } } return true ; } public static void Main ( ) { char [ ] X = " [ ( ) ] ( ) " . ToCharArray ( ) ; int n = 6 ; if ( isBalanced ( X , n ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; char [ ] Y = " [ [ ( ) ] ] ) " . ToCharArray ( ) ; n = 7 ; if ( isBalanced ( Y , n ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Sorting array with conditional swapping | C # program to Check if it is possible to sort the array in ascending order ; Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code
using System ; class GFG { static string possibleToSort ( int [ ] arr , int n , string str ) { int max_element = - 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { max_element = Math . Max ( max_element , arr [ i ] ) ; if ( str [ i ] == '0' ) { if ( max_element > i + 1 ) return " No " ; } } return " Yes " ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 6 } ; int n = arr . Length ; string str = "01110" ; Console . WriteLine ( possibleToSort ( arr , n , str ) ) ; } }
Find sub | C # program to find substring with given power ; Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; If curr_power is equal to target power we found a substring starting from index 0 and ending at index i . ; If curr_power - power already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findSubstring ( String str , int power ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int curr_power = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { curr_power = curr_power + ( str [ i ] - ' a ' + 1 ) ; if ( curr_power == power ) { Console . WriteLine ( " Substring ▁ from ▁ index ▁ 0" + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } if ( map . ContainsKey ( curr_power - power ) ) { Console . WriteLine ( " Substring ▁ from ▁ index ▁ " + ( map [ curr_power - power ] + 1 ) + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } if ( ! map . ContainsKey ( curr_power ) ) map . Add ( curr_power , i ) ; else map [ curr_power ] = i ; } Console . WriteLine ( " No ▁ substring ▁ with ▁ " + " given ▁ power ▁ exists " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int power = 36 ; findSubstring ( str , power ) ; } }
Prime String | C # program to find if string is a Prime or not ; Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
using System ; class GFG { public static bool isPrimeString ( string str ) { int len = str . Length , n = 0 ; for ( int i = 0 ; i < len ; i ++ ) { n += ( int ) str [ i ] ; } if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } public static void Main ( string [ ] args ) { string str = " geekRam " ; if ( isPrimeString ( str ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Ways to split string such that each partition starts with distinct character | C # Program to find number of way to split string such that each partition starts with distinct character with maximum number of partitions . ; Returns the number of we can split the string ; Finding the frequency of each character . ; making frequency of first character of string equal to 1. ; Finding the product of frequency of occurrence of each character . ; Driver Code
using System ; class GFG { static int countWays ( string s ) { int [ ] count = new int [ 26 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) count [ s [ i ] - ' a ' ] ++ ; count [ s [ 0 ] - ' a ' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } public static void Main ( ) { string s = " acbbcc " ; Console . WriteLine ( countWays ( s ) ) ; } }
Lexicographically next greater string using same character set | C # implementation of above algorithm . ; function to print output ; to store unique characters of the string ; to check uniqueness ; if mp [ s [ i ] ] = 0 then it is first time ; sort the unique characters ; simply add n - k smallest characters ; return ; end the program ; searching the first character left of index k and not equal to greatest character of the string ; finding the just next greater character than s [ i ] ; suffix with smallest character ; if we reach here then all indices to the left of k had the greatest character ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void lexoString ( char [ ] s , int k ) { int n = s . Length ; List < char > v = new List < char > ( ) ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! mp . ContainsKey ( s [ i ] ) ) { mp . Add ( s [ i ] , 1 ) ; v . Add ( s [ i ] ) ; } } v . Sort ( ) ; if ( k > n ) { Console . Write ( String . Join ( " " , s ) ) ; for ( int i = n ; i < k ; i ++ ) { Console . Write ( v [ 0 ] ) ; } } for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != v [ v . Count - 1 ] ) { for ( int j = 0 ; j < i ; j ++ ) { Console . Write ( s [ j ] ) ; } for ( int j = 0 ; j < v . Count ; j ++ ) { if ( v [ j ] > s [ i ] ) { Console . Write ( v [ j ] ) ; break ; } } for ( int j = i + 1 ; j < k ; j ++ ) { Console . Write ( v [ 0 ] ) ; } return ; } } Console . Write ( " No ▁ lexicographically ▁ greater ▁ " + " string ▁ of ▁ length ▁ " + k + " ▁ possible ▁ here . " ) ; } public static void Main ( String [ ] arr ) { String s = " gi " ; int k = 3 ; lexoString ( s . ToCharArray ( ) , k ) ; } }
Number of palindromic permutations | Set 1 | C # program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code
using System ; class GFG { static int MAX = 256 ; static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int countPalinPermutations ( string str ) { int n = str . Length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] ] ++ ; long res = fact ( n / 2 ) ; bool oddFreq = false ; for ( int i = 0 ; i < MAX ; i ++ ) { int half = freq [ i ] / 2 ; if ( freq [ i ] % 2 != 0 ) { if ( oddFreq == true ) return 0 ; oddFreq = true ; } res = res / fact ( half ) ; } return ( int ) res ; } public static void Main ( ) { string str = " gffg " ; Console . WriteLine ( countPalinPermutations ( str ) ) ; } }
Substrings starting with vowel and ending with consonants and vice versa | C # program to count special strings ; Returns true if ch is vowel ; function to check consonant ; in case of empty string , we can 't fullfill the  required condition, hence we return ans as 0. ; co [ i ] is going to store counts of consonants from str [ len - 1 ] to str [ i ] . vo [ i ] is going to store counts of vowels from str [ len - 1 ] to str [ i ] . ; Counting consonants and vowels from end of string . ; Now we traverse string from beginning ; If vowel , then count of substrings starting with str [ i ] is equal to count of consonants after it . ; If consonant , then count of substrings starting with str [ i ] is equal to count of vowels after it . ; Driver program
using System ; class GFG { static Boolean isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static Boolean isCons ( char ch ) { return ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) ; } static int countSpecial ( char [ ] str ) { int len = str . Length ; if ( len == 0 ) return 0 ; int [ ] co = new int [ len + 1 ] ; int [ ] vo = new int [ len + 1 ] ; if ( isCons ( str [ len - 1 ] ) == true ) co [ len - 1 ] = 1 ; else vo [ len - 1 ] = 1 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( isCons ( str [ i ] ) == true ) { co [ i ] = co [ i + 1 ] + 1 ; vo [ i ] = vo [ i + 1 ] ; } else { co [ i ] = co [ i + 1 ] ; vo [ i ] = vo [ i + 1 ] + 1 ; } } long ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) ans = ans + co [ i + 1 ] ; else ans = ans + vo [ i + 1 ] ; } return ( int ) ans ; } public static void Main ( String [ ] args ) { String str = " adceba " ; Console . WriteLine ( countSpecial ( str . ToCharArray ( ) ) ) ; } }
Convert the string into palindrome string by changing only one character | C # program to Check if it is possible to convert the string into palindrome string by changing only one character . ; Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Function
using System ; class GFG { static bool checkPalindrome ( string str ) { int n = str . Length ; int count = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) if ( str [ i ] != str [ n - i - 1 ] ) ++ count ; return ( count <= 1 ) ; } public static void Main ( ) { string str = " abccaa " ; if ( checkPalindrome ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Counting even decimal value substrings in a binary string | C # Program to count all even decimal value substring . ; function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generate by this character produce even decimal value . ; Driver code
using System ; class GFG { static int evenDecimalValue ( string str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { result += ( n - i ) ; } } return result ; } public static void Main ( ) { string str = "10010" ; int n = 5 ; Console . WriteLine ( evenDecimalValue ( str , n ) ) ; } }