codeparrot/xlcost-text-to-code huggingface dataset:
Check if given string is a substring of string formed by repeated concatenation of z to a | C ++ program for the above approach ; Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Driver Code ; Given string ; Function Call
#include <iostream> NEW_LINE using namespace std ; void checkInfinite ( string s ) { bool flag = 1 ; int N = s . length ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s [ i ] == char ( int ( s [ i + 1 ] ) + 1 ) ) { continue ; } else if ( s [ i ] == ' a ' && s [ i + 1 ] == ' z ' ) { continue ; } else { flag = 0 ; break ; } } if ( flag == 0 ) cout << " NO " ; else cout << " YES " ; } int main ( ) { string s = " ecbaz " ; checkInfinite ( s ) ; return 0 ; }
Minimum change in lanes required to cross all barriers | C ++ program for the above approach ; Function to find the minimum number of changes of lane required ; If there is a barrier , then add very large value ; Add the minimum value to move forword with or without crossing barrier ; Return the minimum value of dp [ 0 ] , dp [ 1 ] and dp [ 2 ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minChangeInLane ( int barrier [ ] , int n ) { int dp [ ] = { 1 , 0 , 1 } ; for ( int j = 0 ; j < n ; j ++ ) { int val = barrier [ j ] ; if ( val > 0 ) { dp [ val - 1 ] = 1e6 ; } for ( int i = 0 ; i < 3 ; i ++ ) { if ( val != i + 1 ) { dp [ i ] = min ( dp [ i ] , min ( dp [ ( i + 1 ) % 3 ] , dp [ ( i + 2 ) % 3 ] ) + 1 ) ; } } } return min ( dp [ 0 ] , min ( dp [ 1 ] , dp [ 2 ] ) ) ; } int main ( ) { int barrier [ ] = { 0 , 1 , 2 , 3 , 0 } ; int N = sizeof ( barrier ) / sizeof ( barrier [ 0 ] ) ; cout << minChangeInLane ( barrier , N ) ; return 0 ; }
Queries to count groups of N students possible having sum of ratings within given range | C ++ program for the above approach ; Function to count number of ways to get given sum groups ; Initialise dp array ; Mark all 1 st row values as 1 since the mat [ 0 ] [ i ] is all possible sums in first row ; Fix the ith row ; Fix the sum ; Iterate through all values of ith row ; If sum can be obtained ; Find the prefix sum of last row ; Traverse each query ; No of ways to form groups ; Driver Code ; Given n batches and k students ; Given ratings ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numWays ( int ratings [ n ] [ k ] , int queries [ ] [ 2 ] ) { int dp [ n ] [ 10000 + 2 ] ; for ( int i = 0 ; i < k ; i ++ ) dp [ 0 ] [ ratings [ 0 ] [ i ] ] += 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int sum = 0 ; sum <= 10000 ; sum ++ ) { for ( int j = 0 ; j < k ; j ++ ) { if ( sum >= ratings [ i ] [ j ] ) dp [ i ] [ sum ] += dp [ i - 1 ] [ sum - ratings [ i ] [ j ] ] ; } } } for ( int sum = 1 ; sum <= 10000 ; sum ++ ) { dp [ n - 1 ] [ sum ] += dp [ n - 1 ] [ sum - 1 ] ; } for ( int q = 0 ; q < 2 ; q ++ ) { int a = queries [ q ] [ 0 ] ; int b = queries [ q ] [ 1 ] ; cout << dp [ n - 1 ] [ b ] - dp [ n - 1 ] [ a - 1 ] << " ▁ " ; } } int main ( ) { #define n  2 NEW_LINE #define k  3 NEW_LINE int ratings [ n ] [ k ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; int queries [ ] [ 2 ] = { { 6 , 6 } , { 1 , 6 } } ; numWays ( ratings , queries ) ; return 0 ; }
Number of permutation with K inversions | Set 2 | C ++ program for the above approach ; Function to count permutations with K inversions ; Store number of permutations with K inversions ; If N = 1 only 1 permutation with no inversion ; For K = 0 only 1 permutation with no inversion ; Otherwise Update each dp state as per the reccurrance relation formed ; Print final count ; Driver Code ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfPermWithKInversion ( int N , int K ) { int dp [ 2 ] [ K + 1 ] ; int mod = 1000000007 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 0 ; j <= K ; j ++ ) { if ( i == 1 ) dp [ i % 2 ] [ j ] = ( j == 0 ) ; else if ( j == 0 ) dp [ i % 2 ] [ j ] = 1 ; else dp [ i % 2 ] [ j ] = ( dp [ i % 2 ] [ j - 1 ] % mod + ( dp [ 1 - i % 2 ] [ j ] - ( ( max ( j - ( i - 1 ) , 0 ) == 0 ) ? 0 : dp [ 1 - i % 2 ] [ max ( j - ( i - 1 ) , 0 ) - 1 ] ) + mod ) % mod ) % mod ; ; } } cout << dp [ N % 2 ] [ K ] ; } int main ( ) { int N = 3 , K = 2 ; numberOfPermWithKInversion ( N , K ) ; return 0 ; }
Maximum sum path in a Matrix | No of rows and columns ; Declaring the matrix of maximum 100 rows and 100 columns ; Variable visited is used to keep track of all the visited positions Variable dp is used to store maximum sum till current position ; For storing current sum ; For continuous update of maximum sum required ; Function to Input the matrix of size n * m ; Function to calculate maximum sum of path ; Checking boundary condition ; Checking whether or not ( i , j ) is visited ; Marking ( i , j ) is visited ; Checking whether the position hasn 't  visited the last row or the last column.  Making recursive call for all the possible  moves from the current cell and then adding the  maximum returned by the calls and updating it. ; Checking whether position has reached last row ; If the position is in the last column ; Returning the updated maximum value ; Driver Code ; Calling the implemented function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE int n , m ; int a [ N ] [ N ] ; vector < vector < int > > dp ( N , vector < int > ( N ) ) , visited ( N , vector < int > ( N ) ) ; int current_sum = 0 ; int total_sum = 0 ; void inputMatrix ( ) { n = 3 ; m = 3 ; a [ 0 ] [ 0 ] = 500 ; a [ 0 ] [ 1 ] = 100 ; a [ 0 ] [ 2 ] = 230 ; a [ 1 ] [ 0 ] = 1000 ; a [ 1 ] [ 1 ] = 300 ; a [ 1 ] [ 2 ] = 100 ; a [ 2 ] [ 0 ] = 200 ; a [ 2 ] [ 1 ] = 1000 ; a [ 2 ] [ 2 ] = 200 ; } int maximum_sum_path ( int i , int j ) { if ( i == n - 1 && j == m - 1 ) return a [ i ] [ j ] ; if ( visited [ i ] [ j ] ) return dp [ i ] [ j ] ; visited [ i ] [ j ] = 1 ; int & total_sum = dp [ i ] [ j ] ; if ( i < n - 1 & j < m - 1 ) { int current_sum = max ( maximum_sum_path ( i , j + 1 ) , max ( maximum_sum_path ( i + 1 , j + 1 ) , maximum_sum_path ( i + 1 , j ) ) ) ; total_sum = a [ i ] [ j ] + current_sum ; } else if ( i == n - 1 ) total_sum = a [ i ] [ j ] + maximum_sum_path ( i , j + 1 ) ; else total_sum = a [ i ] [ j ] + maximum_sum_path ( i + 1 , j ) ; return dp [ i ] [ j ] = total_sum ; } int main ( ) { inputMatrix ( ) ; int maximum_sum = maximum_sum_path ( 0 , 0 ) ; cout << maximum_sum ; return 0 ; }
Treasure and Cities | k is current index and col is previous color . ; base case ; check if color of this city is equal to prev visited city ; return max of both options ; Driver code ; Initially begin with color 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxProfit ( int treasure [ ] , int color [ ] , int n , int k , int col , int A , int B ) { int sum = 0 ; if ( k == n ) return 0 ; if ( col == color [ k ] ) sum += max ( A * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; else sum += max ( B * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; return sum ; } int main ( ) { int A = -5 , B = 7 ; int treasure [ ] = { 4 , 8 , 2 , 9 } ; int color [ ] = { 2 , 2 , 6 , 2 } ; int n = sizeof ( color ) / sizeof ( color [ 0 ] ) ; cout << MaxProfit ( treasure , color , n , 0 , 0 , A , B ) ; return 0 ; }
Tetranacci Numbers | A simple recursive CPP program to print the nth tetranacci numbers . ; Function to return the N - th tetranacci number ; base cases ; base cases ; base cases ; function to print the nth tetranacci number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int printTetraRec ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return 1 ; if ( n == 3 ) return 2 ; else return printTetraRec ( n - 1 ) + printTetraRec ( n - 2 ) + printTetraRec ( n - 3 ) + printTetraRec ( n - 4 ) ; } void printTetra ( int n ) { cout << printTetraRec ( n ) << " ▁ " ; } int main ( ) { int n = 10 ; printTetra ( n ) ; return 0 ; }
Sum of products of all combination taken ( 1 to n ) at a time | Program to find SOP of all combination taken ( 1 to N ) at a time using brute force ; to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver 's code ; storing numbers from 1 - N in array ; calling allCombination
#include <iostream> NEW_LINE using namespace std ; int sum = 0 ; void Combination ( int a [ ] , int combi [ ] , int n , int r , int depth , int index ) { if ( index == r ) { int product = 1 ; for ( int i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( int i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } void allCombination ( int a [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int * combi = new int [ i ] ; Combination ( a , combi , n , i , 0 , 0 ) ; cout << " f ( " << i << " ) ▁ - - > ▁ " << sum << " STRNEWLINE " ; sum = 0 ; free ( combi ) ; } } int main ( ) { int n = 5 ; int * a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ; return 0 ; }
Dynamic Programming | High | A DP based C ++ program to find maximum tasks . ; Returns the maximum among the 2 numbers ; Returns maximum amount of task that can be done till day n ; An array task_dp that stores the maximum task done ; If n = 0 , no solution exists ; If n = 1 , high effort task on that day will be the solution ; Fill the entire array determining which task to choose on day i ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int max ( int x , int y ) { return ( x > y ? x : y ) ; } int maxTasks ( int high [ ] , int low [ ] , int n ) { int task_dp [ n + 1 ] ; task_dp [ 0 ] = 0 ; task_dp [ 1 ] = high [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) task_dp [ i ] = max ( high [ i - 1 ] + task_dp [ i - 2 ] , low [ i - 1 ] + task_dp [ i - 1 ] ) ; return task_dp [ n ] ; } int main ( ) { int n = 5 ; int high [ ] = { 3 , 6 , 8 , 7 , 6 } ; int low [ ] = { 1 , 5 , 4 , 5 , 3 } ; cout << maxTasks ( high , low , n ) ; return 0 ; }
Permutation Coefficient | A O ( n ) time and O ( 1 ) extra space solution to calculate the Permutation Coefficient ; Compute n * ( n - 1 ) * ( n - 2 ) . ... ( n - k + 1 ) ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int PermutationCoeff ( int n , int k ) { int P = 1 ; for ( int i = 0 ; i < k ; i ++ ) P *= ( n - i ) ; return P ; } int main ( ) { int n = 10 , k = 2 ; cout << " Value ▁ of ▁ P ( " << n << " , ▁ " << k << " ) ▁ is ▁ " << PermutationCoeff ( n , k ) ; return 0 ; }
Partition problem | DP | A Dynamic Programming based C ++ program to partition problem ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of all elements ; initialize top row as true ; initialize leftmost column , except part [ 0 ] [ 0 ] , as 0 ; Fill the partition table in bottom up manner ; uncomment this part to print table ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPartiion ( int arr [ ] , int n ) { int sum = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; bool part [ sum / 2 + 1 ] [ n + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) part [ 0 ] [ i ] = true ; for ( i = 1 ; i <= sum / 2 ; i ++ ) part [ i ] [ 0 ] = false ; for ( i = 1 ; i <= sum / 2 ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { part [ i ] [ j ] = part [ i ] [ j - 1 ] ; if ( i >= arr [ j - 1 ] ) part [ i ] [ j ] = part [ i ] [ j ] || part [ i - arr [ j - 1 ] ] [ j - 1 ] ; } } return part [ sum / 2 ] [ n ] ; } int main ( ) { int arr [ ] = { 3 , 1 , 1 , 2 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( findPartiion ( arr , n ) == true ) cout << " Can ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ " " sum " ; else cout << " Can ▁ not ▁ be ▁ divided ▁ into " << " ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ; return 0 ; }
Minimum number of Appends of X or Y characters from the end to the front required to obtain given string | C ++ program for the above approach ; Function to find the minimum operations required to get the given string after appending m or n characters from the end to the front of the string in each operation ; Store the original string ; Stores the count of operations ; Traverse the string ; Cut m letters from end ; Add cut m letters to beginning ; Update j ; Check if strings are the same ; Cut n letters from end ; Add cut n letters to beginning ; Update j ; Check if strings are the same ; Update the turn ; Driver Code ; Given string S ; Function Call
#include <iostream> NEW_LINE using namespace std ; int minimumOperations ( string orig_str , int m , int n ) { string orig = orig_str ; int turn = 1 ; int j = 1 ; for ( auto i : orig_str ) { string m_cut = orig_str . substr ( orig_str . length ( ) - m ) ; orig_str . erase ( orig_str . length ( ) - m ) ; orig_str = m_cut + orig_str ; j = j + 1 ; if ( orig != orig_str ) { turn = turn + 1 ; string n_cut = orig_str . substr ( orig_str . length ( ) - n ) ; orig_str . erase ( orig_str . length ( ) - n ) ; orig_str = n_cut + orig_str ; j = j + 1 ; } if ( orig == orig_str ) { break ; } turn = turn + 1 ; } cout << turn ; } int main ( ) { string S = " GeeksforGeeks " ; int X = 5 , Y = 3 ; minimumOperations ( S , X , Y ) ; return 0 ; }
Minimum rotations required to get the same String | Set | C ++ implementation of the above approach ; Prints occurrences of txt [ ] in pat [ ] ; Create lps [ ] that will hold the longest prefix suffix values for pattern ; Preprocess the pattern ( calculate lps [ ] array ) ; Index for txt [ ] , index for pat [ ] ; Mismatch after j matches ; Do not match lps [ 0. . lps [ j - 1 ] ] characters , they will match anyway ; Fills lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; The loop calculates lps [ i ] for i = 1 to M - 1 ; ( pat [ i ] != pat [ len ] ) ; This is tricky . Consider the example . AAACAAAA and i = 7. The idea is similar to search step . ; Returns count of rotations to get the same string back ; Form a string excluding the first character and concatenating the string at the end ; Convert the string to character array ; Use the KMP search algorithm to find it in O ( N ) time ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void computeLPSArray ( char * pat , int M , int * lps ) ; int KMPSearch ( char * pat , char * txt ) { int M = strlen ( pat ) ; int N = strlen ( txt ) ; int lps [ M ] ; computeLPSArray ( pat , M , lps ) ; int i = 0 ; int j = 0 ; while ( i < N ) { if ( pat [ j ] == txt [ i ] ) { j ++ ; i ++ ; } if ( j == M ) { return i - j ; j = lps [ j - 1 ] ; } else if ( i < N && pat [ j ] != txt [ i ] ) { if ( j != 0 ) j = lps [ j - 1 ] ; else i = i + 1 ; } } } void computeLPSArray ( char * pat , int M , int * lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } int countRotations ( string s ) { string s1 = s . substr ( 1 , s . size ( ) - 1 ) + s ; char pat [ s . length ( ) ] , text [ s1 . length ( ) ] ; strcpy ( pat , s . c_str ( ) ) ; strcpy ( text , s1 . c_str ( ) ) ; return 1 + KMPSearch ( pat , text ) ; } int main ( ) { string s1 = " geeks " ; cout << countRotations ( s1 ) ; return 0 ; }
DFA for Strings not ending with " THE " | C ++ program to implement DFS that accepts all string that do not end with " THE " ; dfa tells the number associated with the present state ; This function is for the starting state ( zeroth ) of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; This function is for the first state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; On receiving ' H ' or ' h ' goto second state ( 2 ) ; Else goto starting state ( 0 ) ; This function is for the second state of DFA ; On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 ) ; This function is for the third state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 ) ; Store length of string ; Driver code
#include <iostream> NEW_LINE using namespace std ; int dfa = 0 ; void start ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; } void state1 ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; else if ( c == ' h ' c == ' H ' ) dfa = 2 ; else dfa = 0 ; } void state2 ( char c ) { if ( c == ' e ' c == ' E ' ) dfa = 3 ; else if ( c == ' t ' c == ' T ' ) dfa = 1 ; else dfa = 0 ; } void state3 ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; else dfa = 0 ; } bool isAccepted ( string str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( dfa == 0 ) start ( str [ i ] ) ; else if ( dfa == 1 ) state1 ( str [ i ] ) ; else if ( dfa == 2 ) state2 ( str [ i ] ) ; else state3 ( str [ i ] ) ; } return ( dfa != 3 ) ; } int main ( ) { string str = " forTHEgeeks " ; if ( isAccepted ( str ) == true ) cout << " ACCEPTED STRNEWLINE " ; else cout << " NOT ▁ ACCEPTED STRNEWLINE " ; return 0 ; }
Check if one string can be converted to another | C ++ implementation of the above approach . ; Function for find from Disjoint set algorithm ; Function for the union from Disjoint set algorithm ; Function to check if one string can be converted to another . ; All the characters are checked whether it 's either not replaced or replaced  by a similar character using a map. ; To check if there are cycles . If yes , then they are not convertible . Else , they are convertible . ; Function to initialize parent array for union and find algorithm . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int parent [ 26 ] ; int find ( int x ) { if ( x != parent [ x ] ) return parent [ x ] = find ( parent [ x ] ) ; return x ; } void join ( int x , int y ) { int px = find ( x ) ; int pz = find ( y ) ; if ( px != pz ) { parent [ pz ] = px ; } } bool convertible ( string s1 , string s2 ) { map < int , int > mp ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( mp . find ( s1 [ i ] - ' a ' ) == mp . end ( ) ) { mp [ s1 [ i ] - ' a ' ] = s2 [ i ] - ' a ' ; } else { if ( mp [ s1 [ i ] - ' a ' ] != s2 [ i ] - ' a ' ) return false ; } } for ( auto it : mp ) { if ( it . first == it . second ) continue ; else { if ( find ( it . first ) == find ( it . second ) ) return false ; else join ( it . first , it . second ) ; } } return true ; } void initialize ( ) { for ( int i = 0 ; i < 26 ; i ++ ) { parent [ i ] = i ; } } int main ( ) { string s1 , s2 ; s1 = " abbcaa " ; s2 = " bccdbb " ; initialize ( ) ; if ( convertible ( s1 , s2 ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Print characters having prime frequencies in order of occurrence | C ++ implementation of the approach ; 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 print the prime frequency characters in the order of their occurrence ; Function to create Sieve to check primes ; To store the frequency of each of the character of the string ; Initialize all elements of freq [ ] to 0 ; Update the frequency of each character ; Traverse str character by character ; If frequency of current character is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE 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 ; } } } void printChar ( string str , int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , str . length ( ) + 1 ) ; int freq [ SIZE ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ freq [ str [ i ] - ' a ' ] ] ) { cout << str [ i ] ; } } } int main ( ) { string str = " geeksforgeeks " ; int n = str . length ( ) ; printChar ( str , n ) ; return 0 ; }
Print characters having even frequencies in order of occurrence | C ++ implementation of the approach ; Function to print the even frequency characters in the order of their occurrence ; To store the frequency of each of the character of the string ; Initialize all elements of freq [ ] to 0 ; Update the frequency of each character ; Traverse str character by character ; If frequency of current character is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE void printChar ( string str , int n ) { int freq [ SIZE ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str [ i ] - ' a ' ] % 2 == 0 ) { cout << str [ i ] ; } } } int main ( ) { string str = " geeksforgeeks " ; int n = str . length ( ) ; printChar ( str , n ) ; return 0 ; }
Compare two strings considering only alphanumeric characters | Function to check alphanumeric equality of both strings ; variable declaration ; Length of first string ; Length of second string ; To check each and every characters of both string ; If the current character of the first string is not an alphanumeric character , increase the pointer i ; If the current character of the second string is not an alphanumeric character , increase the pointer j ; if all alphanumeric characters of both strings are same then return true ; if any alphanumeric characters of both strings are not same then return false ; If current character matched , increase both pointers to check the next character ; If not same , then return false ; Function to print Equal or Unequal if strings are same or not ; check alphanumeric equality of both strings ; if both are alphanumeric equal , print Equal ; otherwise print Unequal ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool CompareAlphanumeric ( string & str1 , string & str2 ) { int i , j ; i = 0 ; j = 0 ; int len1 = str1 . size ( ) ; int len2 = str2 . size ( ) ; while ( i <= len1 && j <= len2 ) { while ( i < len1 && ( ! ( ( str1 [ i ] >= ' a ' && str1 [ i ] <= ' z ' ) || ( str1 [ i ] >= ' A ' && str1 [ i ] <= ' Z ' ) || ( str1 [ i ] >= '0' && str1 [ i ] <= '9' ) ) ) ) { i ++ ; } while ( j < len2 && ( ! ( ( str2 [ j ] >= ' a ' && str2 [ j ] <= ' z ' ) || ( str2 [ j ] >= ' A ' && str2 [ j ] <= ' Z ' ) || ( str2 [ j ] >= '0' && str2 [ j ] <= '9' ) ) ) ) { j ++ ; } if ( i == len1 && j == len2 ) return true ; else if ( str1 [ i ] != str2 [ j ] ) return false ; else { i ++ ; j ++ ; } } return false ; } void CompareAlphanumericUtil ( string str1 , string str2 ) { bool res ; res = CompareAlphanumeric ( str1 , str2 ) ; if ( res == true ) cout << " Equal " << endl ; else cout << " Unequal " << endl ; } int main ( ) { string str1 , str2 ; str1 = " Ram , ▁ Shyam " ; str2 = " ▁ Ram ▁ - ▁ Shyam . " ; CompareAlphanumericUtil ( str1 , str2 ) ; str1 = " abc123" ; str2 = "123abc " ; CompareAlphanumericUtil ( str1 , str2 ) ; return 0 ; }