codeparrot/xlcost-text-to-code huggingface dataset:
Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of string ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSmaller ( string str ) { int n = str . length ( ) ; int arr [ 26 ] = { 0 } ; int ans [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { arr [ str [ i ] - ' a ' ] ++ ; int ct = 0 ; for ( int j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { ct += arr [ j ] ; } ans [ i ] = ct ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } int main ( ) { string str = " edcbaa " ; countSmaller ( str ) ; return 0 ; }
Find the longest sub | C ++ program to find that substring which is its suffix prefix and also found somewhere in between ; Z - algorithm function ; BIT array ; bit update function which updates values from index " idx " to last by value " val " ; Query function in bit ; Driver Code ; Making the z array ; update in the bit array from index z [ i ] by increment of 1 ; if the value in z [ i ] is not equal to ( n - i ) then no need to move further ; queryng for the maximum length substring from bit array
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > z_function ( string s ) { int n = s . size ( ) ; vector < int > z ( n ) ; for ( int i = 1 , l = 0 , r = 0 ; i < n ; i ++ ) { if ( i <= r ) z [ i ] = min ( r - i + 1 , z [ i - l ] ) ; while ( i + z [ i ] < n && s [ z [ i ] ] == s [ i + z [ i ] ] ) z [ i ] ++ ; if ( i + z [ i ] - 1 > r ) l = i , r = i + z [ i ] - 1 ; } return z ; } int n , len = 0 ; int bit [ 1000005 ] ; string s ; vector < int > z ; map < int , int > m ; void update ( int idx , int val ) { if ( idx == 0 ) return ; while ( idx <= n ) { bit [ idx ] += val ; idx += ( idx & - idx ) ; } } int pref ( int idx ) { int ans = 0 ; while ( idx > 0 ) { ans += bit [ idx ] ; idx -= ( idx & - idx ) ; } return ans ; } int main ( ) { s = " geeksisforgeeksinplatformgeeks " ; n = s . size ( ) ; z = z_function ( s ) ; for ( int i = 1 ; i < n ; i ++ ) { update ( z [ i ] , 1 ) ; } for ( int i = n - 1 ; i > 1 ; i -- ) { if ( z [ i ] != ( n - i ) ) continue ; if ( pref ( n ) - pref ( z [ i ] - 1 ) >= 2 ) { len = max ( len , z [ i ] ) ; } } if ( ! len ) cout << " - 1" ; else cout << s . substr ( 0 , len ) ; return 0 ; }
Count of words ending at the given suffix in Java | C ++ implementation of the approach ; Function to return the count of words in the given sentence that end with the given suffix ; To store the count ; Extract words from the sentence ; For every word ; If it ends with the given suffix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; template < size_t N > void splitString ( string ( & arr ) [ N ] , string str ) { int n = 0 ; istringstream iss ( str ) ; for ( auto it = istream_iterator < string > ( iss ) ; it != istream_iterator < string > ( ) && n < N ; ++ it , ++ n ) arr [ n ] = * it ; } inline bool ends_with ( std :: string const & value , std :: string const & ending ) { if ( ending . size ( ) > value . size ( ) ) return false ; return std :: equal ( ending . rbegin ( ) , ending . rend ( ) , value . rbegin ( ) ) ; } int endingWith ( string str , string suff ) { int cnt = 0 ; const int size = 50 ; string words [ size ] ; splitString ( words , str ) ; for ( int i = 0 ; i < size ; i ++ ) { if ( ends_with ( words [ i ] , suff ) ) cnt ++ ; } return cnt ; } int main ( ) { string str = " GeeksForGeeks ▁ is ▁ a ▁ computer ▁ " " science ▁ portal ▁ for ▁ geeks " ; string suff = " ks " ; cout << endingWith ( str , suff ) ; }
Character whose frequency is equal to the sum of frequencies of other characters of the given string | C ++ implementation of the approach ; Function that returns true if some character exists in the given string whose frequency is equal to the sum frequencies of other characters of the string ; If string is of odd length ; To store the frequency of each character of the string ; Update the frequencies of the characters ; No such character exists ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isFrequencyEqual ( string str , int len ) { if ( len % 2 == 1 ) return false ; int i , freq [ 26 ] = { 0 } ; for ( i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] == len / 2 ) return true ; return false ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; if ( isFrequencyEqual ( str , len ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum replacements to make adjacent characters unequal in a ternary string | C ++ program to count the minimal replacements such that adjacent characters are unequal ; Function to count the number of minimal replacements ; Find the length of the string ; Iterate in the string ; Check if adjacent is similar ; If not the last pair ; Check for character which is not same in i + 1 and i - 1 ; else Last pair ; Check for character which is not same in i - 1 index ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinimalReplacements ( string s ) { int n = s . length ( ) ; int cnt = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { cnt += 1 ; if ( i != ( n - 1 ) ) { for ( auto it : "012" ) { if ( it != s [ i + 1 ] && it != s [ i - 1 ] ) { s [ i ] = it ; break ; } } } { for ( auto it : "012" ) { if ( it != s [ i - 1 ] ) { s [ i ] = it ; break ; } } } } } return cnt ; } int main ( ) { string s = "201220211" ; cout << countMinimalReplacements ( s ) ; return 0 ; }
Sub | C ++ implementation of the approach ; Function that returns the index of next occurrence of the character c in string str starting from index start ; Starting from start ; If current character = c ; Not found ; Function to return the count of required sub - strings ; Stores running count of ' x ' starting from the end ; Next index of ' x ' starting from index 0 ; Next index of ' y ' starting from index 0 ; To store the count of required sub - strings ; If ' y ' appears before ' x ' it won 't contribute to a valid sub-string ; Find next occurrence of ' y ' ; If ' y ' appears after ' x ' every sub - string ending at an ' x ' appearing after this ' y ' and starting with the current ' x ' is a valid sub - string ; Find next occurrence of ' x ' ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextIndex ( string str , int start , char c ) { for ( int i = start ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == c ) return i ; } return -1 ; } int countSubStrings ( string str ) { int i , n = str . length ( ) ; int countX [ n ] ; int count = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' x ' ) count ++ ; countX [ i ] = count ; } int nextIndexX = nextIndex ( str , 0 , ' x ' ) ; int nextIndexY = nextIndex ( str , 0 , ' y ' ) ; count = 0 ; while ( nextIndexX != -1 && nextIndexY != -1 ) { if ( nextIndexX > nextIndexY ) { nextIndexY = nextIndex ( str , nextIndexY + 1 , ' y ' ) ; continue ; } else { count += countX [ nextIndexY ] ; nextIndexX = nextIndex ( str , nextIndexX + 1 , ' x ' ) ; } } return count ; } int main ( ) { string s = " xyyxx " ; cout << countSubStrings ( s ) ; }
Check whether the frequencies of all the characters in a string are prime or not | C ++ implementation of above approach ; function that returns true if n is prime else false ; 1 is not prime ; check if there is any factor or not ; function that returns true if the frequencies of all the characters of s are prime ; create a map to store the frequencies of characters ; update the frequency ; check whether all the frequencies are prime or not ; Driver code ; if all the frequencies are prime
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { int i ; if ( n == 1 ) return false ; for ( i = 2 ; i <= sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } bool check_frequency ( string s ) { map < char , int > m ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) m [ s [ i ] ] ++ ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) if ( m [ ch ] > 0 && ! isPrime ( m [ ch ] ) ) return false ; return true ; } int main ( ) { string s = " geeksforgeeks " ; if ( check_frequency ( s ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Find the count of substrings in alphabetic order | CPP to find the number of substrings in alphabetical order ; Function to find number of substrings ; Iterate over string length ; if any two chars are in alphabetic order ; find next char not in order ; return the result ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubstringCount ( string str ) { int result = 0 ; int n = str . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] + 1 == str [ i + 1 ] ) { result ++ ; while ( str [ i ] + 1 == str [ i + 1 ] ) { i ++ ; } } } return result ; } int main ( ) { string str = " alphabet " ; cout << findSubstringCount ( str ) << endl ; return 0 ; }
Reduce the string by removing K consecutive identical characters | CPP program for the above approach ; Basic Approach is to create a Stack that store the Character and its continuous repetition number This is done using pair < char , int > Further we check at each iteration , whether the character matches the top of stack if it does then check for number of repetitions else add to top of stack with count 1 ; Base Case If k = 1 then all characters can be removed at each instance ; initialize string ; create a stack using pair < > for storing each character and corresponding repetition ; iterate through the string ; if stack is empty then simply add the character with count 1 else check if character is same as top of stack ; if character at top of stack is same as current character increase the number of repetitions in the top of stack by 1 ; if character at top of stack is not same as current character push the character along with count 1 into the top of stack ; Iterate through the stack Use string ( int , char ) in order to replicate the character multiple times and convert into string then add in front of output string ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; class Solution { public : string remove_k_char ( int k , string s ) { if ( k == 1 ) return " " ; string output = " " ; stack < pair < char , int > > stk ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( stk . empty ( ) == true ) { stk . push ( make_pair ( s [ i ] , 1 ) ) ; } else { if ( s [ i ] == ( stk . top ( ) ) . first ) { stk . push ( { s [ i ] , stk . top ( ) . second + 1 } ) ; if ( stk . top ( ) . second == k ) { int x = k ; while ( x ) { stk . pop ( ) ; x -- ; } } } else { stk . push ( make_pair ( s [ i ] , 1 ) ) ; } } } while ( ! stk . empty ( ) ) { output += stk . top ( ) . first ; stk . pop ( ) ; } reverse ( output . begin ( ) , output . end ( ) ) ; return output ; } } ; int main ( ) { string s = " geeksforgeeks " ; int k = 2 ; Solution obj ; cout << obj . remove_k_char ( k , s ) << " STRNEWLINE " ; return 0 ; }
First non | CPP program to find first non - repeating character using 1D array and one traversal . ; The function returns index of the first non - repeating character in a string . If all characters are repeating then returns INT_MAX ; Initialize all characters as absent . ; After below loop , the value of arr [ x ] is going to be index of of x if x appears only once . Else the value is going to be either - 1 or - 2. ; If this character occurs only once and appears before the current result , then update the result ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE int firstNonRepeating ( char * str ) { int arr [ NO_OF_CHARS ] ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) arr [ i ] = -1 ; for ( int i = 0 ; str [ i ] ; i ++ ) { if ( arr [ str [ i ] ] == -1 ) arr [ str [ i ] ] = i ; else arr [ str [ i ] ] = -2 ; } int res = INT_MAX ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( arr [ i ] >= 0 ) res = min ( res , arr [ i ] ) ; return res ; } int main ( ) { char str [ ] = " geeksforgeeks " ; int index = firstNonRepeating ( str ) ; if ( index == INT_MAX ) cout << " Either ▁ all ▁ characters ▁ are ▁ " " repeating ▁ or ▁ string ▁ is ▁ empty " ; else cout << " First ▁ non - repeating ▁ character " " ▁ is ▁ " << str [ index ] ; return 0 ; }
Character replacement after removing duplicates from a string | C ++ program for character replacement after string minimization ; Function to minimize string ; duplicate characters are removed ; checks if character has previously occurred or not if not then add it to the minimized string ' mstr ' ; return mstr ; minimized string ; Utility function to print the minimized , replaced string ; Creating final string by replacing character ; index calculation ; cout << " Final ▁ string : ▁ " << finalStr ; final string ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string minimize ( string str ) { string mstr = " ▁ " ; int l , i , flagchar [ 26 ] = { 0 } ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { ch = str . at ( i ) ; if ( flagchar [ ch - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch - 97 ] = 1 ; } } } void replaceMinimizeUtil ( string str ) { string minimizedStr , finalStr = " " ; int i , index , l ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < minimizedStr . length ( ) ; i ++ ) { ch = minimizedStr . at ( i ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str . at ( index ) ; } } int main ( ) { string str = " geeks " ; replaceMinimizeUtil ( str ) ; return 0 ; }
Latin alphabet cipher | Latin Alphabet Cipher Encryption header files ; function for calculating the encryption ; driver code
#include <bits/stdc++.h> NEW_LINE void cipher ( char str [ ] ) { for ( int i = 0 ; str [ i ] != ' \0' ; i ++ ) { if ( isalpha ( str [ i ] ) == 0 && str [ i ] != ' ▁ ' ) { printf ( " Enter ▁ only ▁ alphabets ▁ and ▁ space STRNEWLINE " ) ; return ; } } printf ( " Encrypted ▁ Code ▁ using ▁ Latin ▁ Alphabet STRNEWLINE " ) ; for ( int i = 0 ; str [ i ] != ' \0' ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) printf ( " % d ▁ " , str [ i ] - ' A ' + 1 ) ; else if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) printf ( " % d ▁ " , str [ i ] - ' a ' + 1 ) ; if ( str [ i ] == ' ▁ ' ) printf ( " % c " , str [ i ] ) ; } printf ( " STRNEWLINE " ) ; } int main ( ) { char str [ ] = " geeksforgeeks " ; cipher ( str ) ; return 0 ; }
Count palindrome words in a sentence | C ++ program to count number of palindrome words in a sentence ; Function to check if a word is palindrome ; Function to count palindrome words ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPalin ( string word ) { int n = word . length ( ) ; transform ( word . begin ( ) , word . end ( ) , word . begin ( ) , :: tolower ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . at ( i ) != word . at ( n - 1 ) ) return false ; return true ; } int countPalin ( string str ) { str = str + " ▁ " ; string word = " " ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . at ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { if ( checkPalin ( word ) ) count ++ ; word = " " ; } } return count ; } int main ( ) { cout << countPalin ( " Madam ▁ Arora ▁ teaches ▁ malayalam " ) << endl ; cout << countPalin ( " Nitin ▁ speaks ▁ malayalam " ) << endl ; }
Remove the forbidden strings | CPP program to remove the forbidden strings ; pre [ ] keeps record of the characters of w that need to be changed ; number of forbidden strings ; given string ; stores the forbidden strings ; letter to replace and occur max number of times ; Function to check if the particula r substring is present in w at position ; If length of substring from this position is greater than length of w then return ; n and n1 are used to check for substring without considering the case of the letters in w by comparing the difference of ASCII values ; If same == true then it means a substring was found starting at position therefore all characters from position to length of substring found need to be changed therefore they needs to be marked ; Function implementing logic ; To verify if any substring is starting from index i ; Modifying the string w according to th rules ; This condition checks if w [ i ] = upper ( letter ) ; This condition checks if w [ i ] is any lowercase letter apart from letter . If true replace it with letter ; This condition checks if w [ i ] is any uppercase letter apart from letter . If true then replace it with upper ( letter ) . ; Driver function for the program ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool pre [ 100 ] ; int n ; string w ; string s [ 110 ] ; char letter ; void verify ( int position , int index ) { int l = w . length ( ) ; int k = s [ index ] . length ( ) ; if ( position + k > l ) return ; bool same = true ; for ( int i = position ; i < position + k ; i ++ ) { int n , n1 ; char ch = w [ i ] ; char ch1 = s [ index ] [ i - position ] ; if ( ch >= ' a ' && ch <= ' z ' ) n = ch - ' a ' ; else n = ch - ' A ' ; if ( ch1 >= ' a ' && ch1 <= ' z ' ) n1 = ch1 - ' a ' ; else n1 = ch1 - ' A ' ; if ( n != n1 ) same = false ; } if ( same == true ) { for ( int i = position ; i < position + k ; i ++ ) pre [ i ] = true ; return ; } } void solve ( ) { int l = w . length ( ) ; int p = letter - ' a ' ; for ( int i = 0 ; i < 100 ; i ++ ) pre [ i ] = false ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) verify ( i , j ) ; } for ( int i = 0 ; i < l ; i ++ ) { if ( pre [ i ] == true ) { if ( w [ i ] == letter ) w [ i ] = ( letter == ' a ' ) ? ' b ' : ' a ' ; else if ( w [ i ] == ' A ' + p ) w [ i ] = ( letter == ' a ' ) ? ' B ' : ' A ' ; else if ( w [ i ] >= ' a ' && w [ i ] <= ' z ' ) w [ i ] = letter ; else if ( w [ i ] >= ' A ' && w [ i ] <= ' Z ' ) w [ i ] = ' A ' + p ; } } cout << w ; } int main ( ) { n = 3 ; s [ 0 ] = " etr " ; s [ 1 ] = " ed " ; s [ 2 ] = " ied " ; w = " PEtrUnited " ; letter = ' d ' ; solve ( ) ; return 0 ; }
Round the given number to nearest multiple of 10 | C ++ program to round the given integer to a whole number which ends with zero . ; function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int round ( int n ) { int a = ( n / 10 ) * 10 ; int b = a + 10 ; return ( n - a > b - n ) ? b : a ; } int main ( ) { int n = 4722 ; cout << round ( n ) << endl ; return 0 ; }
Breaking a number such that first part is integral division of second by a power of 10 | c ++ function to count ways to divide a string in two parts a and b such that b / pow ( 10 , p ) == a ; substring representing int a ; no of digits in a ; consider only most significant l1 characters of remaining string for int b ; if any of a or b contains leading 0 s discard this combination ; if both are equal ; driver function to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( string N ) { int len = N . length ( ) ; int l = ( len ) / 2 ; int count = 0 ; for ( int i = 1 ; i <= l ; i ++ ) { string s = N . substr ( 0 , i ) ; int l1 = s . length ( ) ; string t = N . substr ( i , l1 ) ; if ( s [ 0 ] == '0' t [ 0 ] == '0' ) continue ; if ( s . compare ( t ) == 0 ) count ++ ; } return count ; } int main ( ) { string N = "2202200" ; cout << calculate ( N ) ; return 0 ; }
Number of subsequences as " ab " in a string repeated K times | CPP code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( string s , int K ) { int n = s . length ( ) ; int C , c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' ) c1 ++ ; if ( s [ i ] == ' b ' ) { c2 ++ ; C += c1 ; } } return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ; } int main ( ) { string S = " abcb " ; int k = 2 ; cout << countOccurrences ( S , k ) << endl ; return 0 ; }
Program to check Strength of Password | C ++ program to check if a given password is strong or not . ; Checking lower alphabet in string ; Strength of password ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printStrongNess ( string & input ) { int n = input . length ( ) ; bool hasLower = false , hasUpper = false ; bool hasDigit = false , specialChar = false ; string normalChars = " abcdefghijklmnopqrstu " " vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ▁ " ; for ( int i = 0 ; i < n ; i ++ ) { if ( islower ( input [ i ] ) ) hasLower = true ; if ( isupper ( input [ i ] ) ) hasUpper = true ; if ( isdigit ( input [ i ] ) ) hasDigit = true ; size_t special = input . find_first_not_of ( normalChars ) ; if ( special != string :: npos ) specialChar = true ; } cout << " Strength ▁ of ▁ password : - " ; if ( hasLower && hasUpper && hasDigit && specialChar && ( n >= 8 ) ) cout << " Strong " << endl ; else if ( ( hasLower hasUpper ) && specialChar && ( n >= 6 ) ) cout << " Moderate " << endl ; else cout << " Weak " << endl ; } int main ( ) { string input = " GeeksforGeeks ! @12" ; printStrongNess ( input ) ; return 0 ; }
String with k distinct characters and no same characters adjacent | CPP program to construct a n length string with k distinct characters such that no two same characters are adjacent . ; Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code
#include <iostream> NEW_LINE using namespace std ; string findString ( int n , int k ) { string res = " " ; for ( int i = 0 ; i < k ; i ++ ) res = res + ( char ) ( ' a ' + i ) ; int count = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { res = res + ( char ) ( ' a ' + count ) ; count ++ ; if ( count == k ) count = 0 ; } return res ; } int main ( ) { int n = 5 , k = 2 ; cout << findString ( n , k ) ; return 0 ; }