codeparrot/xlcost-text-to-code huggingface dataset:
Generate lexicographically smallest string of 0 , 1 and 2 with adjacent swaps allowed | C ++ implementation of the approach ; Function to print the required string ; count number of 1 s ; To check if the all the 1 s have been used or not ; Print all the 1 s if any 2 is encountered ; If str [ i ] = 0 or str [ i ] = 2 ; If 1 s are not printed yet ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printString ( string str , int n ) { int ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) ones ++ ; bool used = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '2' && ! used ) { used = 1 ; for ( int j = 0 ; j < ones ; j ++ ) cout << "1" ; } if ( str [ i ] != '1' ) cout << str [ i ] ; } if ( ! used ) for ( int j = 0 ; j < ones ; j ++ ) cout << "1" ; } int main ( ) { string str = "100210" ; int n = str . length ( ) ; printString ( str , n ) ; return 0 ; }
K length words that can be formed from given characters without repetition | C ++ implementation of the approach ; Function to return the required count ; To store the count of distinct characters in str ; Traverse str character by character ; If current character is appearing for the first time in str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPermutation ( string str , int k ) { bool has [ 26 ] = { false } ; int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! has [ str [ i ] - ' a ' ] ) { cnt ++ ; has [ str [ i ] - ' a ' ] = true ; } } long long int ans = 1 ; for ( int i = 2 ; i <= cnt ; i ++ ) ans *= i ; for ( int i = cnt - k ; i > 1 ; i -- ) ans /= i ; return ans ; } int main ( ) { string str = " geeksforgeeks " ; int k = 4 ; cout << findPermutation ( str , k ) ; return 0 ; }
Find the number in a range having maximum product of the digits | CPP Program to find the number in a range having maximum product of the digits ; Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int product ( int x ) { int prod = 1 ; while ( x ) { prod *= ( x % 10 ) ; x /= 10 ; } return prod ; } int findNumber ( int l , int r ) { string a = to_string ( l ) ; string b = to_string ( r ) ; int ans = r ; for ( int i = 0 ; i < b . size ( ) ; i ++ ) { if ( b [ i ] == '0' ) continue ; string curr = b ; curr [ i ] = ( ( curr [ i ] - '0' ) - 1 ) + '0' ; for ( int j = i + 1 ; j < curr . size ( ) ; j ++ ) curr [ j ] = '9' ; int num = 0 ; for ( auto c : curr ) num = num * 10 + ( c - '0' ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } int main ( ) { int l = 1 , r = 10 ; cout << findNumber ( l , r ) << endl ; l = 51 , r = 62 ; cout << findNumber ( l , r ) << endl ; return 0 ; }
Longest Ordered Subsequence of Vowels | C ++ program to find the longest subsequence of vowels in the specified order ; Mapping values for vowels ; Function to check if given subsequence contains all the vowels or not ; not contain vowel ; Function to find the longest subsequence of vowels in the given string in specified order ; If we have reached the end of the string , return the subsequence if it is valid , else return an empty list ; If there is no vowel in the subsequence yet , add vowel at current index if it is ' a ' , else move on to the next character in the string ; If the last vowel in the subsequence until now is same as the vowel at current index , add it to the subsequence ; If the vowel at the current index comes right after the last vowel in the subsequence , we have two options : either to add the vowel in the subsequence , or move on to next character . We choose the one which gives the longest subsequence . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < char > vowels = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; map < char , int > mapping = { { ' a ' , 0 } , { ' e ' , 1 } , { ' i ' , 2 } , { ' o ' , 3 } , { ' u ' , 4 } } ; bool isValidSequence ( string subList ) { for ( char c : vowels ) { if ( subList . find ( c ) == std :: string :: npos ) return 0 ; } return 1 ; } string longestSubsequence ( string str , string subList , int index ) { int len = str . length ( ) ; if ( index >= len ) { if ( isValidSequence ( subList ) ) return subList ; else return " " ; } else if ( subList . size ( ) == 0 ) { if ( str [ index ] != ' a ' ) return longestSubsequence ( str , " " , index + 1 ) ; else return longestSubsequence ( str , subList + str [ index ] , index + 1 ) ; } else if ( mapping [ subList [ subList . size ( ) - 1 ] ] == mapping [ str [ index ] ] ) return longestSubsequence ( str , subList + str [ index ] , index + 1 ) ; else if ( mapping [ subList [ subList . size ( ) - 1 ] ] + 1 == mapping [ str [ index ] ] ) { string sub1 = longestSubsequence ( str , subList + str [ index ] , index + 1 ) ; string sub2 = longestSubsequence ( str , subList , index + 1 ) ; if ( sub1 . length ( ) > sub2 . length ( ) ) return sub1 ; else return sub2 ; } else return longestSubsequence ( str , subList , index + 1 ) ; } int main ( ) { string str = " aeiaaioooauuaeiou " ; string subsequence = longestSubsequence ( str , " " , 0 ) ; if ( subsequence . length ( ) == 0 ) cout << " No ▁ subsequence ▁ possible STRNEWLINE " ; else cout << subsequence << " STRNEWLINE " ; }
Concatenate suffixes of a String | C ++ implementation of the approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printExpansion ( string str ) { int size = 0 ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { string subStr = str . substr ( i , ++ size ) ; cout << subStr ; } } int main ( ) { string str = " geeks " ; printExpansion ( str ) ; return 0 ; }
Construct a binary string following the given constraints | C ++ implementation of the approach ; Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code
#include <iostream> NEW_LINE using namespace std ; int constructBinString ( int a , int b , int x ) { int d , i ; d = x / 2 ; if ( x % 2 == 0 && x / 2 != a ) { d -- ; cout << 0 ; a -- ; } for ( i = 0 ; i < d ; i ++ ) cout << "10" ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { cout << "1" ; } for ( i = 0 ; i < a ; i ++ ) { cout << "0" ; } } int main ( ) { int a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ; return 0 ; }
Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | C ++ implementation of the approach ; Function to match whether there are always n consecutive b ' s ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int matchPattern ( string s ) { int count = 0 ; int n = s . length ( ) ; int i = 0 ; while ( i < n ) { while ( i < n && s [ i ] == ' a ' ) { count ++ ; i ++ ; } while ( i < n && s [ i ] == ' b ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } int main ( ) { string s = " bb " ; if ( matchPattern ( s ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Length of longest consecutive ones by at most one swap in a Binary String | C ++ program to find length of longest consecutive ones by at most one swap in a Binary String ; Function to calculate the length of the longest consecutive 1 's ; To count all 1 's in the string ; To store cumulative 1 's ; Counting cumulative 1 's from left ; If 0 then start new cumulative one from that i ; perform step 3 of the approach ; step 3 ; Driver Code ; string
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum_one ( string s , int n ) { int cnt_one = 0 ; int max_cnt = 0 , temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { cnt_one ++ ; temp ++ ; } else { max_cnt = max ( temp , max_cnt ) ; temp = 0 ; } } max_cnt = max ( max_cnt , temp ) ; int left [ n ] , right [ n ] ; if ( s [ 0 ] == '1' ) left [ 0 ] = 1 ; else left [ 0 ] = 0 ; if ( s [ n - 1 ] == '1' ) right [ n - 1 ] = 1 ; else right [ n - 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) left [ i ] = left [ i - 1 ] + 1 ; else left [ i ] = 0 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) right [ i ] = right [ i + 1 ] + 1 ; else right [ i ] = 0 ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( s [ i ] == '0' ) { int sum = left [ i - 1 ] + right [ i + 1 ] ; if ( sum < cnt_one ) max_cnt = max ( max_cnt , sum + 1 ) ; else max_cnt = max ( max_cnt , sum ) ; } } return max_cnt ; } int main ( ) { string s = "111011101" ; cout << maximum_one ( s , s . length ( ) ) ; return 0 ; }
Maximum length substring with highest frequency in a string | C ++ program to find maximum occurred substring of a string ; function to return maximum occurred substring of a string ; size of the string ; to store maximum frequency ; to store string which has maximum frequency ; return substring which has maximum frequency ; Driver program ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string MaxFreq ( string str ) { int n = str . size ( ) ; unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) { string s = " " ; for ( int j = i ; j < n ; j ++ ) { s += str [ j ] ; m [ s ] ++ ; } } int maxi = 0 ; string s ; for ( auto i = m . begin ( ) ; i != m . end ( ) ; i ++ ) { if ( i -> second > maxi ) { maxi = i -> second ; s = i -> first ; } else if ( i -> second == maxi ) { string ss = i -> first ; if ( ss . size ( ) > s . size ( ) ) s = ss ; } } return s ; } int main ( ) { string str = " ababecdecd " ; cout << MaxFreq ( str ) ; return 0 ; }
Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | CPP program to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only ; Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxFreq ( string s , int a , int b ) { int fre [ 10 ] = { 0 } ; int n = s . size ( ) ; if ( a > b ) swap ( a , b ) ; for ( int i = 0 ; i < n ; i ++ ) fre [ s [ i ] - '0' ] ++ ; if ( fre [ a ] == 0 and fre [ b ] == 0 ) return -1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } int main ( ) { int a = 4 , b = 7 ; string s = "47744" ; cout << maxFreq ( s , a , b ) ; return 0 ; }
Minimum steps to convert one binary string to other only using negation | C ++ implementation of the above approach ; Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code
#include <iostream> NEW_LINE using namespace std ; void convert ( int n , string a , string b ) { int l [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) l [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) l [ i ] = 1 ; } int cc = 0 ; int vl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( l [ i ] == 0 ) { if ( vl != 0 ) cc += 1 ; vl = 0 ; } else vl += 1 ; } if ( vl != 0 ) cc += 1 ; cout << cc << endl ; } int main ( ) { string a = "101010" ; string b = "110011" ; int n = a . length ( ) ; convert ( n , a , b ) ; return 0 ; }
Generate a sequence with the given operations | C ++ Implementation of above approach ; function to find minimum required permutation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void StringMatch ( string s ) { int lo = 0 , hi = s . length ( ) , len = s . length ( ) ; vector < int > ans ; for ( int x = 0 ; x < len ; x ++ ) { if ( s [ x ] == ' I ' ) { ans . push_back ( lo ) ; lo += 1 ; } else { ans . push_back ( hi ) ; hi -= 1 ; } } ans . push_back ( lo ) ; cout << " [ " ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] ; if ( i != ans . size ( ) - 1 ) cout << " , " ; } cout << " ] " ; } int main ( ) { string S = " IDID " ; StringMatch ( S ) ; return 0 ; }
Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | C ++ program to find no of ways to swap bits of s1 so that bitwise OR of s1 and s2 changes ; Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countWays ( string s1 , string s2 , int n ) { int a , b , c , d ; a = b = c = d = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s2 [ i ] == '0' ) { if ( s1 [ i ] == '0' ) { c ++ ; } else { d ++ ; } } else { if ( s1 [ i ] == '0' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } int main ( ) { int n = 5 ; string s1 = "01011" ; string s2 = "11001" ; cout << countWays ( s1 , s2 , n ) ; return 0 ; }
Find the player who rearranges the characters to get a palindrome string first | C ++ program to print the winner of the game ; Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner
#include <bits/stdc++.h> NEW_LINE using namespace std ; int returnWinner ( string s , int l ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] & 1 ) cnt ++ ; } if ( cnt == 0 cnt & 1 ) return 1 ; else return 2 ; } int main ( ) { string s = " abaaab " ; int l = s . length ( ) ; int winner = returnWinner ( s , l ) ; cout << " Player - " << winner ; return 0 ; }
Maximum sum and product of the M consecutive digits in a number | C ++ implementation of the above approach ; Function to find the maximum product ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxProductSum ( string str , int m ) { int n = str . length ( ) ; int maxProd = INT_MIN , maxSum = INT_MIN ; for ( int i = 0 ; i <= n - m ; i ++ ) { int product = 1 , sum = 0 ; for ( int j = i ; j < m + i ; j ++ ) { product = product * ( str [ j ] - '0' ) ; sum = sum + ( str [ j ] - '0' ) ; } maxProd = max ( maxProd , product ) ; maxSum = max ( maxSum , sum ) ; } cout << " Maximum ▁ Product ▁ = ▁ " << maxProd ; cout << " Maximum Sum = " } int main ( ) { string str = "3605356297" ; int m = 3 ; maxProductSum ( str , m ) ; }
Replace all consonants with nearest vowels in a string | C ++ program to replace all consonants with nearest vowels in a string ; Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the absolute difference of ascii value is considered ; when s [ i ] is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; 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 replacingConsonants ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) continue ; else { if ( s [ i ] > ' a ' && s [ i ] < ' e ' ) { if ( abs ( s [ i ] - ' a ' ) > abs ( s [ i ] - ' e ' ) ) s [ i ] = ' e ' ; else s [ i ] = ' a ' ; } else if ( s [ i ] > ' e ' && s [ i ] < ' i ' ) { if ( abs ( s [ i ] - ' e ' ) > abs ( s [ i ] - ' i ' ) ) s [ i ] = ' i ' ; else s [ i ] = ' e ' ; } else if ( s [ i ] > ' i ' && s [ i ] < ' o ' ) { if ( abs ( s [ i ] - ' i ' ) > abs ( s [ i ] - ' o ' ) ) s [ i ] = ' o ' ; else s [ i ] = ' i ' ; } else if ( s [ i ] > ' o ' && s [ i ] < ' u ' ) { if ( abs ( s [ i ] - ' o ' ) > abs ( s [ i ] - ' u ' ) ) s [ i ] = ' u ' ; else s [ i ] = ' o ' ; } else if ( s [ i ] > ' u ' ) s [ i ] = ' u ' ; } } return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << replacingConsonants ( s ) ; return 0 ; }
Find time taken for signal to reach all positions in a string | C ++ program to Find time taken for signal to reach all positions in a string ; Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( string s , int n ) { int right = 0 , left = 0 ; int coun = 0 , max_length = INT_MIN ; s = s + '1' ; for ( int i = 0 ; i <= n ; i ++ ) { if ( s [ i ] == ' o ' ) coun ++ ; else { if ( coun > max_length ) { right = 0 ; left = 0 ; if ( s [ i ] == ' x ' ) right = 1 ; if ( ( ( i - coun ) > 0 ) && ( s [ i - coun - 1 ] == ' x ' ) ) left = 1 ; coun = ceil ( ( double ) coun / ( right + left ) ) ; max_length = max ( max_length , coun ) ; } coun = 0 ; } } return max_length ; } int main ( ) { string s = " oooxoooooooooxooo " ; int n = s . size ( ) ; cout << maxLength ( s , n ) ; return 0 ; }
Lexicographically largest string formed from the characters in range L and R | C ++ program to print the lexicographically largest string that can be formed from the characters in range L and R ; Function to return the lexicographically largest string ; hash array ; make 0 - based indexing ; iterate and count frequencies of character ; ans string ; iterate in frequency array ; add til all characters are added ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printLargestString ( string s , int l , int r ) { int freq [ 26 ] = { 0 } ; l -- ; r -- ; for ( int i = min ( l , r ) ; i <= max ( l , r ) ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } string ans = " " ; for ( int i = 25 ; i >= 0 ; i -- ) { while ( freq [ i ] ) { ans += char ( ' a ' + i ) ; freq [ i ] -- ; } } return ans ; } int main ( ) { string s = " striver " ; int l = 3 , r = 5 ; cout << printLargestString ( s , l , r ) ; return 0 ; }
Arrange a binary string to get maximum value within a range of indices | C ++ implementation of the approach ; Storing the count of 1 's in the string ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged string ; if after maximizing the ranges any 1 is left then we maximize the string lexicographically . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void arrange ( string s ) { int cc = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) cc ++ ; } int a [ s . length ( ) + 1 ] = { 0 } ; int qq [ ] [ 2 ] = { { 2 , 3 } , { 5 , 5 } } ; int n = sizeof ( qq ) / sizeof ( qq [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = qq [ i ] [ 0 ] , r = qq [ i ] [ 1 ] ; l -- , r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } int len_a = sizeof ( a ) / sizeof ( a [ 0 ] ) ; for ( int i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } int zz [ s . length ( ) ] = { 0 } ; for ( int i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( int i = 0 ; i < s . length ( ) ; i ++ ) cout << zz [ i ] ; cout << endl ; } int main ( ) { string str = "11100" ; arrange ( str ) ; return 0 ; }
Check whether the vowels in a string are in alphabetical order or not | C ++ implementation of above approach ; Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areVowelsInOrder ( string s ) { int n = s . length ( ) ; char c = ( char ) 64 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { if ( s [ i ] < c ) return false ; else { c = s [ i ] ; } } } return true ; } int main ( ) { string s = " aabbbddeecc " ; if ( areVowelsInOrder ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Rearrange the string to maximize the number of palindromic substrings | C ++ program to rearrange the string such to maximize the number of palindromic substrings ; Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string newString ( string s ) { int l = s . length ( ) ; int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] += 1 ; } string ans = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { ans += ( char ) ( 97 + i ) ; } } return ans ; } int main ( ) { string s = " aabab " ; cout << newString ( s ) ; return 0 ; }
Program to find remainder when large number is divided by r | CPP implementation to find Remainder when a large Number is divided by R ; Function to Return Remainder ; len is variable to store the length of Number string . ; loop that find Remainder ; Return the remainder ; Driver code ; Get the large number as string ; Get the divisor R ; Find and print the remainder
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Remainder ( string str , int R ) { int len = str . length ( ) ; int Num , Rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { Num = Rem * 10 + ( str [ i ] - '0' ) ; Rem = Num % R ; } return Rem ; } int main ( ) { string str = "13589234356546756" ; int R = 13 ; cout << Remainder ( str , R ) ; return 0 ; }