codeparrot/xlcost-text-to-code huggingface dataset:
Longest Palindromic Subsequence of two distinct characters | C ++ implementation to find Longest Palindromic Subsequence consisting of two distinct characters only ; Function that prints the length of maximum required subsequence ; Calculate length of string ; Store number of occurrences of each character and position of each chatacter in string ; Iterate all characters ; Calculate number of occurences of the current character ; Iterate half of the number of positions of current character ; Determine maximum length of a character between l and r position ; Compute the maximum from all ; Printing maximum length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestPalindrome ( string s ) { int n = s . length ( ) ; vector < vector < int > > pref ( 26 , vector < int > ( n , 0 ) ) ; vector < vector < int > > pos ( 26 ) ; pref [ s [ 0 ] - ' a ' ] [ 0 ] ++ ; pos [ s [ 0 ] - ' a ' ] . push_back ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) pref [ j ] [ i ] += pref [ j ] [ i - 1 ] ; int index = s [ i ] - ' a ' ; pref [ index ] [ i ] ++ ; pos [ index ] . push_back ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int size = pos [ i ] . size ( ) ; ans = max ( ans , size ) ; for ( int j = 0 ; j < size / 2 ; j ++ ) { int l = pos [ i ] [ j ] ; int r = pos [ i ] [ size - j - 1 ] - 1 ; for ( int k = 0 ; k < 26 ; k ++ ) { int sum = pref [ k ] [ r ] - pref [ k ] [ l ] ; ans = max ( ans , 2 * ( j + 1 ) + sum ) ; } } } cout << ans << " STRNEWLINE " ; } int main ( ) { string S = " bbccdcbb " ; longestPalindrome ( S ) ; return 0 ; }
Check if a given string can be formed using characters of adjacent cells of a Matrix | C ++ Program to check if a given word can be formed from the adjacent characters in a matrix of characters ; Function to check if the word exists ; If index exceeds board range ; If the current cell does not contain the required character ; If the cell contains the required character and is the last character of the word required to be matched ; Return true as word is found ; Mark cell visited ; Check Adjacent cells for the next character ; Restore cell value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkWord ( vector < vector < char > > & board , string & word , int index , int row , int col ) { if ( row < 0 || col < 0 || row >= board . size ( ) || col >= board [ 0 ] . size ( ) ) return false ; if ( board [ row ] [ col ] != word [ index ] ) return false ; else if ( index == word . size ( ) - 1 ) return true ; char temp = board [ row ] [ col ] ; board [ row ] [ col ] = ' * ' ; if ( checkWord ( board , word , index + 1 , row + 1 , col ) || checkWord ( board , word , index + 1 , row - 1 , col ) || checkWord ( board , word , index + 1 , row , col + 1 ) || checkWord ( board , word , index + 1 , row , col - 1 ) ) { board [ row ] [ col ] = temp ; return true ; } board [ row ] [ col ] = temp ; return false ; } int main ( ) { vector < vector < char > > board = { { ' A ' , ' B ' , ' C ' , ' E ' } , { ' S ' , ' F ' , ' C ' , ' S ' } , { ' A ' , ' D ' , ' E ' , ' E ' } } ; string word = " CFDASABCESEE " ; for ( int i = 0 ; i < board . size ( ) ; i ++ ) { for ( int j = 0 ; j < board [ 0 ] . size ( ) ; j ++ ) { if ( board [ i ] [ j ] == word [ 0 ] && checkWord ( board , word , 0 , i , j ) ) { cout << " True " << ' ' ; return 0 ; } } } cout << " False " << ' ' ; return 0 ; }
Split the number N by maximizing the count of subparts divisible by K | C ++ program to split the number N by maximizing the count of subparts divisible by K ; Function to count the subparts ; Total subStr till now ; If it can be divided , then this substring is one of the possible answer ; Convert string to long long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string N , int X , string subStr , int index , int n ) { if ( index == n ) return 0 ; string a = subStr + N [ index ] ; int b = 0 ; if ( stoll ( a ) % X == 0 ) b = 1 ; int m1 = count ( N , X , a , index + 1 , n ) ; int m2 = b + count ( N , X , " " , index + 1 , n ) ; return max ( m1 , m2 ) ; } int main ( ) { string N = "00001242" ; int K = 3 ; int l = N . length ( ) ; cout << count ( N , K , " " , 0 , l ) << endl ; return 0 ; }
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 ; Convert numbers into strings ; Find the lengths of strings s1 and s2 ; Base Case ; Traverse the strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Driver Code ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool checkSuffix ( int A , int B ) { string s1 = to_string ( A ) ; string s2 = to_string ( B ) ; int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 < n2 ) { return false ; } for ( int i = 0 ; i < n2 ; i ++ ) { if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) { return false ; } } return true ; } int main ( ) { int A = 12345 , B = 45 ; bool result = checkSuffix ( A , B ) ; if ( result ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
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 ; Convert numbers into strings ; Check if s2 is a suffix of s1 or not using ends_with ( ) function ; If result is true , print " Yes " ; Driver Code ; Given numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; void checkSuffix ( int A , int B ) { string s1 = to_string ( A ) ; string s2 = to_string ( B ) ; bool result ; result = boost :: algorithm :: ends_with ( s1 , s2 ) ; if ( result ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int A = 12345 , B = 45 ; checkSuffix ( A , B ) ; return 0 ; }
Binary string with given frequencies of sums of consecutive pairs of characters | C ++ program to generate a binary string with given frequencies of sums of consecutive pair of characters ; A Function that generates and returns the binary string ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string build_binary_str ( int p , int q , int r ) { string ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " - 1" ; } else { if ( p ) { ans = string ( p + 1 , '0' ) ; } else { ans = string ( r + 1 , '1' ) ; } } } else { for ( int i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += '0' ; } else { ans += '1' ; } } ans . insert ( 1 , string ( p , '0' ) ) ; ans . insert ( 0 , string ( r , '1' ) ) ; } return ans ; } int main ( ) { int p = 1 , q = 2 , r = 2 ; cout << build_binary_str ( p , q , r ) ; return 0 ; }
Check if there exists a permutation of given string which doesn 't contain any monotonous substring | C ++ implementation such that there are no monotonous string in given string ; Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { bool ok = true ; for ( int i = 0 ; i + 1 < s . size ( ) ; ++ i ) ok &= ( abs ( s [ i ] - s [ i + 1 ] ) != 1 ) ; return ok ; } string monotonousString ( string s ) { string odd = " " , even = " " ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { if ( s [ i ] % 2 == 0 ) odd += s [ i ] ; else even += s [ i ] ; } sort ( odd . begin ( ) , odd . end ( ) ) ; sort ( even . begin ( ) , even . end ( ) ) ; if ( check ( odd + even ) ) return " Yes " ; else if ( check ( even + odd ) ) return " Yes " ; return " No " ; } int main ( ) { string str = " abcd " ; string ans ; ans = monotonousString ( str ) ; cout << ans << endl ; return 0 ; }
Length of the smallest substring which contains all vowels | C ++ Program to find the length of the smallest substring of which contains all vowels ; Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current substring ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid substring ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return -1 ; } int findMinLength ( string s ) { int n = s . size ( ) ; int ans = n + 1 ; int start = 0 ; int count [ 5 ] = { 0 } ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s [ x ] ) ; if ( idx != -1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s [ start ] ) ; while ( idx_start == -1 count [ idx_start ] > 1 ) { if ( idx_start != -1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s [ start ] ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return -1 ; return ans ; } int main ( ) { string s = " aaeebbeaccaaoiuooooooooiuu " ; cout << findMinLength ( s ) ; return 0 ; }
Number of strings which starts and ends with same character after rotations | C ++ program for the above approach ; Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStrings ( string s ) { int cnt = 0 ; for ( int i = 1 ; s [ i ] ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { cnt ++ ; } } return cnt ; } int main ( ) { string str ( " aacbb " ) ; cout << countStrings ( str ) ; return 0 ; }
Check if frequency of each character is equal to its position in English Alphabet | C ++ program for the above approach ; Initialise frequency array ; Traverse the string ; Update the frequency ; Check for valid string ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Driver Code ; Given string str
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool checkValidString ( string str ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; str [ i ] ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } int main ( ) { string str = " abbcccdddd " ; if ( checkValidString ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Print all the non | C ++ program to print all the non - repeating words from the two given sentences ; Function to print all the non - repeating words from the two given sentences ; Concatenate the two strings into one ; Iterating over the whole concatenated string ; Searching for the word in A . If while searching , we reach the end of the string , then the word is not present in the string ; Initialise word for the next iteration ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; void removeRepeating ( string s1 , string s2 ) { string s3 = s1 + " ▁ " + s2 + " ▁ " ; string words = " " ; int i = 0 ; for ( auto x : s3 ) { if ( x == ' ▁ ' ) { if ( s1 . find ( words ) == string :: npos || s2 . find ( words ) == string :: npos ) cout << words ; words = " " ; } else { words = words + x ; } } } int main ( ) { string s1 = " I ▁ have ▁ go ▁ a ▁ pen " ; string s2 = " I ▁ want ▁ to ▁ go ▁ park " ; removeRepeating ( s1 , s2 ) ; return 0 ; }
Convert the number from International system to Indian system | C ++ program to convert the number from International system to Indian system ; Function to convert a number represented in International numeric system to Indian numeric system . ; Find the length of the input string ; Removing all the separators ( , ) from the input string ; Reverse the input string ; Declaring the output string ; Process the input string ; Add a separator ( , ) after the third number ; Then add a separator ( , ) after every second number ; Reverse the output string ; Return the output string back to the main function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string convert ( string input ) { int len = input . length ( ) ; for ( int i = 0 ; i < len ; ) { if ( input [ i ] == ' , ▁ ' ) { input . erase ( input . begin ( ) + i ) ; len -- ; i -- ; } else if ( input [ i ] == ' ▁ ' ) { input . erase ( input . begin ( ) + i ) ; len -- ; i -- ; } else { i ++ ; } } reverse ( input . begin ( ) , input . end ( ) ) ; string output ; for ( int i = 0 ; i < len ; i ++ ) { if ( i == 2 ) { output += input [ i ] ; output += " , ▁ " ; } else if ( i > 2 && i % 2 == 0 && i + 1 < len ) { output += input [ i ] ; output += " , ▁ " ; } else { output += input [ i ] ; } } reverse ( output . begin ( ) , output . end ( ) ) ; return output ; } int main ( ) { string input1 = "123 , ▁ 456 , ▁ 789" ; string input2 = "90 , ▁ 050 , ▁ 000 , ▁ 000" ; cout << convert ( input1 ) << endl ; cout << convert ( input2 ) ; }
Construct the Cypher string based on the given conditions | C ++ program for the above approach ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0' ; Function to make the Cypher string ; Resultant string ; Make string for the number N ; Check for semiPrime ; Traverse to make Cypher string ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher string ; If index is odd then current character is changed ; Else add the current character ; If the resultant string is " " then print - 1 ; Else print the resultant string ; Driver Code ; Given Number ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } bool isPossibleSum ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) { return true ; } else { return false ; } } bool checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i , ++ cnt ; } } if ( num > 1 ) { ++ cnt ; } return cnt == 2 ; } void makeCypherString ( int N ) { string semiPrime = " " ; string sumOfPrime = " " ; string str = to_string ( N ) ; if ( checkSemiprime ( N ) ) { for ( int i = 0 ; str [ i ] ; i ++ ) { if ( i & 1 ) { semiPrime += str [ i ] ; } else { semiPrime += char ( str [ i ] - '0' + 65 ) ; } } } if ( isPossibleSum ( N ) ) { for ( int i = 0 ; str [ i ] ; i ++ ) { if ( i & 1 ) { sumOfPrime += char ( str [ i ] - '0' + 65 ) ; } else { sumOfPrime += str [ i ] ; } } } if ( semiPrime + sumOfPrime == " " ) { cout << " - 1" ; } else { cout << semiPrime + sumOfPrime ; } } int main ( ) { int N = 1011243 ; makeCypherString ( N ) ; return 0 ; }
Maximum splits in binary string such that each substring is divisible by given odd number | C ++ Program to split a given binary string into maximum possible segments divisible by given odd number K ; Function to calculate maximum splits possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_segments ( string str , int K ) { int n = str . length ( ) ; int s = 0 , sum = 0 , count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int a = str [ i ] - '0' ; sum += a * pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) cout << " - 1" << endl ; else cout << count << endl ; } int main ( ) { string str = "10111001" ; int K = 5 ; max_segments ( str , K ) ; return 0 ; }
Make the string lexicographically smallest and non palindromic by replacing exactly one character | C ++ program to make the string lexicographically smallest non palindromic string by replacing exactly one character ; Function to find the required string ; length of the string ; Iterate till half of the string ; replacing a non ' a ' char with ' a ' ; Check if there is no ' a ' in string we replace last char of string by ' b ' ; If the input is a single character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findStr ( string S ) { int n = S . size ( ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' a ' ) { S [ i ] = ' a ' ; return S ; } } S [ n - 1 ] = ' b ' ; return n < 2 ? " ▁ - 1 ▁ " : S ; } int main ( ) { string str = " a " ; cout << findStr ( str ) << endl ; string str1 = " abccba " ; cout << findStr ( str1 ) << endl ; return 0 ; }
Find the k | C ++ program find the Kth string in lexicographical order consisting of N - 2 xs and 2 ys ; Function to find the Kth string in lexicographical order which consists of N - 2 xs and 2 ys ; Iterate for all possible positions of Left most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kth_string ( int n , int k ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( k <= ( n - i - 1 ) ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == i or j == n - k ) cout << ' Y ' ; else cout << ' X ' ; } break ; } k -= ( n - i - 1 ) ; } } int main ( ) { int n = 5 , k = 7 ; kth_string ( n , k ) ; return 0 ; }
Transform string str1 into str2 by taking characters from string str3 | C ++ program of the above approach ; Function to check whether str1 can be transformed to str2 ; To store the frequency of characters of string str3 ; Declare two pointers & flag ; Traverse both the string and check whether it can be transformed ; If both pointers point to same characters increment them ; If the letters don 't match check  if we can find it in string C ; If the letter is available in string str3 , decrement it 's  frequency & increment the ptr2 ; If letter isn 't present in str3[]  set the flag to false and break ; If the flag is true and both pointers points to their end of respective strings then it is possible to transformed str1 into str2 , otherwise not . ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convertString ( string str1 , string str2 , string str3 ) { map < char , int > freq ; for ( int i = 0 ; str3 [ i ] ; i ++ ) { freq [ str3 [ i ] ] ++ ; } int ptr1 = 0 ; int ptr2 = 0 ; bool flag = true ; while ( ptr1 < str1 . length ( ) && ptr2 < str2 . length ( ) ) { if ( str1 [ ptr1 ] == str2 [ ptr2 ] ) { ptr1 ++ ; ptr2 ++ ; } else { if ( freq [ str3 [ ptr2 ] ] > 0 ) { freq [ str3 [ ptr2 ] ] -- ; ptr2 ++ ; } else { flag = false ; break ; } } } if ( flag && ptr1 == str1 . length ( ) && ptr2 == str2 . length ( ) ) { cout << " YES " << endl ; } else { cout << " NO " << endl ; } } int main ( ) { string str1 = " abyzfe " ; string str2 = " abcdeyzf " ; string str3 = " popode " ; convertString ( str1 , str2 , str3 ) ; return 0 ; }
Decrypt the String according to given algorithm | C ++ implementation of the approach ; Function that returns ( num % 26 ) ; Initialize result ; One by one process all digits of ' num ' ; Function to return the decrypted string ; To store the final decrypted answer ; One by one check for each character if it is a numeric character ; Modulo the number found in the string by 26 ; Driver code ; Print the decrypted string
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 26 ; int modulo_by_26 ( string num ) { int res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) res = ( res * 10 + ( int ) num [ i ] - '0' ) % MOD ; return res ; } string decrypt_message ( string s ) { string decrypted_str = " " ; string num_found_so_far = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { num_found_so_far += s [ i ] ; } else if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ' a ' + modulo_by_26 ( num_found_so_far ) ; num_found_so_far = " " ; } } if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ' a ' + modulo_by_26 ( num_found_so_far ) ; } return decrypted_str ; } int main ( ) { string s = "32ytAAcV4ui30hf10hj18" ; cout << decrypt_message ( s ) ; return 0 ; }
Maximum number of overlapping string | C ++ implementation to find the maximum number of occurrence of the overlapping count ; Function to find the maximum overlapping strings ; Get the current character ; Condition to check if the current character is the first character of the string T then increment the overlapping count ; Condition to check previous character is also occurred ; Update count of previous and current character ; Condition to check the current character is the last character of the string T ; Condition to check the every subsequence is a valid string T ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOverlap ( string S , string T ) { string str = T ; int count [ T . length ( ) ] = { 0 } ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i <= S . length ( ) ; i ++ ) { int index = str . find ( S [ i ] ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) max_overlap = max ( overlap , max_overlap ) ; count [ index ] ++ ; } else { if ( count [ index - 1 ] <= 0 ) return -1 ; count [ index ] ++ ; count [ index - 1 ] -- ; } if ( index == 4 ) overlap -- ; } if ( overlap == 0 ) return max_overlap ; else return -1 ; } int main ( ) { string S = " chcirphirp " ; string T = " chirp " ; cout << maxOverlap ( S , T ) ; return 0 ; }