codeparrot/xlcost-text-to-code huggingface dataset:
Modify array by removing characters from their Hexadecimal representations which are present in a given string | C ++ program for the above approach ; Function to convert a decimal number to its equivalent hexadecimal number ; Function to convert hexadecimal number to its equavalent decimal number ; Stores characters with their respective hexadecimal values ; Stores answer ; Traverse the string ; If digit ; If character ; Return the answer ; Function to move all the alphabets to front ; Function to modify each array element by removing characters from their hexadecimal representation which are present in a given string ; Traverse the array ; Stores hexadecimal value ; Remove the characters from hexadecimal representation present in string S ; Stores decimal value ; Replace array element ; Print the modified array ; Driven Program ; Given array ; Given string ; Function call to modify array by given operations
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decHex ( int n ) { char alpha [ ] = { ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ; string ans ; while ( n > 0 ) { if ( n % 16 < 10 ) { ans += to_string ( n % 16 ) ; } else { ans += alpha [ n % 16 - 10 ] ; } n /= 16 ; } reverse ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } int hexDec ( string convertedHex ) { char mp [ ] = { 10 , 11 , 12 , 13 , 14 , 15 } ; int ans = 0 ; int pos = 0 ; reverse ( convertedHex . begin ( ) , convertedHex . end ( ) ) ; for ( char ch : convertedHex ) { if ( isdigit ( ch ) ) { ans += ( ( int ) pow ( 16 , pos ) ) * ( ch - '0' ) ; } else { ans += ( ( int ) pow ( 16 , pos ) ) * mp [ ch - ' A ' ] ; } pos += 1 ; } return ans ; } string removeChars ( string hexaVal , string S ) { set < char > setk ; for ( char ch : S ) { setk . insert ( ch ) ; } string ans = " " ; for ( char ch : hexaVal ) { if ( setk . find ( ch ) != setk . end ( ) ) { continue ; } ans += ch ; } return ans ; } void convertArr ( int arr [ ] , int N , string S ) { for ( int i = 0 ; i < N ; i ++ ) { string hexaVal = decHex ( arr [ i ] ) ; string convertedHex = removeChars ( hexaVal , S ) ; int decVal = hexDec ( convertedHex ) ; arr [ i ] = decVal ; } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 74 , 91 , 31 , 122 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string S = "1AB " ; convertArr ( arr , N , S ) ; return 0 ; }
Minimum number of chairs required to ensure that every worker is seated at any instant | C ++ implementation of the above approach ; Function to find the minimum number of chairs required to ensure that every worker is seated at any time ; Stores the number of chairs required ; Pointer to iterate ; Stores minimum number of chairs required ; Iterate over every character ; If character is ' E ' ; Increase the count ; Otherwise ; Update maximum value of count obtained at any given time ; Return mini ; Driver Code ; Given String ; Function call to find the minimum number of chairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimumChairs ( string s ) { int count = 0 ; int i = 0 ; int mini = INT_MIN ; while ( i < s . length ( ) ) { if ( s [ i ] == ' E ' ) count ++ ; else count -- ; mini = max ( count , mini ) ; i ++ ; } return mini ; } int main ( ) { string s = " EELEE " ; cout << findMinimumChairs ( s ) ; }
Modify string by inserting characters such that every K | C ++ program for the above approach ; Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fillString ( string s , int k ) { unordered_map < int , char > mp ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp [ i % k ] = s [ i ] ; } } for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( mp . find ( i % k ) == mp . end ( ) ) { cout << -1 ; return ; } s [ i ] = mp [ i % k ] ; } cout << s ; } int main ( ) { string S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; return 0 ; }
Rearrange a string S1 such that another given string S2 is not its subsequence | C ++ program for the above approach ; Function to rearrange characters in string S1 such that S2 is not a subsequence of it ; Store the frequencies of characters of string s2 ; Traverse the string s2 ; Update the frequency ; Find the number of unique characters in s2 ; Increment unique by 1 if the condition satisfies ; Check if the number of unique characters in string s2 is 1 ; Store the unique character frequency ; Store occurence of it in s1 ; Find count of that character in the string s1 ; Increment count by 1 if that unique character is same as current character ; If count count_in_s1 is less than count_in_s2 , then print s1 and return ; Otherwise , there is no possible arrangement ; Checks if any character in s2 is less than its next character ; Iterate the string , s2 ; If s [ i ] is greater than the s [ i + 1 ] ; Set inc to 0 ; If inc = 1 , print s1 in decreasing order ; Otherwise , print s1 in increasing order ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrangeString ( string s1 , string s2 ) { int cnt [ 26 ] = { 0 } ; for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) cnt [ s2 [ i ] - ' a ' ] ++ ; int unique = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( cnt [ i ] != 0 ) unique ++ ; if ( unique == 1 ) { int count_in_s2 = s2 . size ( ) ; int count_in_s1 = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) if ( s1 [ i ] == s2 [ 0 ] ) count_in_s1 ++ ; if ( count_in_s1 < count_in_s2 ) { cout << s1 ; return ; } cout << -1 ; } else { int inc = 1 ; for ( int i = 0 ; i < s2 . size ( ) - 1 ; i ++ ) if ( s2 [ i ] > s2 [ i + 1 ] ) inc = 0 ; if ( inc == 1 ) { sort ( s1 . begin ( ) , s1 . end ( ) , greater < char > ( ) ) ; cout << s1 ; } else { sort ( s1 . begin ( ) , s1 . end ( ) ) ; cout << s1 ; } } } int main ( ) { string s1 = " abcd " , s2 = " ab " ; rearrangeString ( s1 , s2 ) ; return 0 ; }
Check if a string can be emptied by removing all subsequences of the form "10" | C ++ program for the above approach ; Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isReducible ( string str ) { int N = str . size ( ) ; stack < char > s ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '1' ) s . push ( str [ i ] ) ; else if ( ! s . empty ( ) ) s . pop ( ) ; else return false ; } return s . empty ( ) ; } int main ( ) { string str = "11011000" ; if ( isReducible ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimize flips required such that string does not any pair of consecutive 0 s | C ++ program for the above approach ; Function to find minimum flips required such that a string does not contain any pair of consecutive 0 s ; Stores minimum count of flips ; Iterate over the characters of the string ; If two consecutive characters are equal to '0' ; Update S [ i + 1 ] ; Update cntOp ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool cntMinOperation ( string S , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] == '0' && S [ i + 1 ] == '0' ) { S [ i + 1 ] = '1' ; cntOp += 1 ; } } return cntOp ; } int main ( ) { string S = "10001" ; int N = S . length ( ) ; cout << cntMinOperation ( S , N ) ; return 0 ; }
Rearrange a string to maximize the minimum distance between any pair of vowels | C ++ program for the above approach ; Function to rearrange the string such that the minimum distance between any of vowels is maximum . ; Store vowels and consonants ; Iterate over the characters of string ; If current character is a vowel ; If current character is a consonant ; Stores count of vowels and consonants respectively ; Stores the resultant string ; Stores count of consonants appended into ans ; Append vowel to ans ; Append consonants ; Append consonant to ans ; Update temp ; Remove the taken elements of consonant ; Return final ans ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solution ( string s ) { vector < char > vowel , consonant ; for ( auto i : s ) { if ( i == ' a ' i == ' e ' i == ' i ' i == ' o ' i == ' u ' ) { vowel . push_back ( i ) ; } else { consonant . push_back ( i ) ; } } int Nc , Nv ; Nv = vowel . size ( ) ; Nc = consonant . size ( ) ; int M = Nc / ( Nv - 1 ) ; string ans = " " ; int consotnant_till = 0 ; for ( auto i : vowel ) { ans += i ; int temp = 0 ; for ( int j = consotnant_till ; j < min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant [ j ] ; temp ++ ; } consotnant_till += temp ; } return ans ; } int main ( ) { string str = " aaaabbbcc " ; cout << solution ( str ) ; return 0 ; }
Lexicographically smallest string possible by performing K operations on a given string | C ++ program to implement the above approach ; Function to find the lexicographically smallest possible string by performing K operations on string S ; Store the size of string , s ; Check if k >= n , if true , convert every character to ' a ' ; Iterate in range [ 0 , n - 1 ] using i ; When k reaches 0 , break the loop ; If current character is ' a ' , continue ; Otherwise , iterate in the range [ i + 1 , n - 1 ] using j ; Check if s [ j ] > s [ i ] ; If true , set s [ j ] = s [ i ] and break out of the loop ; Check if j reaches the last index ; Update S [ i ] ; Decrement k by 1 ; Print string ; Driver Code ; Given String , s ; Given k ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestlexicographicstring ( string s , int k ) { int n = s . size ( ) ; if ( k >= n ) { for ( int i = 0 ; i < n ; i ++ ) { s [ i ] = ' a ' ; } cout << s ; return ; } for ( int i = 0 ; i < n ; i ++ ) { if ( k == 0 ) { break ; } if ( s [ i ] == ' a ' ) continue ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( s [ j ] > s [ i ] ) { s [ j ] = s [ i ] ; break ; } else if ( j == n - 1 ) s [ j ] = s [ i ] ; } s [ i ] = ' a ' ; k -- ; } cout << s ; } int main ( ) { string s = " geeksforgeeks " ; int k = 6 ; smallestlexicographicstring ( s , k ) ; return 0 ; }
Minimize removal of non | C ++ program for the above approach ; Function to find minimum count of steps required ot make string S an empty string ; Stores count of occurences ' ( ' ; Stores count of occurences ' ) ' ; Traverse the string , str ; If current character is ' ( ' ; Update count_1 ; Update count_2 ; If all the characters are same , then print - 1 ; If the count of occurence of ' ) ' and ' ( ' are same then print 0 ; If length of string is Odd ; Driver Code ; Given string ; Size of the string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canReduceString ( string S , int N ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ( ' ) { count_1 ++ ; } else { count_2 ++ ; } } if ( count_1 == 0 count_2 == 0 ) { cout << " - 1" << endl ; } else if ( count_1 == count_2 ) { cout << "0" << endl ; } else if ( N % 2 != 0 ) { cout << " - 1" ; } else { cout << abs ( count_1 - count_2 ) / 2 ; } } int main ( ) { string S = " ) ) ) ( ( ( " ; int N = S . length ( ) ; canReduceString ( S , N ) ; return 0 ; }
Program to construct a DFA which accepts the language L = { aN | N â ‰¥ 1 } | C ++ program for the above approach ; Function to check whether the string S satisfy the given DFA or not ; Stores the count of characters ; Iterate over the range [ 0 , N ] ; Count and check every element for ' a ' ; If string matches with DFA ; If not matches ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isAcceptedDFA ( string s , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' a ' ) count ++ ; } if ( count == N && count != 0 ) { cout << " Accepted " ; } else { cout << " Not ▁ Accepted " ; } } int main ( ) { string S = " aaaaa " ; isAcceptedDFA ( S , S . size ( ) ) ; return 0 ; }
Maximize palindromic strings of length 3 possible from given count of alphabets | C ++ program for the above approach ; Function to count maximum number of palindromic string of length 3 ; Stores the final count of palindromic strings ; Traverse the array ; Increment res by arr [ i ] / 3 , i . e forming string of only i + ' a ' character ; Store remainder ; Increment c1 by one , if current frequency is 1 ; Increment c2 by one , if current frequency is 2 ; Count palindromic strings of length 3 having the character at the ends different from that present in the middle ; Update c1 and c2 ; Increment res by 2 * c2 / 3 ; Finally print the result ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximum_pallindromic ( int arr [ ] ) { int res = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { res += arr [ i ] / 3 ; arr [ i ] = arr [ i ] % 3 ; if ( arr [ i ] == 1 ) c1 ++ ; else if ( arr [ i ] == 2 ) c2 ++ ; } res += min ( c1 , c2 ) ; int t = min ( c1 , c2 ) ; c1 -= t ; c2 -= t ; res += 2 * ( c2 / 3 ) ; c2 %= 3 ; res += c2 / 2 ; cout << res ; } int main ( ) { int arr [ ] = { 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; maximum_pallindromic ( arr ) ; return 0 ; }
Find the winner of game of repeatedly removing the first character to empty given string | C ++ program to implement the above approach ; Function to find the winner of a game of repeatedly removing the first character to empty a string ; Store characters of each string of the array arr [ ] ; Stores count of strings in arr [ ] ; Traverse the array arr [ ] ; Stores length of current string ; Traverse the string ; Insert arr [ i ] [ j ] ; 1 st Player starts the game ; Stores the player number for the next turn ; Remove 1 st character of current string ; Update player number for the next turn ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_Winner ( vector < string > & arr , int N ) { vector < queue < char > > Q ( N ) ; int M = arr . size ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int len = arr [ i ] . length ( ) ; for ( int j = 0 ; j < len ; j ++ ) { Q [ i ] . push ( arr [ i ] [ j ] - 1 ) ; } } int player = 0 ; while ( Q [ player ] . size ( ) > 0 ) { int nextPlayer = Q [ player ] . front ( ) - '0' ; Q [ player ] . pop ( ) ; player = nextPlayer ; } cout << " Player ▁ " << ( player + 1 ) ; } int main ( ) { int N = 3 ; vector < string > arr = { "323" , "2" , "2" } ; find_Winner ( arr , N ) ; return 0 ; }
Longest Substring that can be made a palindrome by swapping of characters | C ++ program for the above approach ; Function to find the Longest substring that can be made a palindrome by swapping of characters ; Initialize dp array of size 1024 ; Initializeing dp array with length of s ; Initializing mask and res ; Traverse the string ; Find the mask of the current character ; Finding the length of the longest substring in s which is a palindrome for even count ; Finding the length of the longest substring in s which is a palindrome for one odd count ; Finding maximum length of substring having one odd count ; dp [ mask ] is minimum of current i and dp [ mask ] ; Return longest length of the substring which forms a palindrome with swaps ; Driver Code ; Input String ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubstring ( string s ) { int dp [ 1024 ] ; fill ( dp , dp + 1024 , s . size ( ) ) ; int res = 0 , mask = 0 ; dp [ 0 ] = -1 ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { mask ^= 1 << ( s [ i ] - 48 ) ; res = max ( res , i - dp [ mask ] ) ; for ( int j = 0 ; j <= 9 ; ++ j ) res = max ( res , i - dp [ mask ^ ( 1 << j ) ] ) ; dp [ mask ] = min ( dp [ mask ] , i ) ; } return res ; } int main ( ) { string s = "3242415" ; cout << longestSubstring ( s ) ; return 0 ; }
Convert given string to a valid mobile number | C ++ program for the above approach ; Function to print the valid and formatted phone number ; Length of given string ; Store digits in temp ; Iterate given M ; If any digit , append it to temp ; Find new length of string ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Validate ( string M ) { int len = M . size ( ) ; string temp = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( isdigit ( M [ i ] ) ) temp += M [ i ] ; } int nwlen = temp . size ( ) ; if ( nwlen != 10 ) { cout << " Invalid STRNEWLINE " ; return ; } string res = " " ; string x = temp . substr ( 0 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . substr ( 3 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . substr ( 6 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . substr ( 9 , 1 ) ; res += " ( " + x + " ) " ; cout << res << " STRNEWLINE " ; } int main ( ) { string M = "91 ▁ 234rt5%34*0 ▁ 3" ; Validate ( M ) ; }
Modulus of two Hexadecimal Numbers | C ++ program to implement the above approach ; Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void hexaModK ( string s , string k ) { map < char , int > mp ; for ( char i = 1 ; i <= 9 ; i ++ ) { mp [ i + '0' ] = i ; } mp [ ' A ' ] = 10 ; mp [ ' B ' ] = 11 ; mp [ ' C ' ] = 12 ; mp [ ' D ' ] = 13 ; mp [ ' E ' ] = 14 ; mp [ ' F ' ] = 15 ; long m = stoi ( k , 0 , 16 ) ; long base = 1 ; long ans = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { long n = mp [ s [ i ] ] % m ; ans = ( ans + ( base % m * n % m ) % m ) % m ; base = ( base % m * 16 % m ) % m ; } stringstream ss ; ss << hex << ans ; string su = ss . str ( ) ; transform ( su . begin ( ) , su . end ( ) , su . begin ( ) , :: toupper ) ; cout << ( su ) ; } int main ( ) { string n = "3E8" ; string k = "13" ; hexaModK ( n , k ) ; return 0 ; }
Print all combinations generated by characters of a numeric string which does not exceed N | C ++ program for the above approach ; Store the current sequence of s ; Store the all the required sequences ; Function to print all sequences of S satisfying the required condition ; Print all strings in the set ; Function to generate all sequences of string S that are at most N ; Iterate over string s ; Push ith character to combination ; Convert the string to number ; Check if the condition is true ; Push the current string to the final set of sequences ; Recursively call function ; Backtrack to its previous state ; Driver Code ; Function Call ; Print required sequences
#include <bits/stdc++.h> NEW_LINE using namespace std ; string combination ; set < string > combinations ; void printSequences ( set < string > combinations ) { for ( string s : combinations ) { cout << s << ' ▁ ' ; } } void generateCombinations ( string & s , int n ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { combination . push_back ( s [ i ] ) ; long x = stol ( combination ) ; if ( x <= n ) { combinations . insert ( combination ) ; generateCombinations ( s , n ) ; } combination . pop_back ( ) ; } } int main ( ) { string S = "124" ; int N = 100 ; generateCombinations ( S , N ) ; printSequences ( combinations ) ; return 0 ; }
Count Distinct Strings present in an array using Polynomial rolling hash function | C ++ program to implement the above approach ; Function to find the hash value of a string ; Traverse the string ; Update hash_val ; Update mul ; Return hash_val of str ; Function to find the count of distinct strings present in the given array ; Store the hash values of the strings ; Traverse the array ; Stores hash value of arr [ i ] ; Sort hash [ ] array ; Stores count of distinct strings in the array ; Traverse hash [ ] array ; Update cntElem ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long compute_hash ( string str ) { int p = 31 ; int MOD = 1e9 + 7 ; long long hash_val = 0 ; long long mul = 1 ; for ( char ch : str ) { hash_val = ( hash_val + ( ch - ' a ' + 1 ) * mul ) % MOD ; mul = ( mul * p ) % MOD ; } return hash_val ; } int distinct_str ( vector < string > & arr , int n ) { vector < long long > hash ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { hash [ i ] = compute_hash ( arr [ i ] ) ; } sort ( hash . begin ( ) , hash . end ( ) ) ; int cntElem = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( hash [ i ] != hash [ i - 1 ] ) { cntElem ++ ; } } return cntElem ; } int main ( ) { vector < string > arr = { " abcde " , " abcce " , " abcdf " , abcde " } ; int N = arr . size ( ) ; cout << distinct_str ( arr , N ) << endl ; return 0 ; }
Remove characters from given string whose frequencies are a Prime Number | C ++ program for the above approach ; Function to perform the seive of eratosthenes algorithm ; Initialize all entries in prime [ ] as true ; Initialize 0 and 1 as non prime ; Traversing the prime array ; If i is prime ; All multiples of i must be marked false as they are non prime ; Function to remove characters which have prime frequency in the string ; Length of the string ; Create a boolean array prime ; Sieve of Eratosthenes ; Stores the frequency of character ; Storing the frequencies ; New string that will be formed ; Removing the characters which have prime frequencies ; If the character has prime frequency then skip ; Else concatenate the character to the new string ; Print the modified string ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool * prime , int n ) { for ( int i = 0 ; i <= n ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = 2 ; i * j <= n ; j ++ ) { prime [ i * j ] = false ; } } } } void removePrimeFrequencies ( string s ) { int n = s . length ( ) ; bool prime [ n + 1 ] ; SieveOfEratosthenes ( prime , n ) ; unordered_map < char , int > m ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { m [ s [ i ] ] ++ ; } string new_string = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( prime [ m [ s [ i ] ] ] ) continue ; new_string += s [ i ] ; } cout << new_string ; } int main ( ) { string str = " geeksforgeeks " ; removePrimeFrequencies ( str ) ; return 0 ; }
Rearrange string such that no pair of adjacent characters are of the same type | C ++ program to implement the above approach ; Function to rearrange given alphanumeric string such that no two adjacent characters are of the same type ; Stores alphabets and digits ; Store the alphabets and digits separately in the strings ; Stores the count of alphabets and digits ; If respective counts differ by 1 ; Desired arrangement not possible ; Stores the indexes ; Check if first character should be alphabet or digit ; Place alphabets and digits alternatively ; If current character needs to be alphabet ; If current character needs to be a digit ; Flip flag for alternate arrangement ; Return resultant string ; Driver Code ; Given String ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string rearrange ( string s ) { string s1 = " " , s2 = " " ; for ( char x : s ) { isalpha ( x ) ? s1 . push_back ( x ) : s2 . push_back ( x ) ; } int n = s1 . size ( ) ; int m = s2 . size ( ) ; if ( abs ( n - m ) > 1 ) return " - 1" ; int i = 0 , j = 0 , k = 0 ; int flag = ( n >= m ) ? 1 : 0 ; while ( i < n and j < m ) { if ( flag ) s [ k ++ ] = s1 [ i ++ ] ; else s [ k ++ ] = s2 [ j ++ ] ; flag = ! flag ; } return s ; } int main ( ) { string str = " geeks2020" ; cout << rearrange ( str ) << endl ; return 0 ; }