codeparrot/xlcost-text-to-code huggingface dataset:
Minimum deletions from string to reduce it to string with at most 2 unique characters | C ++ implementation of the above approach ; Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string s ) { int i , j ; int fr [ 26 ] = { 0 } ; int n = s . size ( ) ; for ( i = 0 ; i < n ; i ++ ) { char x = s [ i ] ; fr [ x - ' a ' ] += 1 ; } int minimum = INT_MAX ; for ( i = 0 ; i < 26 ; i ++ ) { for ( j = i + 1 ; j < 26 ; j ++ ) { int z = fr [ i ] + fr [ j ] ; minimum = min ( minimum , n - z ) ; } } return minimum ; } int main ( ) { string s = " geeksforgeeks " ; cout << check ( s ) ; }
Count and Print the alphabets having ASCII value not in the range [ l , r ] | C ++ implementation of the above approach ; Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountCharacters ( string str , int l , int r ) { int cnt = 0 ; unordered_map < char , int > m ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str [ i ] and str [ i ] <= r ) ) { cnt ++ ; if ( m [ str [ i ] ] != 1 ) { cout << str [ i ] << " ▁ " ; m [ str [ i ] ] ++ ; } } } return cnt ; } int main ( ) { string str = " geeksforgeeks " ; int l = 102 , r = 111 ; cout << " Characters ▁ with ▁ ASCII ▁ values " " ▁ not ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ STRNEWLINE in ▁ the ▁ given ▁ string ▁ are : ▁ " ; cout << " and their count is " << CountCharacters ( str , l , r ) ; return 0 ; }
Final state of the string after modification | C ++ implementation of above approach ; Function to return final positions of the boxes ; Populate forces going from left to right ; Populate forces going from right to left ; return final state of boxes ; Driver code ; Function call to print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; string pushBoxes ( string S ) { int N = S . length ( ) ; vector < int > force ( N , 0 ) ; int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' R ' ) { f = N ; } else if ( S [ i ] == ' L ' ) { f = 0 ; } else { f = max ( f - 1 , 0 ) ; } force [ i ] += f ; } f = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' L ' ) { f = N ; } else if ( S [ i ] == ' R ' ) { f = 0 ; } else { f = max ( f - 1 , 0 ) ; } force [ i ] -= f ; } string ans ; for ( int f : force ) { ans += f == 0 ? ' . ' : f > 0 ? ' R ' : ' L ' ; } return ans ; } int main ( ) { string S = " . L . R . . . LR . . L . . " ; cout << pushBoxes ( S ) ; }
Count the number of words having sum of ASCII values less than and greater than k | C ++ implementation of the above approach ; Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CountWords ( string str , int k ) { int sum = 0 ; int NumberOfWords = 0 ; int counter = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str [ i ] == ' ▁ ' ) { if ( sum < k ) counter ++ ; sum = 0 ; NumberOfWords ++ ; } else sum += str [ i ] ; } NumberOfWords ++ ; if ( sum < k ) counter ++ ; cout << " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " " ▁ values ▁ less ▁ than ▁ k ▁ = ▁ " << counter << endl ; cout << " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII ▁ values " " ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = ▁ " << NumberOfWords - counter ; } int main ( ) { string str = " Learn ▁ how ▁ to ▁ code " ; int k = 400 ; CountWords ( str , k ) ; return 0 ; }
Minimum number of given operations required to make two strings equal | C ++ implementation of the above approach ; Function to return the minimum number of operations to convert string A to B ; If both the strings are equal then no operation needs to be performed ; store the position of ' _ ' ; to store the generated string at every move and the position of ' _ ' within it ; vis will store the minimum operations to reach that particular string ; minimum moves to reach the string ss ; if ' _ ' can be swapped with the character on it 's left ; swap with the left character ; if the string is generated for the first time ; if generated string is the required string ; update the distance for the currently generated string ; restore the string before it was swapped to check other cases ; swap ' _ ' with the character on it 's right this time ; if ' _ ' can be swapped with the character ' i + 2' ; if ' _ ' can be swapped with the character at ' i + 2' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string s , string f ) { if ( s == f ) return 0 ; unordered_map < string , int > vis ; int n ; n = s . length ( ) ; int pos = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == ' _ ' ) { pos = i ; break ; } } queue < pair < string , int > > q ; q . push ( { s , pos } ) ; vis [ s ] = 0 ; while ( ! q . empty ( ) ) { string ss = q . front ( ) . first ; int pp = q . front ( ) . second ; int dist = vis [ ss ] ; q . pop ( ) ; if ( pp > 0 ) { swap ( ss [ pp ] , ss [ pp - 1 ] ) ; if ( ! vis . count ( ss ) ) { if ( ss == f ) { return dist + 1 ; break ; } vis [ ss ] = dist + 1 ; q . push ( { ss , pp - 1 } ) ; } swap ( ss [ pp ] , ss [ pp - 1 ] ) ; } if ( pp < n - 1 ) { swap ( ss [ pp ] , ss [ pp + 1 ] ) ; if ( ! vis . count ( ss ) ) { if ( ss == f ) { return dist + 1 ; break ; } vis [ ss ] = dist + 1 ; q . push ( { ss , pp + 1 } ) ; } swap ( ss [ pp ] , ss [ pp + 1 ] ) ; } if ( pp > 1 && ss [ pp - 1 ] != ss [ pp - 2 ] ) { swap ( ss [ pp ] , ss [ pp - 2 ] ) ; if ( ! vis . count ( ss ) ) { if ( ss == f ) { return dist + 1 ; break ; } vis [ ss ] = dist + 1 ; q . push ( { ss , pp - 2 } ) ; } swap ( ss [ pp ] , ss [ pp - 2 ] ) ; } if ( pp < n - 2 && ss [ pp + 1 ] != ss [ pp + 2 ] ) { swap ( ss [ pp ] , ss [ pp + 2 ] ) ; if ( ! vis . count ( ss ) ) { if ( ss == f ) { return dist + 1 ; break ; } vis [ ss ] = dist + 1 ; q . push ( { ss , pp + 2 } ) ; } swap ( ss [ pp ] , ss [ pp + 2 ] ) ; } } } int main ( ) { string A = " aba _ a " ; string B = " _ baaa " ; cout << minOperations ( A , B ) ; return 0 ; }
Rearrange a string in the form of integer sum followed by the minimized character | C ++ implementation of the above approach ; function to return maximum volume ; separate digits and alphabets ; change digit sum to string ; change alphabet sum to string ; concatenate sum to alphabets string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string separateChar ( string str ) { int n = str . size ( ) , digitSum = 0 ; int alphabetSum = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isdigit ( str [ i ] ) ) digitSum += str [ i ] - '0' ; else { alphabetSum += str [ i ] - ' a ' + 1 ; alphabetSum %= 26 ; } } string sumStr = to_string ( digitSum ) ; char alphabetStr = char ( alphabetSum + ' a ' - 1 ) ; sumStr += alphabetStr ; return sumStr ; } int main ( ) { string str = "3652adyz3423" ; cout << separateChar ( str ) ; return 0 ; }
Find the count of palindromic sub | CPP program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; int countPalindrome ( string str ) { int n = str . size ( ) ; int sum = 0 ; int hashTable [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) hashTable [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] ) sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } return sum ; } int main ( ) { string str = " ananananddd " ; cout << countPalindrome ( str ) ; return 0 ; }
Minimum number of elements to be removed so that pairwise consecutive elements are same | C ++ implementation of the above approach ; Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countConsecutive ( string s ) { int count = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) i ++ ; else count ++ ; } return count ; } int main ( ) { string str = "44522255" ; cout << countConsecutive ( str ) ; return 0 ; }
Smallest odd digits number not less than N | CPP program to print the smallest integer not less than N with all odd digits ; function to check if all digits are odd of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 == 0 ) if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check_digits ( int n ) { while ( n ) { return 0 ; n /= 10 ; } return 1 ; } int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) ) return i ; } int main ( ) { int N = 2397 ; cout << smallest_number ( N ) ; return 0 ; }
Smallest odd digits number not less than N | CPP program to print the smallest integer not less than N with all odd digits ; function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNumber ( int n ) { int num = 0 ; string s = " " ; int duplicate = n ; while ( n ) { s = char ( n % 10 + 48 ) + s ; n /= 10 ; } int index = -1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s [ i ] - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == -1 ) return duplicate ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] - '0' ) ; num = num * 10 + ( s [ index ] - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 1 ; return num ; } int main ( ) { int N = 2397 ; cout << smallestNumber ( N ) ; return 0 ; }
Count and Print the alphabets having ASCII value in the range [ l , r ] | C ++ implementation of the above approach ; Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountCharacters ( string str , int l , int r ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( l <= str [ i ] and str [ i ] <= r ) { cnt ++ ; cout << str [ i ] << " ▁ " ; } } return cnt ; } int main ( ) { string str = " geeksforgeeks " ; int l = 102 , r = 111 ; cout << " Characters ▁ with ▁ ASCII ▁ values " " ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ are ▁ STRNEWLINE " ; cout << " and their count is " << CountCharacters ( str , l , r ) ; return 0 ; }
Minimum steps to remove substring 010 from a binary string | CPP program to calculate steps to remove substring 010 from a binary string ; Function to find the minimum steps ; substring "010" found ; Driver code ; Get the binary string ; Find the minimum steps
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) - 2 ; i ++ ) { if ( str [ i ] == '0' ) { if ( str [ i + 1 ] == '1' ) { if ( str [ i + 2 ] == '0' ) { count ++ ; i += 2 ; } } } } return count ; } int main ( ) { string str = "0101010" ; cout << minSteps ( str ) ; return 0 ; }
XOR of Prime Frequencies of Characters in a String | C ++ program to find XOR of Prime Frequencies of Characters in a String ; Function to create Sieve to check primes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } int xorOfPrime ( string s ) { bool prime [ 100005 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , 10005 ) ; int i , j ; map < char , int > m ; for ( i = 0 ; i < s . length ( ) ; i ++ ) m [ s [ i ] ] ++ ; int result = 0 ; int flag = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) { if ( prime [ it -> second ] ) { result ^= it -> second ; flag = 1 ; } } if ( ! flag ) return -1 ; return result ; } int main ( ) { string s = " gggggeeekkkks " ; cout << xorOfPrime ( s ) ; return 0 ; }
Count of alphabets having ASCII value less than and greater than k | C ++ implementation of the above approach ; Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountCharacters ( string str , int k ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] < k ) cnt ++ ; } return cnt ; } int main ( ) { string str = " GeeksForGeeks " ; int k = 90 ; int count = CountCharacters ( str , k ) ; cout << " Characters ▁ with ▁ ASCII ▁ values " " ▁ less ▁ than ▁ K ▁ are ▁ " << count ; cout << " Characters with ASCII values " STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " greater than or equal to K are " << str . length ( ) - count ; return 0 ; }
De Bruijn sequence | Set 1 | C ++ implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < string > seen ; vector < int > edges ; void dfs ( string node , int & k , string & A ) { for ( int i = 0 ; i < k ; ++ i ) { string str = node + A [ i ] ; if ( seen . find ( str ) == seen . end ( ) ) { seen . insert ( str ) ; dfs ( str . substr ( 1 ) , k , A ) ; edges . push_back ( i ) ; } } } string deBruijn ( int n , int k , string A ) { seen . clear ( ) ; edges . clear ( ) ; string startingNode = string ( n - 1 , A [ 0 ] ) ; dfs ( startingNode , k , A ) ; string S ; int l = pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A [ edges [ i ] ] ; S += startingNode ; return S ; } int main ( ) { int n = 3 , k = 2 ; string A = "01" ; cout << deBruijn ( n , k , A ) ; return 0 ; }
Shortest distance to every other character from given character | C ++ implementation of above approach ; Function to return required vector of distances ; list to hold position of c in s ; list to hold the result ; length of string ; Iterate over string to create list ; max value of p2 ; Initialize the pointers ; Create result array ; Values at current pointers ; Current Index is before than p1 ; Current Index is between p1 and p2 ; Current Index is nearer to p1 ; Current Index is nearer to p2 ; Move pointer 1 step ahead ; Current index is after p2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > shortestToChar ( string s , char c ) { vector < int > list ; vector < int > res ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == c ) { list . push_back ( i ) ; } } int p1 , p2 , v1 , v2 ; int l = list . size ( ) - 1 ; p1 = 0 ; p2 = l > 0 ? 1 : 0 ; for ( int i = 0 ; i < len ; i ++ ) { v1 = list [ p1 ] ; v2 = list [ p2 ] ; if ( i <= v1 ) { res . push_back ( v1 - i ) ; } else if ( i <= v2 ) { if ( i - v1 < v2 - i ) { res . push_back ( i - v1 ) ; } else { res . push_back ( v2 - i ) ; p1 = p2 ; p2 = p2 < l ? ( p2 + 1 ) : p2 ; } } else { res . push_back ( i - v2 ) ; } } return res ; } int main ( ) { string s = " geeksforgeeks " ; char c = ' e ' ; vector < int > res = shortestToChar ( s , c ) ; for ( auto i : res ) cout << i << " ▁ " ; return 0 ; }
Number of words in a camelcase sequence | CPP code to find the count of words in a CamelCase sequence ; Function to find the count of words in a CamelCase sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWords ( string str ) { int count = 1 ; for ( int i = 1 ; i < str . length ( ) - 1 ; i ++ ) { if ( isupper ( str [ i ] ) ) count ++ ; } return count ; } int main ( ) { string str = " geeksForGeeks " ; cout << countWords ( str ) ; return 0 ; }
Check whether frequency of characters in a string makes Fibonacci Sequence | C ++ program to check whether frequency of characters in a string makes Fibonacci Sequence ; Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; Vector to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isFibonacci ( string s ) { map < char , int > m ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { m [ s [ i ] ] ++ ; } vector < int > v ; int n = m . size ( ) ; int a = 1 , b = 1 ; int c ; v . push_back ( a ) ; v . push_back ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { v . push_back ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; int i = 0 ; for ( auto itr = m . begin ( ) ; itr != m . end ( ) ; itr ++ ) { if ( itr -> second != v [ i ] ) { flag = 0 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } int main ( ) { string s = " abeeedd " ; cout << isFibonacci ( s ) ; return 0 ; }
Lexicographically smallest string formed by removing at most one character | C ++ program to find the lexicographically smallest string by removing at most one character ; Function to return the smallest string ; iterate the string ; first point where s [ i ] > s [ i + 1 ] ; append the string without i - th character in it ; leave the last character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string smallest ( string s ) { int l = s . length ( ) ; string ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) ans += s [ j ] ; } return ans ; } } ans = s . substr ( 0. , l - 1 ) ; return ans ; } int main ( ) { string s = " abcda " ; cout << smallest ( s ) ; return 0 ; }
Arrangement of the characters of a word such that all vowels are at odd places | C ++ program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } int countPermutations ( string str ) { int even = floor ( str . length ( ) / 2 ) ; int odd = str . length ( ) - even ; int ways = 0 ; int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { ++ freq [ str [ i ] - ' a ' ] ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . length ( ) - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } int main ( ) { string str = " geeks " ; cout << countPermutations ( str ) ; return 0 ; }
Replace all consonants with nearest vowels in a string | C ++ program to replace all consonants with nearest vowels in a string ; Function to replace consonant with nearest vowels ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string replacingConsonants ( string s ) { char nVowel [ ] = " aaaeeeeiiiiioooooouuuuuuuu " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) s [ i ] = nVowel [ s [ i ] - ' a ' ] ; return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << replacingConsonants ( s ) ; return 0 ; }
Replace consonants with next immediate consonants alphabetically in a String | C ++ program of above approach ; Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) return false ; return true ; } string replaceConsonants ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' z ' ) s [ i ] = ' b ' ; else { s [ i ] = ( char ) ( s [ i ] + 1 ) ; if ( isVowel ( s [ i ] ) ) s [ i ] = ( char ) ( s [ i ] + 1 ) ; } } } return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << replaceConsonants ( s ) ; return 0 ; }
Remove characters from string that appears strictly less than K times | C ++ program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; string removeChars ( string str , int k ) { int hash [ MAX_CHAR ] = { 0 } ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; ++ i ) hash [ str [ i ] - ' a ' ] ++ ; string res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str [ i ] - ' a ' ] >= k ) { res += str [ i ] ; } } return res ; } int main ( ) { string str = " geeksforgeeks " ; int k = 2 ; cout << removeChars ( str , k ) ; return 0 ; }
Count changes in Led Lights to display digits one by one | CPP program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOnOff ( string n ) { int Led [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 } ; int len = n . length ( ) ; int sum = Led [ n [ 0 ] - '0' ] ; for ( int i = 1 ; i < len ; i ++ ) { sum = sum + abs ( Led [ n [ i ] - '0' ] - Led [ n [ i - 1 ] - '0' ] ) ; } return sum ; } int main ( ) { string n = "082" ; cout << countOnOff ( n ) ; return 0 ; }