codeparrot/xlcost-text-to-code huggingface dataset:
Convert the given RGB color code to Hex color code | C ++ code to convert the given RGB color code to Hex color code ; function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; string decToHexa ( int n ) { char hexaDeciNum [ 2 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = temp + 48 ; i ++ ; } else { hexaDeciNum [ i ] = temp + 55 ; i ++ ; } n = n / 16 ; } string hexCode = " " ; if ( i == 2 ) { hexCode . push_back ( hexaDeciNum [ 0 ] ) ; hexCode . push_back ( hexaDeciNum [ 1 ] ) ; } else if ( i == 1 ) { hexCode = "0" ; hexCode . push_back ( hexaDeciNum [ 0 ] ) ; } else if ( i == 0 ) hexCode = "00" ; return hexCode ; } string convertRGBtoHex ( int R , int G , int B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { string hexCode = " # " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " - 1" ; } int main ( ) { int R = 0 , G = 0 , B = 0 ; cout << convertRGBtoHex ( R , G , B ) << endl ; R = 255 , G = 255 , B = 255 ; cout << convertRGBtoHex ( R , G , B ) << endl ; R = 25 , G = 56 , B = 123 ; cout << convertRGBtoHex ( R , G , B ) << endl ; R = 2 , G = 3 , B = 4 ; cout << convertRGBtoHex ( R , G , B ) << endl ; R = 255 , G = 255 , B = 256 ; cout << convertRGBtoHex ( R , G , B ) << endl ; return 0 ; }
Program to build a DFA to accept strings that start and end with same character | C ++ Program for DFA that accepts string if it starts and ends with same character ; States of DFA ; Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
#include <bits/stdc++.h> NEW_LINE using namespace std ; void q1 ( string , int ) ; void q2 ( string , int ) ; void q3 ( string , int ) ; void q4 ( string , int ) ; void q1 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " Yes ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q2 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q3 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " Yes ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q4 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q0 ( string s , int i ) { if ( i == s . length ( ) ) { cout << " No ▁ STRNEWLINE " ; return ; } if ( s [ i ] == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } int main ( ) { string s = " abbaabb " ; q0 ( s , 0 ) ; }
Longest prefix in a string with highest frequency | C ++ implementation of the above approach ; Function to find Longest prefix string with the highest frequency ; storing all indices where first element is found ; if the first letter in the string does not occur again then answer will be the whole string ; loop till second appearance of the first element ; check one letter after every stored index ; If there is no mismatch we move forward ; otherwise we stop ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void prefix ( string str ) { int k = 1 , j ; int n = str . length ( ) ; vector < int > g ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) { g . push_back ( i ) ; flag = 1 ; } } if ( flag == 0 ) { cout << str << endl ; } else { int len = g . size ( ) ; while ( k < g [ 0 ] ) { int cnt = 0 ; for ( j = 0 ; j < len ; j ++ ) { if ( str [ g [ j ] + k ] == str [ k ] ) { cnt ++ ; } } if ( cnt == len ) { k ++ ; } else { break ; } } for ( int i = 0 ; i < k ; i ++ ) { cout << str [ i ] ; } cout << endl ; } } int main ( ) { string str = " abcab " ; prefix ( str ) ; return 0 ; }
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | C ++ implementation of the approach ; Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int N , int K ) { vector < int > res ; int l = 1 , r = N , flag = 0 ; for ( int i = 0 ; i < K ; i ++ ) { if ( ! flag ) { res . push_back ( l ) ; l ++ ; } else { res . push_back ( r ) ; r -- ; } flag ^= 1 ; } if ( ! flag ) { for ( int i = r ; i >= l ; i -- ) res . push_back ( i ) ; } else { for ( int i = l ; i <= r ; i ++ ) res . push_back ( i ) ; } for ( auto i : res ) cout << i << " ▁ " ; } int main ( ) { int N = 10 , K = 4 ; printPermutation ( N , K ) ; return 0 ; }
Find Nth term of the series 1 , 8 , 54 , 384. . . | CPP program to find N - th term of the series : 1 , 8 , 54 , 384. . . ; calculate factorial of N ; calculate Nth term of series ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int fact ( int N ) { int i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } int nthTerm ( int N ) { return ( N * N ) * fact ( N ) ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) ; return 0 ; }
Rabin | Following program is a C ++ implementation of Rabin Karp Algorithm given in the CLRS book ; d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; hash value for pattern int t = 0 ; hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters one by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver code ; A prime number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define d  256  NEW_LINE void search ( char pat [ ] , char txt [ ] , int q ) { int M = strlen ( pat ) ; int N = strlen ( txt ) ; int i , j ; int h = 1 ; for ( i = 0 ; i < M - 1 ; i ++ ) h = ( h * d ) % q ; for ( i = 0 ; i < M ; i ++ ) { p = ( d * p + pat [ i ] ) % q ; t = ( d * t + txt [ i ] ) % q ; } for ( i = 0 ; i <= N - M ; i ++ ) { if ( p == t ) { bool flag = true ; for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) { flag = false ; break ; } if ( flag ) cout << i << " ▁ " ; } if ( j == M ) cout << " Pattern ▁ found ▁ at ▁ index ▁ " << i << endl ; } if ( i < N - M ) { t = ( d * ( t - txt [ i ] * h ) + txt [ i + M ] ) % q ; if ( t < 0 ) t = ( t + q ) ; } } } int main ( ) { char txt [ ] = " GEEKS ▁ FOR ▁ GEEKS " ; char pat [ ] = " GEEK " ; int q = 101 ; search ( pat , txt , q ) ; return 0 ; }
Find the final string after flipping bits at the indices that are multiple of prime factors of array elements | C ++ program for the above approach ; Stores smallest prime factor ; Function to find the smallest prime factor for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Function to find all the distinct prime factors of the given number x ; Find the prime factors for X ; Find the spf [ ] of x ; Return the prime factors for x ; Function to find string after flipping the characters at indices of prime factors of array elements arr [ ] ; Precalculating Smallest Prime Factor ; Stores the frequency of each prime factor ; Iterate over all elements of the array arr [ ] ; Stores prime factors of arr [ i ] ; Increment the frequency of all prime factors of arr [ i ] ; Iterate over all elements of the array frequency [ ] ; If frequency [ i ] is odd ; Flip bits of all indices that are multiple of i ; Return Answer ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; #define MAXN  100001 NEW_LINE int spf [ MAXN ] ; void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } } vector < int > getFactorization ( int x ) { vector < int > ret ; while ( x != 1 ) { ret . push_back ( spf [ x ] ) ; int value = spf [ x ] ; while ( x % value == 0 ) x = x / value ; } return ret ; } string flipString ( string S , int arr [ ] , int M ) { sieve ( ) ; int frequency [ MAXN ] = { 0 } ; for ( int i = 0 ; i < M ; i ++ ) { vector < int > primeFactors = getFactorization ( arr [ i ] ) ; for ( auto & factors : primeFactors ) { frequency [ factors ] ++ ; frequency [ factors ] %= 2 ; } } int N = S . length ( ) ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( frequency [ i ] & 1 ) { for ( int j = i ; j <= N ; j += i ) { S [ j - 1 ] = ( S [ j - 1 ] == '1' ? '0' : '1' ) ; } } } return S ; } int main ( ) { string S = "000000" ; int arr [ ] = { 2 , 4 , 6 } ; int M = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << flipString ( S , arr , M ) ; return 0 ; }
Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | C ++ program for the above approach ; Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isOnesGreater ( string S , int N ) { int cnt0 = 0 ; int cnt1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) cnt1 ++ ; else cnt0 ++ ; } for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { if ( i > 0 && S [ i - 1 ] == '0' ) { S [ i - 1 ] = ' _ ' ; cnt0 -- ; } else if ( i < N && S [ i + 1 ] == '0' ) { S [ i + 1 ] = ' _ ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string S = "01" ; int N = S . length ( ) ; isOnesGreater ( S , N ) ; return 0 ; }
Minimize flips to make binary string as all 1 s by flipping characters in substring of size K repeatedly | C ++ program for the above approach ; Function to find the minimum number of operations required to convert all the characters to 1 by flipping the substrings of size K ; Stores the minimum number of operations required ; Traverse the string S ; If the character is 0 ; Flip the substrings of size K starting from i ; Increment the minimum count of operations required ; After performing the operations check if string S contains any 0 s ; If S contains only 1 's ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperation ( string S , int K , int N ) { int min = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' && i + K <= N ) { for ( int j = i ; j < i + K ; j ++ ) { if ( S [ j ] == '1' ) S [ j ] = '0' ; else S [ j ] = '1' ; } min ++ ; } } for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) break ; } if ( i == N ) cout << min ; else cout << -1 ; } int main ( ) { string S = "00010110" ; int K = 3 ; int N = S . length ( ) ; minOperation ( S , K , N ) ; return 0 ; }
Check if string S1 can be formed using repeated insertions of another string S2 | C ++ implementation for the above approach ; Function to check a valid insertion ; Store the size of string ; Maintain a stack for characters ; Iterate through the string ; push the current character on top of the stack ; If the current character is the last character of string S2 then pop characters until S2 is not formed ; index of last character of the string S2 ; pop characters till 0 - th index ; Check if stack in non - empty ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool validInsertionstring ( string S1 , string S2 ) { int N = S1 . length ( ) ; int M = S2 . length ( ) ; stack < char > st ; for ( int i = 0 ; i < N ; i ++ ) { st . push ( S1 [ i ] ) ; if ( S1 [ i ] == S2 [ M - 1 ] ) { int idx = M - 1 ; while ( idx >= 0 ) { if ( st . empty ( ) ) { return false ; } char c = st . top ( ) ; st . pop ( ) ; if ( c != S2 [ idx ] ) { return false ; } idx -- ; } } } if ( ! st . empty ( ) ) { return false ; } else { return true ; } } int main ( ) { string S1 = " aabb " ; string S2 = " ab " ; validInsertionstring ( S1 , S2 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; }
Check if rearranging Array elements can form a Palindrome or not | C ++ implementation of the above approach ; Function to check whether elements of an array can form a palindrome ; create an empty string to append elements of an array ; append each element to the string str to form a string so that we can solve it in easy way ; Create a freq array and initialize all values as 0 ; For each character in formed string , increment freq in the corresponding freq array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Drive code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  256 NEW_LINE bool can_form_palindrome ( int arr [ ] , int n ) { string str = " " ; for ( int i = 0 ; i < n ; i ++ ) { str += arr [ i ] ; } int freq [ MAX ] = { 0 } ; for ( int i = 0 ; str [ i ] ; i ++ ) { freq [ str [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] & 1 ) { count ++ ; } if ( count > 1 ) { return false ; } } return true ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; can_form_palindrome ( arr , n ) ? cout << " YES " : cout << " NO " ; return 0 ; }
Minimum number of flips to make a Binary String increasing | C ++ program for the above approach ; ; Function to find the minimum number of flips required to make string increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; stores the minimum count of flip ; Traverse the given string ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Driver code ; Given string ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumFlips ( string s ) { int n = s . size ( ) ; int cnt0 = count ( s . begin ( ) , s . end ( ) , '0' ) ; int cnt1 = 0 ; int res = n - cnt0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { cnt0 -= 1 ; } else if ( s [ i ] == '1' ) { res = min ( res , cnt1 + cnt0 ) ; cnt1 ++ ; } } return res ; } int main ( ) { string S = "000110" ; cout << minimumFlips ( S ) ; return 0 ; }
Lexicographically shortest string of length at most K which is not a substring of given String | C ++ implementation for above approach ; Function to return a set of all substrings of given string which have length less than or equal to k ; Function to print the lexicographically smallest substring of length atmost k which is not present in given string s ; All substrings of length atmost k present in string s are stored in this set ; Loop to change length of substring ; String with length = len which has all characters as ' a ' ; If the substrings set does not contain this string then we have found the answer ; Changing the likes of ' azz ' and ' daz ' to ' baa ' and ' dba ' respectively ; Reached a string like ' zz ' or ' zzz ' increase the length of the substring ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < string > presentSubstring ( string s , int k ) { set < string > st ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string s1 = " " ; for ( int j = 0 ; j < k && i + j < n ; j ++ ) { s1 . push_back ( s [ i + j ] ) ; st . insert ( s1 ) ; } } return st ; } string smallestSubstring ( string s , int k ) { set < string > st ; st = presentSubstring ( s , k ) ; int index ; for ( int len = 1 ; len <= k ; len ++ ) { string t ( len , ' a ' ) ; while ( true ) { if ( st . count ( t ) == 0 ) { return t ; } index = len - 1 ; while ( index >= 0 && t [ index ] == ' z ' ) { t [ index ] = ' a ' ; index -- ; } if ( index >= 0 ) t [ index ] ++ ; else break ; } } return " - 1" ; } int main ( ) { string s = " sdhaacbdefghijklmnopqrstuvwxyz " ; int K = 3 ; cout << smallestSubstring ( s , K ) << endl ; return 0 ; }
Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0 | C ++ program for the above approach ; Function to find the maximum value of K such that flipping substrings of size at least K make all characters 0 s ; Stores the maximum value of K ; Traverse the given string S ; Store the minimum of the maximum of LHS and RHS length ; Flip performed ; If no flips performed ; Return the possible value of K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumK ( string & S ) { int N = S . length ( ) ; int ans = N ; int flag = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] != S [ i + 1 ] ) { flag = 1 ; ans = min ( ans , max ( i + 1 , N - i - 1 ) ) ; } } if ( flag == 0 ) return 0 ; return ans ; } int main ( ) { string S = "010" ; cout << maximumK ( S ) ; return 0 ; }
Check if a pair of strings exists that starts with and without the character K or not | C ++ program for the above approach ; Function to check whether a pair of strings exists satisfying the conditions ; Stores the visited strings ; Iterate over the array arr [ ] ; If first character of current string is K ; Otherwise ; Adding to the visited ; Driver Code ; Given Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkhappy ( vector < string > arr , char K , int N ) { set < string > visited ; for ( string s : arr ) { if ( s [ 0 ] == K ) if ( visited . find ( s . substr ( 1 ) ) != visited . end ( ) ) return " Yes " ; else if ( visited . find ( ( K + s ) ) != visited . end ( ) ) return " Yes " ; visited . insert ( s ) ; } return " No " ; } int main ( ) { vector < string > arr = { " a " , " ! ▁ a " , " b " , " ! ▁ c " , " d " , " ! ▁ d " } ; char K = ' ! ' ; int N = arr . size ( ) ; cout << checkhappy ( arr , K , N ) << endl ; return 0 ; }
Minimum moves to make count of lowercase and uppercase letters equal | C ++ program for the above approach ; Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the string S ; If current character is uppercase ; Increment count of Uppercase characters ; Otherwise , ; Increment count of Lowercase characters ; Stores minimum number of moves needed ; If there are more upper case characters ; Iterate until upper is greater than N / 2 ; Convert uppercase into lowercase until upper = N / 2 ; Increment the pointer ; If there are more lower case characters ; Iterate until lower is greater than N / 2 ; Convert lowercase into uppercase until lower = N / 2 ; Increment the pointer ; Print moves required ; Print resultant string ; Driver Code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumTimeToConvertString ( string S , int N ) { int upper = 0 , lower = 0 ; for ( int i = 0 ; i < N ; i ++ ) { char c = S [ i ] ; if ( isupper ( c ) ) { upper ++ ; } else { lower ++ ; } } int moves = 0 ; if ( upper > N / 2 ) { int i = 0 ; while ( upper > N / 2 && i < N ) { if ( isupper ( S [ i ] ) ) { S [ i ] += 32 ; moves ++ ; upper -- ; lower ++ ; } i ++ ; } } else if ( lower > N / 2 ) { int i = 0 ; while ( lower > N / 2 && i < N ) { if ( islower ( S [ i ] ) ) { S [ i ] -= 32 ; moves ++ ; upper ++ ; lower -- ; } i ++ ; } } cout << moves << endl ; cout << S << endl ; } int main ( ) { string S = " AbcdEf " ; int N = S . length ( ) ; minimumTimeToConvertString ( S , N ) ; return 0 ; }
Program to print a string in vertical zigzag manner | C ++ program for the above approach ; Function to print any string in zigzag fashion ; Store the gap between the major columns ; Traverse through rows ; Store the step value for each row ; Iterate in the range [ 1 , N - 1 ] ; Print the character ; Print the spaces before character s [ j + step ] ; Print the character ; Print the spaces after character after s [ j + step ] ; Print the spaces for first and last rows ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void zigzag ( string s , int rows ) { int interval = 2 * rows - 2 ; for ( int i = 0 ; i < rows ; i ++ ) { int step = interval - 2 * i ; for ( int j = i ; j < s . length ( ) ; j = j + interval ) { cout << s [ j ] ; if ( step > 0 && step < interval && step + j < s . length ( ) ) { for ( int k = 0 ; k < ( interval - rows - i ) ; k ++ ) cout << " ▁ " ; cout << s [ j + step ] ; for ( int k = 0 ; k < i - 1 ; k ++ ) cout << " ▁ " ; } else { for ( int k = 0 ; k < ( interval - rows ) ; k ++ ) cout << " ▁ " ; } } cout << endl ; } } int main ( ) { string s = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh " " ijklmnopqrstuvwxyz " ; int rows = 9 ; zigzag ( s , rows ) ; }
Number of substrings with each character occurring even times | C ++ program for the above approach ; Function to count substrings having even frequency of each character ; Stores the count of a character ; Stores bitmask ; Stores the count of substrings with even count of each character ; Traverse the string S ; Flip the ord ( i ) - 97 bits in pre ; Increment the count by hash [ pre ] ; Increment count of pre in hash ; Return the total count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subString ( string s , int n ) { map < int , int > hash ; hash [ 0 ] = 1 ; int pre = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << int ( s [ i ] ) - 97 ) ; count += hash [ pre ] ; hash [ pre ] = hash [ pre ] + 1 ; } return count ; } int main ( ) { string S = " abbaa " ; int N = S . length ( ) ; cout << ( subString ( S , N ) ) ; }