codeparrot/xlcost-text-to-code huggingface dataset:
Minimize changes to make all characters equal by changing vowel to consonant and vice versa | C ++ program for the above approach ; Function to find the minimum number of steps to make all characters equal ; Initializing the variables ; Store the frequency ; Iterate over the string ; Calculate the total number of vowels ; Storing frequency of each vowel ; Count the consonants ; Storing the frequency of each consonant ; Iterate over the 2 maps ; Maximum frequency of consonant ; Maximum frequency of vowel ; Find the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int operations ( string s ) { int ans = 0 ; int vowels = 0 , consonants = 0 ; int max_vowels = INT_MIN ; int max_consonants = INT_MIN ; unordered_map < char , int > freq_consonants ; unordered_map < char , int > freq_vowels ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) { vowels += 1 ; freq_vowels [ s [ i ] ] += 1 ; } else { consonants += 1 ; freq_consonants [ s [ i ] ] += 1 ; } } for ( auto it = freq_consonants . begin ( ) ; it != freq_consonants . end ( ) ; it ++ ) { max_consonants = max ( max_consonants , it -> second ) ; } for ( auto it = freq_vowels . begin ( ) ; it != freq_vowels . end ( ) ; it ++ ) { max_vowels = max ( max_vowels , it -> second ) ; } ans = min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) ; return ans ; } int main ( ) { string S = " geeksforgeeks " ; cout << operations ( S ) ; return 0 ; }
Count of ways to empty given String by recursively removing all adjacent duplicates | C ++ implementation for the above approach ; Define the dp table globally ; Recursive function to calculate the dp values for range [ L , R ] ; The range is odd length ; The state is already calculated ; If the length is 2 ; Total answer for this state ; Variable to store the current answer . ; Remove characters s [ l ] and s [ i ] . ; Initialize all the states of dp to - 1 ; Calculate all Combinations ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 505 ] [ 505 ] , choose [ 502 ] [ 502 ] ; int calc ( int l , int r , string & s ) { if ( abs ( r - l ) % 2 == 0 ) { return 0 ; } if ( l > r ) { return dp [ l ] [ r ] = 1 ; } if ( dp [ l ] [ r ] != -1 ) { return dp [ l ] [ r ] ; } if ( ( r - l ) == 1 ) { if ( s [ l ] == s [ r ] ) { dp [ l ] [ r ] = 1 ; } else { dp [ l ] [ r ] = 0 ; } return dp [ l ] [ r ] ; } int ans = 0 ; for ( int k = l + 1 ; k <= r ; k += 2 ) { int temp = 1 ; if ( s [ l ] == s [ k ] ) { temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ ( r - l + 1 ) / 2 ] [ ( r - k ) / 2 ] ; ans += temp ; } } return dp [ l ] [ r ] = ans ; } int waysToClearString ( string S ) { memset ( dp , -1 , sizeof ( dp ) ) ; int n = S . length ( ) ; choose [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n / 2 ; ++ i ) { choose [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; ++ j ) { choose [ i ] [ j ] = ( choose [ i - 1 ] [ j ] + choose [ i - 1 ] [ j - 1 ] ) ; } } return calc ( 0 , n - 1 , S ) ; } int main ( ) { string S = " aabccb " ; cout << waysToClearString ( S ) ; return 0 ; }
Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | C ++ program for the above approach ; Function to check for valid subsequences ; Stores which element belongs to which subsequence ; Check for each pos if a possible subsequence exist or not ; Last member of 1 subsequence ; Last Member of 2 nd subsequence ; Check if current element can go to 2 nd subsequence ; Check if the current elements belongs to first subsequence ; If the current element does not belong to any subsequence ; Check if last digit of first subsequence is greater than pos ; If a subsequence is found , find the subsequences ; Stores the resulting subsequences ; Print the subsequence ; If no subsequence found , print - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubsequence ( string str ) { int n = str . size ( ) ; char res [ n ] ; for ( int i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( int pos = 0 ; pos <= 9 ; pos ++ ) { char lst1 = '0' ; bool flag = 1 ; char lst2 = pos + '0' ; for ( int i = 0 ; i < n ; i ++ ) { if ( lst2 <= str [ i ] ) { res [ i ] = '2' ; lst2 = str [ i ] ; } else if ( lst1 <= str [ i ] ) { res [ i ] = '1' ; lst1 = str [ i ] ; } else flag = 0 ; } if ( lst1 > pos + '0' ) flag = 0 ; if ( flag ) { string S1 = " " ; string S2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( res [ i ] == '1' ) { S1 += str [ i ] ; } else { S2 += str [ i ] ; } } cout << S1 << ' ▁ ' << S2 << endl ; return ; } } cout << " - 1" ; } int main ( ) { string S = "040425524644" ; findSubsequence ( S ) ; S = "123456789" ; findSubsequence ( S ) ; return 0 ; }
Find Kth lexicographical ordered numeric string of length N with distinct products of each substring | C ++ program for the above approach ; Function to find the required string ; If the current length is equal to n exit from here only ; Iterate for all the characters ; Check if the product is present before ; If the current string is good then recurse for the next character ; Decrease all the products back to their original state ; Erase the last character ; Function to calculate kth ordered valid string ; Check for the base cases ; There are atmost 10 valid strings for n = 1 ; Vector to keep a check on number of occurences of products ; Recursively construct the strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getString ( int curlen , string & ans , string & s , int N , int & K , vector < int > & prod ) { if ( curlen == N ) { K -- ; if ( K == 0 ) ans = s ; return ; } char ch ; int ok , t , i ; for ( ch = '2' ; ch <= '9' ; ch ++ ) { s += ch ; ok = 1 ; t = 1 ; for ( i = curlen ; i >= 0 ; i -- ) { t *= s [ i ] - 48 ; if ( prod [ t ] ) ok = 0 ; prod [ t ] ++ ; } if ( ok ) getString ( curlen + 1 , ans , s , N , K , prod ) ; t = 1 ; for ( i = curlen ; i >= 0 ; i -- ) { t *= s [ i ] - 48 ; prod [ t ] -- ; } s . erase ( s . length ( ) - 1 ) ; } } string kthValidString ( int N , int K ) { if ( N > 10 ) { return " - 1" ; } if ( N == 1 ) { if ( K > 10 ) { return " - 1" ; } string s = " " ; K -- ; s += ( K + '0' ) ; return s ; } string ans = " - 1" ; string s = " " ; vector < int > prod ( 10005 , 0 ) ; getString ( 0 , ans , s , N , K , prod ) ; return ans ; } int main ( ) { int N = 3 , K = 4 ; cout << kthValidString ( N , K ) ; }
Minimum swaps to balance the given brackets at any index | C ++ implementation for the above approach ; Function to balance the given bracket by swap ; To count the number of uunbalanced pairs ; if there is an opening bracket and we encounter closing bracket then it will decrement the count of unbalanced bracket . ; else it will increment unbalanced pair count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int BalancedStringBySwapping ( string s ) { int unbalancedPair = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( unbalancedPair > 0 && s [ i ] == ' ] ' ) { -- unbalancedPair ; } else if ( s [ i ] == ' [ ' ) { ++ unbalancedPair ; } } return ( unbalancedPair + 1 ) / 2 ; } int main ( ) { string s = " ] ] ] [ [ [ " ; cout << ( BalancedStringBySwapping ( s ) ) ; return 0 ; }
Count of palindromic strings of size upto N consisting of first K alphabets occurring at most twice | C ++ program for the above approach ; Function of return the number of palindromic strings of length N with first K alphabets possible ; If N is odd , half + 1 position can be filled to cope with the extra middle element ; K is reduced by one , because count of choices for the next position is reduced by 1 as a element can only once ; Return the possible count ; Function to find the count of palindromic string of first K characters according to the given criteria ; If N = 1 , then only K palindromic strings possible . ; If N = 2 , the 2 * K palindromic strings possible , K for N = 1 and K for N = 2 ; Initialize ans with the count of strings possible till N = 2 ; Return the possible count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lengthNPalindrome ( int N , int K ) { int half = N / 2 ; if ( N & 1 ) { half += 1 ; } int ans = 1 ; for ( int i = 1 ; i <= half ; i ++ ) { ans *= K ; K -- ; } return ans ; } int palindromicStrings ( int N , int K ) { if ( N == 1 ) { return K ; } if ( N == 2 ) { return 2 * K ; } int ans = 0 ; ans += ( 2 * K ) ; for ( int i = 3 ; i <= N ; i ++ ) { ans += lengthNPalindrome ( i , K ) ; } return ans ; } int main ( ) { int N = 4 , K = 3 ; cout << palindromicStrings ( N , K ) ; return 0 ; }
Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | C ++ program for the above approach ; Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int LCS ( string & S , int N , string & T , int M , vector < vector < int > > & dp ) { if ( N == 0 or M == 0 ) return 0 ; if ( dp [ N ] [ M ] != -1 ) return dp [ N ] [ M ] ; if ( S [ N - 1 ] == T [ M - 1 ] ) { return dp [ N ] [ M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; } return dp [ N ] [ M ] = max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; } int minimumCharacter ( string & S ) { string T = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) , M = T . length ( ) ; vector < vector < int > > dp ( N + 1 , vector < int > ( M + 1 , -1 ) ) ; return ( 26 - LCS ( S , N , T , M , dp ) ) ; } int main ( ) { string S = " abcdadc " ; cout << minimumCharacter ( S ) ; return 0 ; }
Count ways to make the number formed by K concatenations of a numeric string divisible by 5 | C ++ program for the above approach ; Function to find the value of a ^ b modulo 1000000007 ; Stores the resultant value a ^ b ; Find the value of a ^ b ; Function to count the number of ways such that the formed number is divisible by 5 by removing digits ; Stores the count of ways ; Find the count for string S ; If the digit is 5 or 0 ; Find the count of string for K concatenation of string S ; Find the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long LL ; const int MOD = 1000000007 ; int exp_mod ( LL a , LL b ) { LL ret = 1 ; for ( ; b ; b >>= 1 , a = a * a % MOD ) { if ( b & 1 ) ret = ret * a % MOD ; } return ret ; } int countOfWays ( string s , int k ) { int N = s . size ( ) ; LL ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '5' s [ i ] == '0' ) { ans = ( ans + exp_mod ( 2 , i ) ) % MOD ; } } LL q = exp_mod ( 2 , N ) ; LL qk = exp_mod ( q , k ) ; LL inv = exp_mod ( q - 1 , MOD - 2 ) ; ans = ans * ( qk - 1 ) % MOD ; ans = ans * inv % MOD ; return ans ; } int main ( ) { string S = "1256" ; int K = 1 ; cout << countOfWays ( S , K ) ; return 0 ; }
Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | C ++ Program for the above approach ; Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printMiddleString ( string S , string T , int N ) { vector < int > a1 ( N + 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = S [ i ] - ' a ' + T [ i ] - ' a ' ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( a1 [ i ] & 1 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] /= 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { cout << char ( a1 [ i ] + ' a ' ) ; } return 0 ; } int main ( ) { int N = 5 ; string S = " afogk " ; string T = " asdji " ; printMiddleString ( S , T , N ) ; }
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | C ++ program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char KthCharacter ( string s , string t , long k ) { long f = 1 ; long ss = 2 ; string tmp = " " ; int len = tmp . length ( ) ; while ( len < k ) { long tf = f ; long ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ( ) ; } char output = tmp [ k - 1 ] ; return output ; } int main ( ) { string S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; cout << ans ; }
Minimum count of 0 s to be removed from given Binary string to make all 1 s occurs consecutively | C ++ program for the above approach ; Function to find minimum count of 0 s removed from the string S such that all 1 s occurs consecutively ; Stores the first and the last occurrence of 1 ; Iterate over the characters the string , S ; If current character is '1' ; Update fst_occur ; Iterate over the characters the string , S ; If current character is '1' ; Update lst_occur ; Stores the count of 0 s between fst_occur and lst_occur ; Iterate over the characters of S between fst_occur and lst_occur ; If current character is '0' ; Update count ; Print the resultant minimum count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makeContiguous ( string S , int N ) { int fst_occur , lst_occur ; for ( int x = 0 ; x < N ; x ++ ) { if ( S [ x ] == '1' ) { fst_occur = x ; break ; } } for ( int x = N - 1 ; x >= 0 ; x -- ) { if ( S [ x ] == '1' ) { lst_occur = x ; break ; } } int count = 0 ; for ( int x = fst_occur ; x <= lst_occur ; x ++ ) { if ( S [ x ] == '0' ) { count ++ ; } } cout << count ; } int main ( ) { string S = "010001011" ; int N = S . size ( ) ; makeContiguous ( S , N ) ; return 0 ; }
Lexicographically smallest string with period K possible by replacing ' ? ' s from a given string | C ++ program for the above approach ; Function to modify the given string such that string S is a period of K ; Iterate over the range [ 0 , K ] ; Stores the frequency of the characters S [ i + j * K ] ; Iterate over the string with increment of K ; Print " - 1" ; Replace all characters of the string with ' ? ' to ' a ' to make it smallest ; Otherwise ; Find the character other than ' ? ' ; Replace all characters of the string with ' ? ' to character ch ; Clear the map M ; Return the modified string ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; string modifyString ( string & S , int K ) { int N = S . length ( ) ; for ( int i = 0 ; i < K ; i ++ ) { map < char , int > M ; for ( int j = i ; j < N ; j += K ) { M [ S [ j ] ] ++ ; } if ( M . size ( ) > 2 ) { return " - 1" ; } else if ( M . size ( ) == 1 ) { if ( M [ ' ? ' ] != 0 ) { for ( int j = i ; j < N ; j += K ) { S [ j ] = ' a ' ; } } } else if ( M . size ( ) == 2 ) { char ch ; for ( auto & it : M ) { if ( it . first != ' ? ' ) { ch = it . first ; } } for ( int j = i ; j < N ; j += K ) { S [ j ] = ch ; } } M . clear ( ) ; } return S ; } int main ( ) { string S = " ab ? ? " ; int K = 2 ; cout << modifyString ( S , K ) ; return 0 ; }
Count of ways to rearrange N digits and M alphabets keeping all alphabets together | C ++ program for the above approach ; Function to find the factorial of the given number ; Function to count ways to rearrange characters of the string such that all alphabets are adjacent . ; Stores factorial of ( N + 1 ) ; Stores factorial of ; Driver Code ; Given a and b ; int M = 2 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } int findComb ( int N , int M ) { int x = fact ( N + 1 ) ; int y = fact ( M ) ; return ( x * y ) ; } int main ( ) { int N = 2 ; cout << findComb ( N , M ) ; }
Count of camel case characters present in a given string | C ++ program for the above approach ; Function to count all the camelcase characters in the string S ; Stores the total count of camelcase characters ; Traverse the string S ; If ASCII value of character lies over the range [ 65 , 91 ] then increment the count ; Print the total count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCamelCase ( string & S ) { int count = 0 ; for ( int i = 0 ; S [ i ] ; i ++ ) { if ( S [ i ] >= 65 && S [ i ] <= 91 ) { count ++ ; } } cout << count ; } int main ( ) { string S = " ckjkUUYII " ; countCamelCase ( S ) ; return 0 ; }
Decode a given string by removing duplicate occurrences | C ++ program for the above approach ; Function to count the appearances of each character ; If the character is lower case ; If the character is uppercase ; If the character is a punctuation mark ; Function to decode the given encoded string ; Iterate the given string str ; Find the index of the next character to be printed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRepitition ( char a ) { if ( a <= ' z ' && a >= ' a ' ) { return a - ' a ' ; } else if ( a <= ' Z ' && a >= ' A ' ) { return a - ' A ' ; } return 0 ; } void decodeString ( string str ) { string output = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { output . push_back ( str [ i ] ) ; i += findRepitition ( str [ i ] ) ; } cout << " Decrypted ▁ code ▁ is ▁ { " << output << " } " << endl ; } int main ( ) { string str = " abbbb ▁ acccdddd " ; decodeString ( str ) ; return 0 ; }
Program to perform a letter frequency attack on a monoalphabetic substitution cipher | C ++ program for the above approach ; Function to decrypt a monoalphabetic substitution cipher using the letter frequency attack ; Stores final 5 possible deciphered plaintext ; Store the frequency of each letter in cipher text ; Stores the frequency of each letter in cipher text in descending order ; Store which alphabet is used already ; Traverse the string S ; Copy the frequency array ; Stores the string formed from concatanating the english letters in the decreasing frequency in the english language ; Sort the array in descending order ; Itearate over the range [ 0 , 5 ] ; Iterate over the range [ 0 , 26 ] ; Store the numerical equivalent of letter at ith index of array letter_frequency ; Calculate the probable shift used in monoalphabetic cipher ; Temporary string to generate one plaintext at a time ; Generate the probable ith plaintext string using the shift calculated above ; Insert whitespaces as it is ; Shift the kth letter of the cipher by x ; Add the kth calculated / shifted letter to temporary string ; Print the generated 5 possible plaintexts ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printString ( string S , int N ) { string plaintext [ 5 ] ; int freq [ 26 ] = { 0 } ; int freqSorted [ 26 ] ; int Used [ 26 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' ▁ ' ) { freq [ S [ i ] - ' A ' ] ++ ; } } for ( int i = 0 ; i < 26 ; i ++ ) { freqSorted [ i ] = freq [ i ] ; } string T = " ETAOINSHRDLCUMWFGYPBVKJXQZ " ; sort ( freqSorted , freqSorted + 26 , greater < int > ( ) ) ; for ( int i = 0 ; i < 5 ; i ++ ) { int ch = -1 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freqSorted [ i ] == freq [ j ] && Used [ j ] == 0 ) { Used [ j ] = 1 ; ch = j ; break ; } } if ( ch == -1 ) break ; int x = T [ i ] - ' A ' ; x = x - ch ; string curr = " " ; for ( int k = 0 ; k < N ; k ++ ) { if ( S [ k ] == ' ▁ ' ) { curr += ' ▁ ' ; continue ; } int y = S [ k ] - ' A ' ; y += x ; if ( y < 0 ) y += 26 ; if ( y > 25 ) y -= 26 ; curr += ' A ' + y ; } plaintext [ i ] = curr ; } for ( int i = 0 ; i < 5 ; i ++ ) { cout << plaintext [ i ] << endl ; } } int main ( ) { string S = " B ▁ TJNQMF ▁ NFTTBHF " ; int N = S . length ( ) ; printString ( S , N ) ; return 0 ; }
Count of substrings in a Binary String that contains more 1 s than 0 s | C ++ program for the above approach ; Function to merge two partitions such that the merged array is sorted ; Counting inversions ; Function to implement merge sort ; Function to calculate number of inversions in a given array ; Calculate the number of inversions ; Return the number of inversions ; Function to count the number of substrings that contains more 1 s than 0 s ; Stores the prefix sum array ; Stores the count of valid substrings ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void merge ( vector < int > & v , int left , int mid , int right , int & inversions ) { vector < int > temp ( right - left + 1 ) ; int i = left ; int j = mid + 1 ; int k = 0 ; int cnt = 0 ; while ( i <= mid && j <= right ) { if ( v [ i ] <= v [ j ] ) { temp [ k ++ ] = v [ i ++ ] ; } else { inversions += ( mid - i + 1 ) ; temp [ k ++ ] = v [ j ++ ] ; } } while ( i <= mid ) temp [ k ++ ] = v [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = v [ j ++ ] ; k = 0 ; for ( int a = left ; a <= right ; a ++ ) { v [ a ] = temp [ k ++ ] ; } } void mergeSort ( vector < int > & v , int left , int right , int & inversions ) { if ( left < right ) { int mid = ( left + right ) / 2 ; mergeSort ( v , left , mid , inversions ) ; mergeSort ( v , mid + 1 , right , inversions ) ; merge ( v , left , mid , right , inversions ) ; } } int CountInversions ( vector < int > & v ) { int n = v . size ( ) ; int inversions = 0 ; mergeSort ( v , 0 , n - 1 , inversions ) ; return inversions ; } int getSubsCount ( string & input ) { int n = input . length ( ) ; vector < int > nums ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { nums [ i ] = input [ i ] - '0' ; if ( nums [ i ] == 0 ) nums [ i ] = -1 ; } vector < int > pref ( n ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += nums [ i ] ; pref [ i ] = sum ; } int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( pref [ i ] > 0 ) cnt ++ ; } reverse ( pref . begin ( ) , pref . end ( ) ) ; int inversions = CountInversions ( pref ) ; int ans = cnt + inversions ; return ans ; } int main ( ) { string input = "101" ; int ans = getSubsCount ( input ) ; cout << ans << endl ; return 0 ; }
Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | C ++ program for the above approach ; Function to check if the given string S can be made empty ; Stores the characters of the string S ; Traverse the given string ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string canMadeEmpty ( string s , int n ) { stack < char > St ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' c ' ) { if ( St . size ( ) >= 2 ) { char b = St . top ( ) ; St . pop ( ) ; char a = St . top ( ) ; St . pop ( ) ; if ( a != ' a ' b != ' b ' ) return " No " ; } else return " No " ; } else St . push ( s [ i ] ) ; } if ( St . size ( ) == 0 ) { return " Yes " ; } else { return " No " ; } } int main ( ) { string S = " aabcbc " ; int N = S . length ( ) ; cout << canMadeEmpty ( S , N ) ; return 0 ; }