codeparrot/xlcost-text-to-code huggingface dataset:
Count strings from given array having all characters appearing in a given string | C ++ program to implement the above approach ; Function to count the number of strings from an array having all characters appearing in the string S ; Initialize a set to store all distinct characters of string S ; Traverse over string S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over string arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the string S or not ; Increment the count if all the characters of arr [ i ] are present in the string S ; Finally , print the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStrings ( string S , vector < string > & list ) { unordered_set < char > valid ; for ( auto x : S ) { valid . insert ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . size ( ) ; j ++ ) { if ( valid . count ( list [ i ] [ j ] ) ) continue ; else break ; } if ( j == list [ i ] . size ( ) ) cnt ++ ; } return cnt ; } int main ( ) { vector < string > arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; string S = " ab " ; cout << countStrings ( S , arr ) << endl ; }
Minimize a string by removing all occurrences of another string | C ++ program for the above approach ; Function to find the minimum length to which string str can be reduced to by removing all occurrences of string K ; Initialize stack of characters ; Push character into the stack ; If stack size >= K . size ( ) ; Create empty string to store characters of stack ; Traverse the string K in reverse ; If any of the characters differ , it means that K is not present in the stack ; Push the elements back into the stack ; Store the string ; Remove top element ; Size of stack gives the minimized length of str ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( string str , int N , string K , int M ) { stack < char > stackOfChar ; for ( int i = 0 ; i < N ; i ++ ) { stackOfChar . push ( str [ i ] ) ; if ( stackOfChar . size ( ) >= M ) { string l = " " ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( K [ j ] != stackOfChar . top ( ) ) { int f = 0 ; while ( f != l . size ( ) ) { stackOfChar . push ( l [ f ] ) ; f ++ ; } break ; } else { l = stackOfChar . top ( ) + l ; stackOfChar . pop ( ) ; } } } } return stackOfChar . size ( ) ; } int main ( ) { string S1 = " fffoxoxoxfxo " ; string S2 = " fox " ; int N = S1 . length ( ) ; int M = S2 . length ( ) ; cout << minLength ( S1 , N , S2 , M ) ; return 0 ; }
Largest palindrome not exceeding N which can be expressed as product of two 3 | C ++ program for the above approach ; Function to find the largest palindrome not exceeding N which can be expressed as the product of two 3 - digit numbers ; Stores all palindromes ; Stores the product ; Check if X is palindrome ; Check n is less than N ; If true , append it in the list ; Print the largest palindrome ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void palindrome_prod ( string N ) { vector < int > palindrome_list ; for ( int i = 101 ; i < 1000 ; i ++ ) { for ( int j = 121 ; j < 1000 ; j += ( i % 11 == 0 ) ? 1 : 11 ) { int n = i * j ; string x = to_string ( n ) ; string y = x ; reverse ( y . begin ( ) , y . end ( ) ) ; if ( x == y ) { if ( n < stoi ( N ) ) { palindrome_list . push_back ( i * j ) ; } } } } cout << ( * max_element ( palindrome_list . begin ( ) , palindrome_list . end ( ) ) ) ; } int main ( ) { string N = "101110" ; palindrome_prod ( N ) ; return 0 ; }
Check if given strings can be made same by swapping two characters of same or different strings | C ++ program to implement the above approach ; Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] [ j ] ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isEqualStrings ( string arr [ ] , int N ) { int M = arr [ 0 ] . length ( ) ; int cntFreq [ 256 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] [ j ] - ' a ' ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } int main ( ) { string arr [ ] = { " aab " , " bbc " , " cca " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isEqualStrings ( arr , N ) ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }
Minimize a binary string by repeatedly removing even length substrings of same characters | C ++ program for the above approach ; Recursive function to print stack elements from bottom to top without changing their order ; If stack is empty ; Pop top element of the stack ; Recursively call the function PrintStack ; Print the stack element from the bottom ; Push the same element onto the stack to preserve the order ; Function to minimize binary string by removing substrings consisting of same character ; Declare a stack of characters ; Push the first character of the string into the stack ; Traverse the string s ; If Stack is empty ; Push current character into the stack ; Check if the current character is same as the top of the stack ; If true , pop the top of the stack ; Otherwise , push the current element ; Print stack from bottom to top ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintStack ( stack < char > s ) { if ( s . empty ( ) ) return ; char x = s . top ( ) ; s . pop ( ) ; PrintStack ( s ) ; cout << x ; s . push ( x ) ; } void minString ( string s ) { stack < char > Stack ; Stack . push ( s [ 0 ] ) ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { if ( Stack . empty ( ) ) { Stack . push ( s [ i ] ) ; } else { if ( Stack . top ( ) == s [ i ] ) { Stack . pop ( ) ; } else { Stack . push ( s [ i ] ) ; } } } PrintStack ( Stack ) ; } int main ( ) { string str = "101001" ; minString ( str ) ; return 0 ; }
Probability of collision between two trucks | C ++ program for the above approach ; Function to calculate total number of accidents ; String size ; Function to calculate count of all possible collision ; Stores the count of collisions ; Total number of truck in lane b ; Count total number of collisions while traversing the string a ; Function to calculate the probability of collisions ; Evaluate total outcome that is all the possible accident ; Evaluate favourable outcome i . e . , count of collision of trucks ; Print desired probability ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double count_of_accident ( string a , string b ) { int n = a . size ( ) , m = b . size ( ) ; if ( n > m ) return ( m * ( m + 1 ) ) / 2 ; else return ( n * ( n + 1 ) ) / 2 + ( m - n ) * n ; } double count_of_collision ( string a , string b ) { int n = a . size ( ) , m = b . size ( ) ; int answer = 0 ; int count_of_truck_in_lane_b = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] == ' T ' ) count_of_truck_in_lane_b ++ ; for ( int i = 0 ; i < n && i < m ; i ++ ) { if ( a [ i ] == ' T ' ) answer += count_of_truck_in_lane_b ; if ( b [ i ] == ' T ' ) count_of_truck_in_lane_b -- ; } return answer ; } double findProbability ( string a , string b ) { double total_outcome = count_of_accident ( a , b ) ; double favourable_outcome = count_of_collision ( a , b ) ; cout << favourable_outcome / total_outcome ; } int main ( ) { string S = " TCCBCTTB " , T = " BTCCBBTT " ; findProbability ( S , T ) ; return 0 ; }
Count alphanumeric palindromes of length N | C ++ program for the above approach ; Function to calculate ( x ^ y ) mod p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Return the final result ; Driver Code ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } int main ( ) { int N = 3 ; int flag , k , m ; if ( ( N == 1 ) || ( N == 2 ) ) cout << 62 ; else m = 1000000000 + 7 ; if ( N % 2 == 0 ) { k = N / 2 ; flag = true ; } else { k = ( N - 1 ) / 2 ; flag = false ; } if ( flag != 0 ) { int a = power ( 62 , k , m ) ; cout << a ; } else { int a = power ( 62 , ( k + 1 ) , m ) ; cout << a ; } }
Length of the longest substring with every character appearing even number of times | C ++ program for the above approach ; Function to find length of the longest substring with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required substring ; Traverse the string ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Driver Code ; Given string ; Length of the given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lenOfLongestReqSubstr ( string s , int N ) { unordered_map < int , int > ind ; int mask = 0 ; ind [ 0 ] = -1 ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = s [ i ] - '0' ; mask ^= ( 1 << val ) ; if ( ind . find ( mask ) != ind . end ( ) ) { ans = max ( ans , i - ind [ mask ] ) ; } else ind [ mask ] = i ; } return ans ; } int main ( ) { string s = "223015150" ; int N = s . length ( ) ; cout << lenOfLongestReqSubstr ( s , N ) ; return 0 ; }
Count distinct strings possible by replacing each character by its Morse code | C ++ program to implement the above approach ; Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of string by replacing each character by Morse code ; Stores length of arr [ ] array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current string ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int uniqueMorseRep ( vector < string > & arr ) { vector < string > morseCode = { " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " } ; set < string > st ; int N = arr . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { string temp = " " ; int M = arr [ i ] . length ( ) ; for ( int j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] [ j ] - ' a ' ] ; } st . insert ( temp ) ; } return st . size ( ) ; } int main ( ) { vector < string > arr = { " gig " , " zeg " , " gin " , " msn " } ; cout << uniqueMorseRep ( arr ) << endl ; }
Maximum repeating character for every index in given String | C ++ Program to implement the above approach ; Function to print the maximum repeating character at each index of the String ; Stores frequency of each distinct character ; Stores frequency of maximum repeating character ; Stores the character having maximum frequency ; Traverse the String ; Stores current character ; Update the frequency of str [ i ] ; If frequency of current character exceeds max ; Update max ; Update charMax ; Print the required output ; Driver Code ; Stores length of str
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFreq ( string str , int N ) { int freq [ 256 ] ; memset ( freq , 0 , sizeof ( freq ) ) ; int max = 0 ; char charMax = '0' ; for ( int i = 0 ; i < N ; i ++ ) { char ch = str [ i ] ; freq [ ch ] ++ ; if ( freq [ ch ] >= max ) { max = freq [ ch ] ; charMax = ch ; } cout << charMax << " - > " << max << endl ; } } int main ( ) { string str = " abbc " ; int N = str . size ( ) ; findFreq ( str , N ) ; }
Minimize cost to convert given string to a palindrome | C ++ program for the above approach ; Function to find the minimum cost to convert the string into a palindrome ; Length of the string ; If iointer is in the second half ; Reverse the string ; Pointing to initial position ; Find the farthest index needed to change on left side ; Find the farthest index needed to change on right side ; Changing the variable to make string palindrome ; min distance to travel to make string palindrome ; Total cost ; Return the minimum cost ; Driver Code ; Given string S ; Given pointer P ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinCost ( string str , int pos ) { int n = str . length ( ) ; if ( pos >= n / 2 ) { reverse ( str . begin ( ) , str . end ( ) ) ; pos = n - pos - 1 ; } int left , right ; left = right = pos ; for ( int i = pos ; i >= 0 ; -- i ) { if ( str [ i ] != str [ n - i - 1 ] ) { left = i ; } } for ( int i = pos ; i < n / 2 ; ++ i ) { if ( str [ i ] != str [ n - i - 1 ] ) { right = i ; } } int ans = 0 ; for ( int i = left ; i <= right ; ++ i ) { if ( str [ i ] != str [ n - i - 1 ] ) ans += 1 ; } int dis = min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ; ans = ans + dis ; return ans ; } int main ( ) { string S = " bass " ; int P = 3 ; cout << findMinCost ( S , P ) ; return 0 ; }
Minimize replacements by previous or next alphabet required to make all characters of a string the same | C ++ program for the above approach ; Function to find the minimum count of operations to make all characters of the string same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( string s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += min ( abs ( i - ( s [ j ] - ' a ' ) ) , 26 - abs ( i - ( s [ j ] - ' a ' ) ) ) ; } minValue = min ( minValue , cnt ) ; } return minValue ; } int main ( ) { string str = " geeksforgeeks " ; int N = str . length ( ) ; cout << minCost ( str , N ) ; return 0 ; }
Longest palindromic string possible by concatenating strings from a given array | C ++ program for the above approach ; Stores the distinct strings from the given array ; Insert the strings into set ; Stores the left and right substrings of the given string ; Stores the middle substring ; Traverse the array of strings ; Reverse the current string ; Checking if the string is itself a palindrome or not ; Check if the reverse of the string is present or not ; Append to the left substring ; Append to the right substring ; Erase both the strings from the set ; Print the left substring ; Print the middle substring ; Print the right substring ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_len ( string s [ ] , int N , int M ) { unordered_set < string > set_str ; for ( int i = 0 ; i < N ; i ++ ) { set_str . insert ( s [ i ] ) ; } vector < string > left_ans , right_ans ; string mid ; for ( int i = 0 ; i < N ; i ++ ) { string t = s [ i ] ; reverse ( t . begin ( ) , t . end ( ) ) ; if ( t == s [ i ] ) { mid = t ; } else if ( set_str . find ( t ) != set_str . end ( ) ) { left_ans . push_back ( s [ i ] ) ; right_ans . push_back ( t ) ; set_str . erase ( s [ i ] ) ; set_str . erase ( t ) ; } } for ( auto x : left_ans ) { cout << x ; } cout << mid ; reverse ( right_ans . begin ( ) , right_ans . end ( ) ) ; for ( auto x : right_ans ) { cout << x ; } } int main ( ) { int N = 4 , M = 3 ; string s [ ] = { " omg " , " bbb " , " ffd " , " gmo " } ; max_len ( s , N , M ) ; return 0 ; }
Minimize steps defined by a string required to reach the destination from a given source | C ++ program for the above approach ; Function to find the minimum length of string required to reach from source to destination ; Size of the string ; Stores the index of the four directions E , W , N , S ; If destination reached ; Iterate over the string ; Move east ; Change x1 according to direction E ; Move west ; Change x1 according to direction W ; Move north ; Change y1 according to direction N ; Move south ; Change y1 according to direction S ; Store the max of all positions ; Print the minimum length of string required ; Otherwise , it is impossible ; Driver Code ; Given string ; Given source and destination ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimum_length ( int x1 , int y1 , int x2 , int y2 , string str ) { int n = str . size ( ) ; int pos1 , pos2 , pos3 , pos4 ; pos1 = -1 ; pos2 = -1 ; pos3 = -1 ; pos4 = -1 ; if ( x1 == x2 && y1 == y2 ) { cout << 0 << endl ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( x2 > x1 ) { if ( str [ i ] == ' E ' ) { x1 = x1 + 1 ; if ( x1 == x2 ) { pos1 = i ; } } } if ( x2 < x1 ) { if ( str [ i ] == ' W ' ) { x1 = x1 - 1 ; if ( x1 == x2 ) { pos2 = i ; } } } if ( y2 > y1 ) { if ( str [ i ] == ' N ' ) { y1 = y1 + 1 ; if ( y1 == y2 ) { pos3 = i ; } } } if ( y2 < y1 ) { if ( str [ i ] == ' S ' ) { y1 = y1 - 1 ; if ( y1 == y2 ) { pos4 = i ; } } } } int z ; z = max ( max ( max ( pos1 , pos2 ) , pos3 ) , pos4 ) ; if ( x1 == x2 && y1 == y2 ) { cout << z + 1 << endl ; } else { cout << " - 1" << endl ; } } } int main ( ) { string str = " SESNW " ; int x1 = 0 , x2 = 1 , y1 = 0 , y2 = 1 ; minimum_length ( x1 , y1 , x2 , y2 , str ) ; return 0 ; }
Remove all duplicate adjacent characters from a string using Stack | C ++ program to implement the above approach ; Function to remove adjacent duplicate elements ; Store the string without duplicate elements ; Store the index of str ; Traverse the string str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string ShortenString ( string str1 ) { stack < char > st ; int i = 0 ; while ( i < str1 . length ( ) ) { if ( st . empty ( ) || str1 [ i ] != st . top ( ) ) { st . push ( str1 [ i ] ) ; i ++ ; } else { st . pop ( ) ; i ++ ; } } if ( st . empty ( ) ) { return ( " Empty ▁ String " ) ; } else { string short_string = " " ; while ( ! st . empty ( ) ) { short_string = st . top ( ) + short_string ; st . pop ( ) ; } return ( short_string ) ; } } int main ( ) { string str1 = " azzxzy " ; cout << ShortenString ( str1 ) ; return 0 ; }
Count ways to place all the characters of two given strings alternately | C ++ Program to implement the above approach ; Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { res = res * i ; } return res ; } int distinctWays ( string str1 , string str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; if ( n == m ) { return 2 * fact ( n ) * fact ( m ) ; } return fact ( n ) * fact ( m ) ; } int main ( ) { string str1 = " aegh " ; string str2 = " rsw " ; cout << distinctWays ( str1 , str2 ) ; }
Smallest string without any multiplication sign that represents the product of two given numbers | C ++ program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver code ; Product A X B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string len ( long A , long B ) { string res = " " ; long Log = 0 ; do { Log = ( long ) ( log ( B ) / log ( 2 ) ) ; if ( Log != 0 ) { res = res + to_string ( A ) + " < < " + to_string ( Log ) ; } else { res += A ; break ; } B = B - ( long ) pow ( 2 , Log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( Log != 0 ) ; return res ; } void minimumString ( long A , long B ) { string res1 = len ( A , B ) ; string res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { cout << res2 << endl ; } else { cout << res1 << endl ; } } int main ( ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; return 0 ; }
Count substrings of a given string whose anagram is a palindrome | C ++ program for the above approach ; Function to print count of substrings whose anagrams are palindromic ; Stores the answer ; Iterate over the string ; Set the current character ; Parity update ; Print the final count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubstring ( string s ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int x = 0 ; for ( int j = i ; j < s . length ( ) ; j ++ ) { int temp = 1 << s [ j ] - ' a ' ; x ^= temp ; if ( ( x & ( x - 1 ) ) == 0 ) res ++ ; } } cout << res ; } int main ( ) { string str = " aaa " ; countSubstring ( str ) ; return 0 ; }
Count substrings of a given string whose anagram is a palindrome | C ++ program for the above approach ; Function to get the count of substrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubstring ( string s ) { int answer = 0 ; unordered_map < int , int > m ; m [ 0 ] = 1 ; int x = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { x ^= 1 << ( s [ j ] - ' a ' ) ; answer += m [ x ] ; for ( int i = 0 ; i < 26 ; ++ i ) { answer += m [ x ^ ( 1 << i ) ] ; } m [ x ] += 1 ; } cout << answer ; } int main ( ) { string str = " abab " ; countSubstring ( str ) ; return 0 ; }