codeparrot/xlcost-text-to-code huggingface dataset:
Find value after N operations to remove N characters of string S with given constraints | C ++ program for the above approach ; Function to find the value after N operations to remove all the N characters of string S ; Iterate till N ; Remove character at ind and decrease n ( size of string ) ; Increase count by ind + 1 ; Driver Code ; Given string str ; Function call
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; void charactersCount ( string str , int n ) { int count = 0 ; while ( n > 0 ) { char cur = str [ 0 ] ; int ind = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( str [ j ] < cur ) { cur = str [ j ] ; ind = j ; } } str . erase ( str . begin ( ) + ind ) ; n -- ; count += ind + 1 ; } cout << count << endl ; } int main ( ) { string str = " aabbc " ; int n = 5 ; charactersCount ( str , n ) ; return 0 ; }
Print the middle character of a string | C ++ program to implement the above approach ; Function that prints the middle character of a string ; Finding string length ; Finding middle index of string ; Print the middle character of the string ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMiddleCharacter ( string str ) { int len = str . size ( ) ; int middle = len / 2 ; cout << str [ middle ] ; } int main ( ) { string str = " GeeksForGeeks " ; printMiddleCharacter ( str ) ; return 0 ; }
Maximize length of the String by concatenating characters from an Array of Strings | C ++ Program to implement the above approach ; Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { set < char > a ; for ( auto i : s ) { if ( a . count ( i ) ) return false ; a . insert ( i ) ; } return true ; } vector < string > helper ( vector < string > & arr , int ind ) { if ( ind == arr . size ( ) ) return { " " } ; vector < string > tmp = helper ( arr , ind + 1 ) ; vector < string > ret ( tmp . begin ( ) , tmp . end ( ) ) ; for ( auto i : tmp ) { string test = i + arr [ ind ] ; if ( check ( test ) ) ret . push_back ( test ) ; } return ret ; } int maxLength ( vector < string > & arr ) { vector < string > tmp = helper ( arr , 0 ) ; int len = 0 ; for ( auto i : tmp ) { len = len > i . size ( ) ? len : i . size ( ) ; } return len ; } int main ( ) { vector < string > s ; s . push_back ( " abcdefgh " ) ; cout << maxLength ( s ) ; return 0 ; }
Perform range sum queries on string as per given condition | C ++ program for the above approach ; Function to perform range sum queries on string as per the given condition ; Initialize N by string size ; Create array A [ ] for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Driver Code ; Given string ; Given Queries ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Range_sum_query ( string S , vector < pair < int , int > > Query ) { int N = S . length ( ) ; int A [ N ] ; A [ 0 ] = S [ 0 ] - ' a ' + 1 ; for ( int i = 1 ; i < N ; i ++ ) { A [ i ] = S [ i ] - ' a ' + 1 ; A [ i ] = A [ i ] + A [ i - 1 ] ; } for ( int i = 0 ; i < Query . size ( ) ; i ++ ) { if ( Query [ i ] . first == 1 ) { cout << A [ ( Query [ i ] . second ) - 1 ] << endl ; } else { cout << A [ ( Query [ i ] . second ) - 1 ] - A [ ( Query [ i ] . first ) - 2 ] << endl ; } } } int main ( ) { string S = " abcd " ; vector < pair < int , int > > Query ; Query . push_back ( make_pair ( 2 , 4 ) ) ; Query . push_back ( make_pair ( 1 , 3 ) ) ; Range_sum_query ( S , Query ) ; return 0 ; }
Generate all possible permutations of a Number divisible by N | C ++ Program to implement the above approach ; Function to Swap two characters ; Function to generate all permutations and print the ones that are divisible by the N ; Convert string to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap_ ( char & a , char & b ) { char temp ; temp = a ; a = b ; b = temp ; } void permute ( char * str , int l , int r , int n ) { int i ; if ( l == r ) { int j = atoi ( str ) ; if ( j % n == 0 ) cout << str << endl ; return ; } for ( i = l ; i < r ; i ++ ) { swap_ ( str [ l ] , str [ i ] ) ; permute ( str , l + 1 , r , n ) ; swap_ ( str [ l ] , str [ i ] ) ; } } int main ( ) { char str [ 100 ] = "125" ; int n = 5 ; int len = strlen ( str ) ; if ( len > 0 ) permute ( str , 0 , len , n ) ; return 0 ; }
Check if binary representations of 0 to N are present as substrings in given binary string | C ++ program for the above approach ; Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the string as a substring or not ; To store the count of number exists as a substring ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the string s is a substring of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0' ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decimalToBinary ( int N ) { string ans = " " ; while ( N > 0 ) { if ( N & 1 ) { ans = '1' + ans ; } else { ans = '0' + ans ; } N /= 2 ; } return ans ; } string checkBinaryString ( string & str , int N ) { int map [ N + 10 ] , cnt = 0 ; memset ( map , 0 , sizeof ( map ) ) ; for ( int i = N ; i > 0 ; i -- ) { if ( ! map [ i ] ) { int t = i ; string s = decimalToBinary ( t ) ; if ( str . find ( s ) != str . npos ) { while ( t && ! map [ t ] ) { map [ t ] = 1 ; cnt ++ ; t >>= 1 ; } } } } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '0' ) { cnt ++ ; break ; } } if ( cnt == N + 1 ) return " True " ; else return " False " ; } int main ( ) { string str = "0110" ; int N = 3 ; cout << checkBinaryString ( str , N ) ; return 0 ; }
Find the word with most anagrams in a given sentence | C ++ Program to find the word with most anagrams in a sentence ; Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestAnagramGrp ( vector < string > arr ) { int prime [ ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 } ; int max = -1 ; long maxpdt = -1 ; unordered_map < long long , string > W ; unordered_map < long long , long > P ; for ( string temp : arr ) { long pdt = 1 ; for ( char t : temp ) { pdt *= prime [ t - ' a ' ] ; } if ( P . find ( pdt ) != P . end ( ) ) { P [ pdt ] ++ ; } else { W [ pdt ] = temp ; P [ pdt ] = 1 ; } } for ( auto e : P ) { if ( max < e . second ) { max = e . second ; maxpdt = e . first ; } } return W [ maxpdt ] ; } int main ( ) { char S [ ] = " please ▁ be ▁ silent ▁ and ▁ listen ▁ to ▁ what ▁ the ▁ professor ▁ says ▁ " ; vector < string > arr ; char * token = strtok ( S , " ▁ " ) ; while ( token != NULL ) { arr . push_back ( token ) ; token = strtok ( NULL , " ▁ " ) ; } cout << largestAnagramGrp ( arr ) << endl ; return 0 ; }
Replace every vowels with lexicographically next vowel in a String | C ++ program to convert all the vowels in in the string to the next vowel ; Function to replace every vowel with next vowel lexicographically ; Storing the vowels in the map with custom numbers showing their index ; Iterate over the string ; If the current character is a vowel Find the index in Hash and Replace it with next vowel from Hash ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string print_next_vovel_string ( string str ) { map < char , int > m ; m [ ' a ' ] = 0 ; m [ ' e ' ] = 1 ; m [ ' i ' ] = 2 ; m [ ' o ' ] = 3 ; m [ ' u ' ] = 4 ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; int N = str . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { char c = str [ i ] ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { int index = m + 1 ; int newindex = index % 5 ; str [ i ] = arr [ newindex ] ; } } return str ; } int main ( ) { string str = " geeksforgeeks " ; cout << print_next_vovel_string ( str ) ; return 0 ; }
Check if string is palindrome after removing all consecutive duplicates | C ++ program for the above approach ; Function to check if a string is palindrome or not ; Length of the string ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as str is palindromic ; Function to check if string str is palindromic after removing every consecutive characters from the str ; Length of the string str ; Create an empty compressed string ; The first character will always be included in the final string ; Check all the characters of the string ; If the current character is not same as its previous one , then insert it in the final string ; Check if the compressed string is a palindrome ; Driver Code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != str [ len - i - 1 ] ) return false ; } return true ; } bool isCompressablePalindrome ( string str ) { int len = str . length ( ) ; string compressed = " " ; compressed . push_back ( str [ 0 ] ) ; for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] != str [ i - 1 ] ) compressed . push_back ( str [ i ] ) ; } return isPalindrome ( compressed ) ; } int main ( ) { string str = " abbcbbbaaa " ; if ( isCompressablePalindrome ( str ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Count of substrings consisting only of vowels | C ++ program to Count all substrings in a string which contains only vowels ; Function to check vowel or not ; Function to Count all substrings in a string which contains only vowels ; Check if current character is vowel ; Increment length of substring ; Calculate possible substrings of calculated length ; Reset the length ; Add remaining possible substrings consisting of vowels occupying last indices of the string ; Driver Program
#include <iostream> NEW_LINE using namespace std ; bool isvowel ( char ch ) { return ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) ; } int CountTotal ( string & s ) { int ans = 0 ; int n = s . length ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isvowel ( s [ i ] ) ) cnt += 1 ; else { ans += ( cnt * ( cnt + 1 ) / 2 ) ; cnt = 0 ; } } if ( cnt != 0 ) { ans += ( cnt * ( cnt + 1 ) / 2 ) ; } return ans ; } int main ( ) { string s = " geeksforgeeks " ; cout << ( CountTotal ( s ) ) << endl ; return 0 ; }
Number formed by flipping all bits to the left of rightmost set bit | C ++ program to find the integer formed after flipping all bits to the left of the rightmost set bit ; Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totCount ; int firstCount ; void getTotCount ( int num ) { totCount = 1 ; firstCount = 1 ; int temp = 1 ; while ( ( num & temp ) == 0 ) { temp = temp << 1 ; totCount += 1 ; } firstCount = totCount ; temp = num >> totCount ; while ( temp != 0 ) { totCount += 1 ; temp = temp >> 1 ; } } int flipBitsFromRightMostSetBit ( int num ) { getTotCount ( num ) ; int num1 = num ^ ( ( 1 << totCount ) - 1 ) ; num1 = num1 ^ ( ( 1 << firstCount ) - 1 ) ; return num1 ; } int main ( ) { int n = 120 ; cout << flipBitsFromRightMostSetBit ( n ) << endl ; return 0 ; }
Longest substring of vowels with no two adjacent alphabets same | C ++ implementation of the above approach ; Function to check a character is vowel or not ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; Stores length of current valid subString ; If curr and prev character are not same , include it ; If same as prev one , start new subString from here ; Store max in maxLen ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isvowel ( char c ) { return c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ; } int findMaxLen ( string & s ) { int maxLen = 0 ; int cur = 0 ; if ( isvowel ( s [ 0 ] ) ) cur = maxLen = 1 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( isvowel ( s [ i ] ) ) { if ( s [ i ] != s [ i - 1 ] ) cur += 1 ; else cur = 1 ; } else { cur = 0 ; } maxLen = max ( cur , maxLen ) ; } return maxLen ; } int main ( ) { string Str = " aeoibsddaeiouudb " ; cout << findMaxLen ( Str ) << endl ; return 0 ; }
Count of non | C ++ Program to count non - palindromic strings of length M using N distinct characters ; Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long power ( unsigned long long base , unsigned long long pow ) { unsigned long long res = 1 ; while ( pow > 0 ) { if ( pow & 1 ) res = ( res * base ) ; base = ( base * base ) ; pow >>= 1 ; } return res ; } unsigned long long countNonPalindromicString ( unsigned long long n , unsigned long long m ) { unsigned long long total = power ( n , m ) ; unsigned long long palindrome = power ( n , m / 2 + m % 2 ) ; unsigned long long count = total - palindrome ; return count ; } int main ( ) { int n = 3 , m = 5 ; cout << countNonPalindromicString ( n , m ) ; return 0 ; }
Count of K | C ++ program for the above approach ; To store the frequency array ; Function to check palindromic of of any substring using frequency array ; Initialise the odd count ; Traversing frequency array to compute the count of characters having odd frequency ; Returns true if odd count is atmost 1 ; Function to count the total number substring whose any permutations are palindromic ; Computing the frequency of first K character of the string ; To store the count of palindromic permutations ; Checking for the current window if it has any palindromic permutation ; Start and end point of window ; Decrementing count of first element of the window ; Incrementing count of next element of the window ; Checking current window character frequency count ; Return the final count ; Driver Code ; Given string str ; Window of size K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > freq ( 26 ) ; bool checkPalindrome ( ) { int oddCnt = 0 ; for ( auto x : freq ) { if ( x % 2 == 1 ) oddCnt ++ ; } return oddCnt <= 1 ; } int countPalindromePermutation ( string s , int k ) { for ( int i = 0 ; i < k ; i ++ ) { freq [ s [ i ] - 97 ] ++ ; } int ans = 0 ; if ( checkPalindrome ( ) ) { ans ++ ; } int i = 0 , j = k ; while ( j < s . size ( ) ) { freq [ s [ i ++ ] - 97 ] -- ; freq [ s [ j ++ ] - 97 ] ++ ; if ( checkPalindrome ( ) ) { ans ++ ; } } return ans ; } int main ( ) { string str = " abbaca " ; int K = 3 ; cout << countPalindromePermutation ( str , K ) << endl ; return 0 ; }
Minimum flips required to form given binary string where every flip changes all bits to its right as well | C ++ program for the above approach ; Function to return the count of minimum flips required ; If curr occurs in the final string ; Switch curr to '0' if '1' or vice - versa ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlips ( string target ) { char curr = '1' ; int count = 0 ; for ( int i = 0 ; i < target . length ( ) ; i ++ ) { if ( target [ i ] == curr ) { count ++ ; curr = ( char ) ( 48 + ( curr + 1 ) % 2 ) ; } } return count ; } int main ( ) { string S = "011000" ; cout << ( minFlips ( S ) ) ; }
Check if a number ends with another number or not | C ++ program for the above approach ; Function to check if B is a suffix of A or not ; Find the number of digit in B ; Subtract B from A ; Returns true , if B is a suffix of A ; Driver Code ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; bool checkSuffix ( int A , int B ) { int digit_B = log10 ( B ) + 1 ; A -= B ; return ( A % int ( pow ( 10 , digit_B ) ) ) ; } int main ( ) { int A = 12345 , B = 45 ; bool result = checkSuffix ( A , B ) ; if ( ! result ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Minimum length of substring whose rotation generates a palindromic substring | C ++ Program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_min_length ( string s ) { int hash [ 26 ] ; int ans = INT_MAX ; for ( int i = 0 ; i < 26 ; i ++ ) hash [ i ] = -1 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( hash [ s [ i ] - ' a ' ] == -1 ) hash [ s [ i ] - ' a ' ] = i ; else { if ( hash [ s [ i ] - ' a ' ] == i - 1 hash [ s [ i ] - ' a ' ] == i - 2 ) return 0 ; ans = min ( ans , i - hash [ s [ i ] - ' a ' ] - 1 ) ; hash [ s [ i ] - ' a ' ] = i ; } } if ( ans == INT_MAX ) return -1 ; return ans ; } int main ( ) { string str = " abcdeba " ; cout << count_min_length ( str ) ; }
Program to remove HTML tags from a given String | C ++ program for the above approach ; Function to remove the HTML tags from the given string ; Use regex_replace function in regex to erase every tags enclosed in < > ; Print string after removing tags ; Driver Code ; Given String ; Function call to print the HTML string after removing tags
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; void RemoveHTMLTags ( string s ) { const regex pattern ( " \\ < . * ? \\ > " ) ; s = regex_replace ( s , pattern , " " ) ; cout << s ; return ; } int main ( ) { string str = " < div > < b > Geeks ▁ for ▁ Geeks < / b > < / div > " ; RemoveHTMLTags ( str ) ; return 0 ; }
Score of Parentheses using Tree | C ++ program to find the score of parentheses using Tree ; Customized tree class or struct , contains all required methods . ; Function to add a child into the list of children ; Function to change the parent pointer to the node passed ; Function to return the parent of the current node ; Function to compute the score recursively . ; Base case ; Adds scores of all children ; Function to create the tree structure ; Creating a node for every " ( ) " ; If we find " ( " we add a node as a child ; On finding " ) " which confirms that a pair is closed , we go back to the parent ; Driver code ; Generating the tree ; Computing the score
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; class TreeNode { TreeNode * parent = NULL ; vector < TreeNode * > children ; public : void addChild ( TreeNode * node ) { children . push_back ( node ) ; } void setParent ( TreeNode * node ) { parent = node ; } TreeNode * getParent ( ) { return parent ; } int computeScore ( ) { if ( children . size ( ) == 0 ) return 1 ; int res = 0 ; for ( TreeNode * curr : children ) res += curr -> computeScore ( ) ; if ( parent == NULL ) return res ; else return 2 * res ; } } ; TreeNode * computeTree ( string s ) { TreeNode * current = new TreeNode ( ) ; TreeNode * root = current ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' ( ' ) { TreeNode * child = new TreeNode ( ) ; child -> setParent ( current ) ; current -> addChild ( child ) ; current = child ; } else { current = current -> getParent ( ) ; } } return root ; } int main ( ) { string s = " ( ( ) ( ( ) ) ) " ; TreeNode * root = computeTree ( s ) ; cout << root -> computeScore ( ) ; return 0 ; }
Move all occurrence of letter ' x ' from the string s to the end using Recursion | C ++ program for above approach ; Recursive program to bring ' x ' to the end ; When the string is completed from reverse direction end of recursion ; If the character x is found ; Transverse the whole string ; Swap the x so that it moves to the last ; call to the smaller problem now ; Driver Code ; Size of a ; Call to rec
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rec ( char * a , int i ) { if ( i == 0 ) { cout << a << endl ; return ; } if ( a [ i ] == ' x ' ) { int j = i ; while ( a [ j ] != ' \0' && a [ j + 1 ] != ' \0' ) { swap ( a [ j ] , a [ j + 1 ] ) ; j ++ ; } } rec ( a , i - 1 ) ; } int main ( ) { char a [ ] = { ' g ' , ' e ' , ' e ' , ' k ' , ' x ' , ' s ' , ' x ' , ' x ' , ' k ' , ' s ' , ' \0' } ; int n = 10 ; rec ( a , n - 1 ) ; }
Generate a string of size N whose each substring of size M has exactly K distinct characters | C ++ program to generate a string of size N whose each substring of size M has exactly K distinct characters ; Function to generate the string ; Declare empty string ; counter for M ; counter for K ; Loop to generate string size of N ; Generating K distinct letters one by one ; After generating b distinct letters , append rest a - b letters as ' a ' ; Reset the counter value and repeat the process ; return final result string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generateString ( int N , int M , int K ) { string s = " " ; int cnt1 = 0 ; int cnt2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt1 ++ ; cnt2 ++ ; if ( cnt1 <= M ) { if ( cnt2 <= K ) { s = s + char ( 96 + cnt1 ) ; } else { s = s + ' a ' ; } } else { cnt1 = 1 ; cnt2 = 1 ; s = s + ' a ' ; } } return s ; } int main ( ) { int N = 7 , M = 5 , K = 3 ; cout << generateString ( N , M , K ) << endl ; return 0 ; }