codeparrot/xlcost-text-to-code huggingface dataset:
Minimum replacements such that the difference between the index of the same characters is divisible by 3 | C ++ program to find minimum replacements such that the difference between the index of the same characters is divisible by 3 ; Function to count the number of minimal replacements ; Generate all permutations ; Count the replacements ; Return the replacements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinimalReplacements ( string s ) { int n = s . length ( ) ; int mini = INT_MAX ; string dup = "012" ; do { int dif = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] != dup [ i % 3 ] ) dif ++ ; mini = min ( mini , dif ) ; } while ( next_permutation ( dup . begin ( ) , dup . end ( ) ) ) ; return mini ; } int main ( ) { string s = "2101200" ; cout << countMinimalReplacements ( s ) ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the count of sub - strings of str that are divisible by k ; Take all sub - strings starting from i ; If current sub - string is divisible by k ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubStr ( string str , int len , int k ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int n = 0 ; for ( int j = i ; j < len ; j ++ ) { n = n * 10 + ( str [ j ] - '0' ) ; if ( n % k == 0 ) count ++ ; } } return count ; } int main ( ) { string str = "33445" ; int len = str . length ( ) ; int k = 11 ; cout << countSubStr ( str , len , k ) ; return 0 ; }
Find the resulting Colour Combination | C ++ program to find the resultant colour combination ; Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code
#include <iostream> NEW_LINE using namespace std ; char Colour_Combination ( string s ) { char temp = s [ 0 ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( temp != s [ i ] ) { if ( ( temp == ' B ' temp == ' G ' ) && ( s [ i ] == ' G ' s [ i ] == ' B ' ) ) temp = ' Y ' ; else if ( ( temp == ' B ' temp == ' Y ' ) && ( s [ i ] == ' Y ' s [ i ] == ' B ' ) ) temp = ' G ' ; else temp = ' B ' ; } } return temp ; } int main ( int argc , char * * argv ) { string s = " GBYGB " ; cout << Colour_Combination ( s ) ; }
Reverse Middle X Characters | C ++ implementation of the approach ; Function to reverse the middle x characters in a string ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Driver code
#include <iostream> NEW_LINE using namespace std ; void reverse ( string str , int x ) { int n = ( str . length ( ) - x ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) cout << str [ i ] ; for ( int i = n + x - 1 ; i >= n ; i -- ) cout << str [ i ] ; for ( int i = n + x ; i < str . length ( ) ; i ++ ) cout << str [ i ] ; } int main ( ) { string str = " geeksforgeeks " ; int x = 3 ; reverse ( str , x ) ; return 0 ; }
Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | CPP program to Minimize the number of replacements to get a string with same number of a , b and c in it ; Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' b ' and ;c; ; Check if it is ' b ' and it more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' c ' ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of b 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' b ' and it more than n / 3 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexoSmallest ( string s , int n ) { int ca = 0 , cb = 0 , cc = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' ) ca ++ ; else if ( s [ i ] == ' b ' ) cb ++ ; else cc ++ ; } if ( ca == cb && cb == cc ) { return s ; } int cnt = n / 3 ; if ( cnt * 3 != n ) { return " - 1" ; } int i = 0 ; while ( ca < cnt && i < n ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' a ' ; ca ++ ; } else if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = ' a ' ; ca ++ ; } i ++ ; } i = 0 ; while ( cb < cnt && i < n ) { if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = '1' ; cb ++ ; } i ++ ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } i = n - 1 ; while ( cb < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' b ' ; cb ++ ; } i -- ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } return s ; } int main ( ) { string s = " aaaaaa " ; int n = s . size ( ) ; cout << lexoSmallest ( s , n ) ; return 0 ; }
Minimum moves to reach from i to j in a cyclic string | C ++ implementation of the approach ; Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft ; Return the minimum of two paths ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSteps ( string str , int i , int j , int n ) { int k = i + 1 ; int steps = 0 ; char ch = str [ i ] ; while ( k <= j ) { if ( str [ k ] != ch ) { steps ++ ; ch = str [ k ] ; } k ++ ; } return steps ; } int getMinSteps ( string str , int i , int j , int n ) { if ( j < i ) { int temp = i ; i = j ; j = temp ; } int stepsToRight = getSteps ( str , i , j , n ) ; int stepsToLeft = getSteps ( str , 0 , i , n ) + getSteps ( str , j , n - 1 , n ) ; if ( str [ 0 ] != str [ n - 1 ] ) stepsToLeft ++ ; return min ( stepsToLeft , stepsToRight ) ; } int main ( ) { string str = " SSNSS " ; int n = str . length ( ) ; int i = 0 , j = 3 ; cout << getMinSteps ( str , i , j , n ) ; return 0 ; }
Count distinct substrings that contain some characters at most k times | C ++ implementation of the approach ; Function to return the count of valid sub - strings ; Store all characters of anotherStr in a direct index table for quick lookup . ; To store distinct output substrings ; Traverse through the given string and one by one generate substrings beginning from s [ i ] . ; One by one generate substrings ending with s [ j ] ; If character is illegal ; If current substring is valid ; If current substring is invalid , adding more characters would not help . ; Return the count of distinct sub - strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; int countSubStrings ( string s , string anotherStr , int k ) { bool illegal [ MAX_CHAR ] = { false } ; for ( int i = 0 ; i < anotherStr . size ( ) ; i ++ ) illegal [ anotherStr [ i ] ] = true ; unordered_set < string > us ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { string ss = " " ; int count = 0 ; for ( int j = i ; j < s . size ( ) ; ++ j ) { if ( illegal [ s [ j ] ] ) ++ count ; ss = ss + s [ j ] ; if ( count <= k ) { us . insert ( ss ) ; } else break ; } } return us . size ( ) ; } int main ( ) { string str = " acbacbacaa " ; string anotherStr = " abcdefghijklmnopqrstuvwxyz " ; int k = 2 ; cout << countSubStrings ( str , anotherStr , k ) ; return 0 ; }
Count pairs of parentheses sequences such that parentheses are balanced | C ++ program to count the number of pairs of balanced parentheses ; Function to count the number of pairs ; Hashing function to count the opening and closing brackets ; Traverse for all bracket sequences ; Get the string ; Counts the opening and closing required ; Traverse in the string ; If it is a opening bracket ; else Closing bracket ; If openings are there , then close it ; else Else increase count of closing ; If requirements of openings are there and no closing ; If requirements of closing are there and no opening ; Perfect ; Divide by two since two perfect makes one pair ; Traverse in the open and find corresponding minimum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( string bracks [ ] , int num ) { unordered_map < int , int > open , close ; int cnt = 0 ; for ( int i = 0 ; i < num ; i ++ ) { string s = bracks [ i ] ; int l = s . length ( ) ; int op = 0 , cl = 0 ; for ( int j = 0 ; j < l ; j ++ ) { if ( s [ j ] == ' ( ' ) op ++ ; { if ( op ) op -- ; cl ++ ; } } if ( op && ! cl ) open [ op ] ++ ; if ( cl && ! op ) close [ cl ] ++ ; if ( ! op && ! cl ) cnt ++ ; } cnt = cnt / 2 ; for ( auto it : open ) cnt += min ( it . second , close [ it . first ] ) ; return cnt ; } int main ( ) { string bracks [ ] = { " ) ( ) ) " , " ) " , " ( ( " , " ( ( " , " ( " , " ) " , " ) " } ; int num = sizeof ( bracks ) / sizeof ( bracks [ 0 ] ) ; cout << countPairs ( bracks , num ) ; }
Decrypt a string according to given rules | C ++ program to decrypt the original string ; Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decrypt ( string s , int l ) { string ans = " " ; if ( l % 2 ) { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . size ( ) != l ) { if ( cnt % 2 == 0 ) ans += s [ indl ++ ] ; else ans += s [ indr -- ] ; cnt ++ ; } } else { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . size ( ) != l ) { if ( cnt % 2 == 0 ) ans += s [ indr -- ] ; else ans += s [ indl ++ ] ; cnt ++ ; } } reverse ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } int main ( ) { string s = " segosegekfrek " ; int l = s . length ( ) ; cout << decrypt ( s , l ) ; return 0 ; }
Remove consecutive alphabets which are in same case | C ++ program to remove the consecutive characters from a string that are in same case ; Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeChars ( string s ) { string modifiedStr = " " ; modifiedStr += s [ 0 ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( isupper ( s [ i ] ) && islower ( s [ i - 1 ] ) || islower ( s [ i ] ) && isupper ( s [ i - 1 ] ) ) modifiedStr += s [ i ] ; } return modifiedStr ; } int main ( ) { string s = " GeeksForGeeks " ; cout << removeChars ( s ) ; return 0 ; }
Cost to make a string Panagram | C ++ program to find the cost to make a string Panagram ; Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pangramCost ( int arr [ ] , string str ) { int cost = 0 ; bool occurred [ 26 ] = { false } ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) occurred [ str [ i ] - ' a ' ] = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( ! occurred [ i ] ) cost += arr [ i ] ; } return cost ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 } ; string str = " abcdefghijklmopqrstuvwz " ; cout << pangramCost ( arr , str ) ; return 0 ; }
Recursive program to insert a star between pair of identical characters | Recursive CPP program to insert * between two consecutive same characters . ; Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append ' * ' ; Driver code
#include <iostream> NEW_LINE using namespace std ; void pairStar ( string & input , string & output , int i = 0 ) { output = output + input [ i ] ; if ( i == input . length ( ) - 1 ) return ; if ( input [ i ] == input [ i + 1 ] ) output = output + ' * ' ; pairStar ( input , output , i + 1 ) ; } int main ( ) { string input = " geeks " , output = " " ; pairStar ( input , output ) ; cout << output << endl ; return 0 ; }
Maximum number of removals of given subsequence from a string | C ++ implementation of the approach ; Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOperations ( string str ) { int i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' g ' ) { g ++ ; } else if ( str [ i ] == ' k ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str [ i ] == ' s ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } int main ( ) { string a = " ggkssk " ; cout << maxOperations ( a ) ; return 0 ; }
Split a string in equal parts such that all parts are palindromes | C ++ implementation of the approach ; Function to return the frequency array for the given string ; Function to return the required count ; Add frequencies of the even appearing characters ; Count of the characters that appeared odd number of times ; If there are no characters with odd frequency ; If there are no characters with even frequency ; Only a single character with odd frequency ; More than 1 character with odd frequency string isn 't a palindrome ; All odd appearing characters can also contribute to the even length palindrome if one character is removed from the frequency leaving it as even ; If k palindromes are possible where k is the number of characters with odd frequency ; Current character can no longer be an element in a string other than the mid character ; If current character has odd frequency > 1 take two characters which can be used in any of the parts ; Update the frequency ; If not possible , then every character of the string will act as a separate palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * getFrequencies ( string str ) { static int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } return freq ; } int countMinParts ( string str ) { int n = str . length ( ) ; int * freq = getFrequencies ( str ) ; vector < int > oddFreq , evenFreq ; int i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . push_back ( freq [ i ] ) ; else oddFreq . push_back ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . size ( ) ; i ++ ) { sumEven += evenFreq [ i ] ; } if ( oddFreq . size ( ) == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . size ( ) == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . size ( ) ) { if ( ( sumEven / 2 ) % oddFreq . size ( ) == 0 ) return oddFreq . size ( ) ; if ( oddFreq [ i ] == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq [ i ] = oddFreq [ i ] - 2 ; } return n ; } int main ( ) { string s = " noonpeep " ; cout << countMinParts ( s ) ; }
Substring Reverse Pattern | C ++ program to print the required pattern ; Function to print the required pattern ; Print the unmodified string ; Reverse the string ; Replace the first and last character by ' * ' then second and second last character and so on until the string has characters remaining ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPattern ( char s [ ] , int n ) { cout << s << " STRNEWLINE " ; int i = 0 , j = n - 2 ; while ( i < j ) { char c = s [ i ] ; s [ i ] = s [ j ] ; s [ j ] = c ; i ++ ; j -- ; } i = 0 ; j = n - 2 ; while ( j - i > 1 ) { s [ i ] = s [ j ] = ' * ' ; cout << s << " STRNEWLINE " ; i ++ ; j -- ; } } int main ( ) { char s [ ] = " geeks " ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; printPattern ( s , n ) ; return 0 ; }
Check if the string satisfies the given condition | C ++ implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if c is a vowel ; Function that returns true if the count of vowels in word is prime ; If count of vowels is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; bool prime ( int n ) { 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 ; } bool isVowel ( char c ) { c = tolower ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } bool isValidString ( string word ) { int cnt = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { if ( isVowel ( word [ i ] ) ) cnt ++ ; } if ( prime ( cnt ) ) return true ; else return false ; } int main ( ) { string s = " geeksforgeeks " ; if ( isValidString ( s ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
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 ) { string suff = " " ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { suff = suff + str [ i ] ; cout << suff ; } } int main ( ) { string str = " geeks " ; printExpansion ( str ) ; return 0 ; }
Solve the Logical Expression given by string | C ++ program to solve the logical expression . ; Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char logicalExpressionEvaluation ( string str ) { stack < char > arr ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' [ ' ) { vector < char > s ; while ( arr . top ( ) != ' ] ' ) { s . push_back ( arr . top ( ) ) ; arr . pop ( ) ; } arr . pop ( ) ; if ( s . size ( ) == 3 ) { s [ 2 ] == '1' ? arr . push ( '0' ) : arr . push ( '1' ) ; } else if ( s . size ( ) == 5 ) { int a = s [ 0 ] - 48 , b = s [ 4 ] - 48 , c ; s [ 2 ] == ' & ' ? c = a && b : c = a || b ; arr . push ( ( char ) c + 48 ) ; } } else { arr . push ( str [ i ] ) ; } } return arr . top ( ) ; } int main ( ) { string str = " [ [0 , & ,1 ] , | , [ ! ,1 ] ] " ; cout << logicalExpressionEvaluation ( str ) << endl ; return 0 ; }
Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | C ++ program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string s , int k ) { int n = s . length ( ) ; int d = 0 , i ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s [ i ] ; if ( d % k == 0 ) count += 1 ; for ( i = k ; i < n ; i ++ ) { int prev = s [ i - k ] ; d -= prev ; d += s [ i ] ; if ( d % k == 0 ) count += 1 ; } return count ; } int main ( ) { string s = " bcgabc " ; int k = 3 ; int ans = count ( s , k ) ; cout << ( ans ) ; }
Final string after performing given operations | C ++ implementation of the approach ; Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printFinalString ( string s ) { int i , n ; n = s . length ( ) ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' x ' ) x ++ ; else y ++ ; } string finalString = " " ; if ( x > y ) for ( i = 0 ; i < x - y ; i ++ ) finalString += " x " ; else for ( i = 0 ; i < y - x ; i ++ ) finalString += " y " ; return finalString ; } int main ( ) { string s = " xxyyxyy " ; cout << printFinalString ( s ) ; }
String which when repeated exactly K times gives a permutation of S | C ++ program to find a string which when repeated exactly k times gives a permutation of the given string ; Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string K_String ( string s , int k ) { int n = s . size ( ) ; int fre [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) fre [ s [ i ] - ' a ' ] ++ ; string str = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { int x = fre [ i ] / k ; while ( x -- ) { str += ( char ) ( i + ' a ' ) ; } } else { return " - 1" ; } } return str ; } int main ( ) { string s = " aabb " ; int k = 2 ; cout << K_String ( s , k ) ; return 0 ; }
Maximum even length sub | C ++ code to find the maximum length of sub - string ( of even length ) which can be arranged into a Palindrome ; function that returns true if the given sub - string can be arranged into a Palindrome ; This function returns the maximum length of the sub - string ( of even length ) which can be arranged into a Palindrome ; If we reach end of the string ; if string is of even length ; if string can be arranged into a palindrome ; Even length sub - string ; Check if current sub - string can be arranged into a palindrome ; Odd length sub - string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < int , int > countt ; bool canBePalindrome ( unordered_map < int , int > & countt ) { for ( auto key : countt ) { if ( key . second & 1 ) return false ; } return true ; } int maxPal ( string str , unordered_map < int , int > & countt , int start , int end ) { if ( end == str . length ( ) ) { if ( ( end - start ) % 2 == 0 ) if ( canBePalindrome ( countt ) ) return end - start ; return 0 ; } else { if ( ( end - start ) % 2 == 0 ) { if ( canBePalindrome ( countt ) ) { countt [ str [ end ] ] ++ ; return max ( end - start , maxPal ( str , countt , start , end + 1 ) ) ; } else { countt [ str [ end ] ] ++ ; return maxPal ( str , countt , start , end + 1 ) ; } } else { countt [ str [ end ] ] ++ ; unordered_map < int , int > c ( countt . begin ( ) , countt . end ( ) ) ; int length = maxPal ( str , c , start , end + 1 ) ; countt [ str [ end ] ] -- ; countt [ str [ start ] ] -- ; return max ( length , maxPal ( str , countt , start + 1 , end ) ) ; } } } int main ( int argc , char const * argv [ ] ) { string str = "124565463" ; int start = 0 , end = 0 ; cout << maxPal ( str , countt , start , end ) << endl ; return 0 ; }