codeparrot/xlcost-text-to-code huggingface dataset:
Number of balanced bracket subsequence of length 2 and 4 | C ++ implementation of above approach ; Taking the frequency suffix sum of the number of 2 's present after every index ; Storing the count of subsequence ; Subsequence of length 2 ; Subsequence of length 4 of type 1 1 2 2 ; Subsequence of length 4 of type 1 2 1 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countWays ( int a [ ] , int n ) { int i , j ; long suff [ n ] ; if ( a [ n - 1 ] == 2 ) suff [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] == 2 ) suff [ i ] = suff [ i + 1 ] + 1 ; else suff [ i ] = suff [ i + 1 ] ; } long ss = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ss += suff [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && suff [ j ] >= 2 ) { ss += ( suff [ j ] ) * ( suff [ j ] - 1 ) / 2 ; } } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && ( suff [ i ] - suff [ j ] ) >= 1 && suff [ j ] >= 1 ) { ss += ( suff [ i ] - suff [ j ] ) * suff [ j ] ; } } } cout << ( ss ) ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 1 , 2 , 2 } ; int n = 6 ; countWays ( a , n ) ; return 0 ; }
Count the number of carry operations required to add two numbers | C ++ implementation of above approach ; Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_carry ( string a , string b ) { int carry = 0 ; int count = 0 ; int len_a = a . length ( ) , len_b = b . length ( ) ; while ( len_a != 0 len_b != 0 ) { int x = 0 , y = 0 ; if ( len_a > 0 ) { x = a [ len_a - 1 ] - '0' ; len_a -- ; } if ( len_b > 0 ) { y = b [ len_b - 1 ] - '0' ; len_b -- ; } int sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } int main ( ) { string a = "9555" , b = "555" ; int count = count_carry ( a , b ) ; if ( count == 0 ) cout << "0 STRNEWLINE " ; else if ( count == 1 ) cout << "1 STRNEWLINE " ; else cout << count << " STRNEWLINE " ; return 0 ; }
Check if a number is in given base or not | CPP program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver code
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool isInGivenBase ( string str , int base ) { if ( base > 16 ) return false ; else if ( base <= 10 ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( str [ i ] >= '0' and str [ i ] < ( '0' + base ) ) ) return false ; } else { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( ( str [ i ] >= '0' && str [ i ] < ( '0' + base ) ) || ( str [ i ] >= ' A ' && str [ i ] < ( ' A ' + base - 10 ) ) ) ) return false ; } return true ; } int main ( ) { string str = " AF87" ; if ( isInGivenBase ( str , 16 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find indices of all occurrence of one string in other | C ++ program to find indices of all occurrences of one string in other . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printIndex ( string str , string s ) { bool flag = false ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . substr ( i , s . length ( ) ) == s ) { cout << i << " ▁ " ; flag = true ; } } if ( flag == false ) cout << " NONE " ; } int main ( ) { string str1 = " GeeksforGeeks " ; string str2 = " Geeks " ; printIndex ( str1 , str2 ) ; return 0 ; }
Alternatively Merge two Strings in Java | C ++ code to alternatively merge two strings ; Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string merge ( string s1 , string s2 ) { string result = " " ; for ( int i = 0 ; i < s1 . length ( ) || i < s2 . length ( ) ; i ++ ) { if ( i < s1 . length ( ) ) result += s1 [ i ] ; if ( i < s2 . length ( ) ) result += s2 [ i ] ; } return result ; } int main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; cout << merge ( s1 , s2 ) ; return 0 ; }
Maximum occurring character in an input string | Set | C ++ implementation to find the maximum occurring character in an input string which is lexicographically first ; function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; to store the maximum occurring character ; length of ' str ' ; get frequency of each character of ' str ' ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update ' result ' ; maximum occurring character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char getMaxOccurringChar ( char str [ ] ) { int freq [ 26 ] = { 0 } ; int max = -1 ; char result ; int len = strlen ( str ) ; for ( int i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( max < freq [ i ] ) { max = freq [ i ] ; result = ( char ) ( i + ' a ' ) ; } return result ; } int main ( ) { char str [ ] = " sample ▁ program " ; cout << " Maximum ▁ occurring ▁ character ▁ = ▁ " << getMaxOccurringChar ( str ) ; return 0 ; }
Check if the given string of words can be formed from words present in the dictionary | C ++ program to check if a sentence can be formed from a given set of words . ; here isEnd is an integer that will store count of words ending at that node ; utility function to create a new node ; Initialize new node with null ; Function to insert new words in trie ; Iterate for the length of a word ; If the next key does not contains the character ; isEnd is increment so not only the word but its count is also stored ; Search function to find a word of a sentence ; Iterate for the complete length of the word ; If the character is not present then word is also not present ; If present move to next character in Trie ; If word foundthen decrement count of the word ; if the word is found decrement isEnd showing one occurrence of this word is already taken so ; Function to check if string can be formed from the sentence ; Iterate for all words in the string ; if a word is not found in a string then the sentence cannot be made from this dictionary of words ; If possible ; Function to insert all the words of dictionary in the Trie ; Driver Code ; Dictionary of words ; Calling Function to insert words of dictionary to tree ; String to be checked ; Function call to check possibility
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int ALPHABET_SIZE = 26 ; struct trieNode { trieNode * t [ ALPHABET_SIZE ] ; int isEnd ; } ; trieNode * getNode ( ) { trieNode * temp = new ( trieNode ) ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) temp -> t [ i ] = NULL ; temp -> isEnd = 0 ; return temp ; } void insert ( trieNode * root , string key ) { trieNode * trail ; trail = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { if ( trail -> t [ key [ i ] - ' a ' ] == NULL ) { trieNode * temp ; temp = getNode ( ) ; trail -> t [ key [ i ] - ' a ' ] = temp ; } trail = trail -> t [ key [ i ] - ' a ' ] ; } ( trail -> isEnd ) ++ ; } bool search_mod ( trieNode * root , string word ) { trieNode * trail ; trail = root ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { if ( trail -> t [ word [ i ] - ' a ' ] == NULL ) return false ; trail = trail -> t [ word [ i ] - ' a ' ] ; } if ( ( trail -> isEnd ) > 0 && trail != NULL ) { ( trail -> isEnd ) -- ; return true ; } else return false ; } void checkPossibility ( string sentence [ ] , int m , trieNode * root ) { int flag = 1 ; for ( int i = 0 ; i < m ; i ++ ) { if ( search_mod ( root , sentence [ i ] ) == false ) { cout << " NO " ; return ; } } cout << " YES " ; } void insertToTrie ( string dictionary [ ] , int n , trieNode * root ) { for ( int i = 0 ; i < n ; i ++ ) insert ( root , dictionary [ i ] ) ; } int main ( ) { trieNode * root ; root = getNode ( ) ; string dictionary [ ] = { " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " } ; int N = sizeof ( dictionary ) / sizeof ( dictionary [ 0 ] ) ; insertToTrie ( dictionary , N , root ) ; string sentence [ ] = { " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " } ; int M = sizeof ( sentence ) / sizeof ( sentence [ 0 ] ) ; checkPossibility ( sentence , M , root ) ; return 0 ; }
Given two numbers as strings , find if one is a power of other | CPP program to check if one number is power of other ; Multiply the numbers . It multiplies each digit of second string to each digit of first and stores the result . ; If the digit exceeds 9 , add the cumulative carry to previous digit . ; if all zeroes , return "0" . ; Remove starting zeroes . ; Removes Extra zeroes from front of a string . ; Make sure there are no leading zeroes in the string . ; Making sure that s1 is smaller . If it is greater , we recur we reversed parameters . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isGreaterThanEqualTo ( string s1 , string s2 ) { if ( s1 . size ( ) > s2 . size ( ) ) return true ; return ( s1 == s2 ) ; } string multiply ( string s1 , string s2 ) { int n = s1 . size ( ) ; int m = s2 . size ( ) ; vector < int > result ( n + m , 0 ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) for ( int j = m - 1 ; j >= 0 ; j -- ) result [ i + j + 1 ] += ( s1 [ i ] - '0' ) * ( s2 [ j ] - '0' ) ; int size = result . size ( ) ; for ( int i = size - 1 ; i > 0 ; i -- ) { if ( result [ i ] >= 10 ) { result [ i - 1 ] += result [ i ] / 10 ; result [ i ] = result [ i ] % 10 ; } } int i = 0 ; while ( i < size && result [ i ] == 0 ) i ++ ; if ( i == size ) return "0" ; string temp ; while ( i < size ) { temp += ( result [ i ] + '0' ) ; i ++ ; } return temp ; } string removeLeadingZeores ( string s ) { int n = s . size ( ) ; int i = 0 ; while ( i < n && s [ i ] == '0' ) i ++ ; if ( i == n ) return "0" ; string temp ; while ( i < n ) temp += s [ i ++ ] ; return temp ; } bool isPower ( string s1 , string s2 ) { s1 = removeLeadingZeores ( s1 ) ; s2 = removeLeadingZeores ( s2 ) ; if ( s1 == "0" s2 == "0" ) return false ; if ( s1 == "1" && s2 == "1" ) return true ; if ( s1 == "1" s2 == "1" ) return true ; if ( s1 . size ( ) > s2 . size ( ) ) return isPower ( s2 , s1 ) ; string temp = s1 ; while ( ! isGreaterThanEqualTo ( s1 , s2 ) ) s1 = multiply ( s1 , temp ) ; return s1 == s2 ; } int main ( ) { string s1 = "374747" , s2 = "52627712618930723" ; cout << ( isPower ( s1 , s2 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ) ; s1 = "4099" , s2 = "2" ; cout << ( isPower ( s1 , s2 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ) ; return 0 ; }
Check for balanced parentheses in an expression | O ( 1 ) space | C ++ code to check balanced parentheses with O ( 1 ) space . ; Function1 to match closing bracket ; Function1 to match opening bracket ; Function to check balanced parentheses ; helper variables ; Handling case of opening parentheses ; Handling case of closing parentheses ; If corresponding matching opening parentheses doesn 't  lie in given interval return 0 ; else continue ; If corresponding closing parentheses doesn 't lie in given interval  return 0 ; if found , now check for each opening and closing parentheses in this interval ; Driver Code
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int matchClosing ( char X [ ] , int start , int end , char open , char close ) { int c = 1 ; int i = start + 1 ; while ( i <= end ) { if ( X [ i ] == open ) c ++ ; else if ( X [ i ] == close ) c -- ; if ( c == 0 ) return i ; i ++ ; } return i ; } int matchingOpening ( char X [ ] , int start , int end , char open , char close ) { int c = -1 ; int i = end - 1 ; while ( i >= start ) { if ( X [ i ] == open ) c ++ ; else if ( X [ i ] == close ) c -- ; if ( c == 0 ) return i ; i -- ; } return -1 ; } bool isBalanced ( char X [ ] , int n ) { int i , j , k , x , start , end ; for ( i = 0 ; i < n ; i ++ ) { if ( X [ i ] == ' ( ' ) j = matchClosing ( X , i , n - 1 , ' ( ' , ' ) ' ) ; else if ( X [ i ] == ' { ' ) j = matchClosing ( X , i , n - 1 , ' { ' , ' } ' ) ; else if ( X [ i ] == ' [ ' ) j = matchClosing ( X , i , n - 1 , ' [ ' , ' ] ' ) ; else { if ( X [ i ] == ' ) ' ) j = matchingOpening ( X , 0 , i , ' ( ' , ' ) ' ) ; else if ( X [ i ] == ' } ' ) j = matchingOpening ( X , 0 , i , ' { ' , ' } ' ) ; else if ( X [ i ] == ' ] ' ) j = matchingOpening ( X , 0 , i , ' [ ' , ' ] ' ) ; if ( j < 0 j > = i ) return false ; continue ; } if ( j >= n j < 0 ) return false ; start = i ; end = j ; for ( k = start + 1 ; k < end ; k ++ ) { if ( X [ k ] == ' ( ' ) { x = matchClosing ( X , k , end , ' ( ' , ' ) ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ) ' ) { x = matchingOpening ( X , start , k , ' ( ' , ' ) ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' { ' ) { x = matchClosing ( X , k , end , ' { ' , ' } ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' } ' ) { x = matchingOpening ( X , start , k , ' { ' , ' } ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' [ ' ) { x = matchClosing ( X , k , end , ' [ ' , ' ] ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ] ' ) { x = matchingOpening ( X , start , k , ' [ ' , ' ] ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } } } return true ; } int main ( ) { char X [ ] = " [ ( ) ] ( ) " ; int n = 6 ; if ( isBalanced ( X , n ) ) printf ( " Yes STRNEWLINE " ) ; else printf ( " No STRNEWLINE " ) ; char Y [ ] = " [ [ ( ) ] ] ) " ; n = 7 ; if ( isBalanced ( Y , n ) ) printf ( " Yes STRNEWLINE " ) ; else printf ( " No STRNEWLINE " ) ; return 0 ; }
Sorting array with conditional swapping | CPP program to Check if it is possible to sort the array in ascending order . ; Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string possibleToSort ( int * arr , int n , string str ) { int max_element = -1 ; for ( long i = 0 ; i < str . size ( ) ; i ++ ) { max_element = max ( max_element , arr [ i ] ) ; if ( str [ i ] == '0' ) { if ( max_element > i + 1 ) return " No " ; } } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string str = "01110" ; cout << possibleToSort ( arr , n , str ) ; return 0 ; }
Find sub | C ++ program to find substring with given power ; Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; If curr_power is equal to target power we found a substring starting from index 0 and ending at index i . ; If curr_power - power already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Drivers code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void findSubstring ( string str , ll power ) { ll i ; unordered_map < ll , ll > map ; int curr_power = 0 ; int len = str . length ( ) ; for ( i = 0 ; i < len ; i ++ ) { curr_power = curr_power + ( str [ i ] - ' a ' + 1 ) ; if ( curr_power == power ) { cout << " Substring ▁ from ▁ index ▁ " << 0 << " ▁ to ▁ " << i << " ▁ has ▁ power ▁ " << power << endl ; return ; } if ( map . find ( curr_power - power ) != map . end ( ) ) { cout << " Substring ▁ from ▁ index ▁ " << map [ curr_power - power ] + 1 << " ▁ to ▁ " << i << " ▁ has ▁ power ▁ " << power << endl ; return ; } map [ curr_power ] = i ; } cout << " No ▁ substring ▁ with ▁ given ▁ power ▁ exists . " ; } int main ( ) { string str = " geeksforgeeks " ; ll power = 36 ; findSubstring ( str , power ) ; return 0 ; }
Prime String | C ++ program to find if string is a Prime or not . ; Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrimeString ( string str ) { int len = str . length ( ) , n = 0 ; for ( int i = 0 ; i < len ; i ++ ) n += ( int ) str [ i ] ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int main ( ) { string str = " geekRam " ; if ( isPrimeString ( str ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }
Ways to split string such that each partition starts with distinct character | CPP Program to find number of way to split string such that each partition starts with distinct character with maximum number of partitions . ; Returns the number of we can split the string ; Finding the frequency of each character . ; making frequency of first character of string equal to 1. ; Finding the product of frequency of occurrence of each character . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( string s ) { int count [ 26 ] = { 0 } ; for ( char x : s ) count [ x - ' a ' ] ++ ; count [ s [ 0 ] - ' a ' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } int main ( ) { string s = " acbbcc " ; cout << countWays ( s ) << endl ; return 0 ; }
Lexicographically next greater string using same character set | C ++ implementation of above algorithm . ; function to print output ; to store unique characters of the string ; to check uniqueness ; if mp [ s [ i ] ] = 0 then it is first time ; sort the unique characters ; simply add n - k smallest characters ; return ; end the program ; searching the first character left of index k and not equal to greatest character of the string ; finding the just next greater character than s [ i ] ; suffix with smallest character ; if we reach here then all indices to the left of k had the greatest character ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lexoString ( string s , int k ) { int n = s . size ( ) ; vector < char > v ; map < char , int > mp ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( mp [ s [ i ] ] == 0 ) { mp [ s [ i ] ] = 1 ; v . push_back ( s [ i ] ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; if ( k > n ) { cout << s ; for ( int i = n ; i < k ; i ++ ) { cout << v [ 0 ] ; } } for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != v [ v . size ( ) - 1 ] ) { for ( int j = 0 ; j < i ; j ++ ) { cout << s [ j ] ; } for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v [ j ] > s [ i ] ) { cout << v [ j ] ; break ; } } for ( int j = i + 1 ; j < k ; j ++ ) cout << v [ 0 ] ; return ; } } cout << " No ▁ lexicographically ▁ greater ▁ string ▁ of ▁ length ▁ " << k << " ▁ possible ▁ here . " ; } int main ( ) { string s = " gi " ; int k = 3 ; lexoString ( s , k ) ; return 0 ; }
Number of palindromic permutations | Set 1 | CPP program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 256 ; long long int fact ( int n ) { long long int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int countPalinPermutations ( string & str ) { int n = str . length ( ) ; int freq [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] ] ++ ; long long int res = fact ( n / 2 ) ; bool oddFreq = false ; for ( int i = 0 ; i < MAX ; i ++ ) { int half = freq [ i ] / 2 ; if ( freq [ i ] % 2 != 0 ) { if ( oddFreq == true ) return 0 ; oddFreq = true ; } res = res / fact ( half ) ; } return res ; } int main ( ) { string str = " gffg " ; cout << countPalinPermutations ( str ) ; return 0 ; }
Rearrange characters to form palindrome if possible | C ++ program to rearrange a string to make palindrome . ; Store counts of characters ; find the number of odd elements . Takes O ( n ) ; odd_cnt = 1 only if the length of str is odd ; Generate first halh of palindrome ; Build a string of floor ( count / 2 ) occurrences of current character ; Attach the built string to end of and begin of second half ; Insert odd character if there is any ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getPalindrome ( string str ) { unordered_map < char , int > hmap ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) hmap [ str [ i ] ] ++ ; int oddCount = 0 ; char oddChar ; for ( auto x : hmap ) { if ( x . second % 2 != 0 ) { oddCount ++ ; oddChar = x . first ; } } if ( oddCount > 1 || oddCount == 1 && str . length ( ) % 2 == 0 ) return " NO ▁ PALINDROME " ; string firstHalf = " " , secondHalf = " " ; for ( auto x : hmap ) { string s ( x . second / 2 , x . first ) ; firstHalf = firstHalf + s ; secondHalf = s + secondHalf ; } return ( oddCount == 1 ) ? ( firstHalf + oddChar + secondHalf ) : ( firstHalf + secondHalf ) ; } int main ( ) { string s = " mdaam " ; cout << getPalindrome ( s ) ; return 0 ; }
Substrings starting with vowel and ending with consonants and vice versa | CPP program to count special strings ; Returns true if ch is vowel ; function to check consonant ; in case of empty string , we can 't fullfill the  required condition, hence we return ans as 0. ; co [ i ] is going to store counts of consonants from str [ len - 1 ] to str [ i ] . vo [ i ] is going to store counts of vowels from str [ len - 1 ] to str [ i ] . ; Counting consonants and vowels from end of string . ; Now we traverse string from beginning ; If vowel , then count of substrings starting with str [ i ] is equal to count of consonants after it . ; If consonant , then count of substrings starting with str [ i ] is equal to count of vowels after it . ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } bool isCons ( char ch ) { return ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) ; } int countSpecial ( string & str ) { int len = str . length ( ) ; if ( len == 0 ) return 0 ; int co [ len + 1 ] ; int vo [ len + 1 ] ; memset ( co , 0 , sizeof ( co ) ) ; memset ( vo , 0 , sizeof ( vo ) ) ; if ( isCons ( str [ len - 1 ] ) == 1 ) co [ len - 1 ] = 1 ; else vo [ len - 1 ] = 1 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( isCons ( str [ i ] ) == 1 ) { co [ i ] = co [ i + 1 ] + 1 ; vo [ i ] = vo [ i + 1 ] ; } else { co [ i ] = co [ i + 1 ] ; vo [ i ] = vo [ i + 1 ] + 1 ; } } long long ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) ans = ans + co [ i + 1 ] ; else ans = ans + vo [ i + 1 ] ; } return ans ; } int main ( ) { string str = " adceba " ; cout << countSpecial ( str ) ; return 0 ; }