codeparrot/xlcost-text-to-code huggingface dataset:
Find the last remaining Character in the Binary String according to the given conditions | C ++ implementation of the above approach ; Delete counters for each to count the deletes ; Counters to keep track of characters left from each type ; Queue to simulate the process ; Initializing the queue ; Looping till at least 1 digit is left from both the type ; If there is a floating delete for current character we will delete it and move forward otherwise we will increase delete counter for opposite digit ; If 0 are left then answer is 0 else answer is 1 ; Driver Code ; Input String ; Length of String ; Printing answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; string remainingDigit ( string S , int N ) { int del [ ] = { 0 , 0 } ; int count [ ] = { 0 , 0 } ; queue < int > q ; for ( int i = 0 ; i < N ; i ++ ) { int x = S [ i ] == '1' ? 1 : 0 ; count [ x ] ++ ; q . push ( x ) ; } while ( count [ 0 ] > 0 && count [ 1 ] > 0 ) { int t = q . front ( ) ; q . pop ( ) ; if ( del [ t ] > 0 ) { del [ t ] -- ; count [ t ] -- ; } else { del [ t ^ 1 ] ++ ; q . push ( t ) ; } } if ( count [ 0 ] > 0 ) return "0" ; return "1" ; } int main ( ) { string S = "1010100100000" ; int N = S . length ( ) ; cout << remainingDigit ( S , N ) ; }
Closest leaf to a given node in Binary Tree | Find closest leaf to the given node x in a tree ; A Tree node ; Utility function to create a new node ; This function finds closest leaf to root . This distance is stored at * minDist . ; base case ; If this is a leaf node , then check if it is closer than the closest so far ; Recur for left and right subtrees ; This function finds if there is closer leaf to x through parent node . ; Base cases ; Search x in left subtree of root ; If left subtree has x ; Find closest leaf in right subtree ; Search x in right subtree of root ; If right subtree has x ; Find closest leaf in left subtree ; Returns minimum distance of a leaf from given node x ; Initialize result ( minimum distance from a leaf ) ; Find closest leaf down to x ; See if there is a closer leaf through parent ; Driver program ; Let us create Binary Tree shown in above example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void findLeafDown ( Node * root , int lev , int * minDist ) { if ( root == NULL ) return ; if ( root -> left == NULL && root -> right == NULL ) { if ( lev < ( * minDist ) ) * minDist = lev ; return ; } findLeafDown ( root -> left , lev + 1 , minDist ) ; findLeafDown ( root -> right , lev + 1 , minDist ) ; } int findThroughParent ( Node * root , Node * x , int * minDist ) { if ( root == NULL ) return -1 ; if ( root == x ) return 0 ; int l = findThroughParent ( root -> left , x , minDist ) ; if ( l != -1 ) { findLeafDown ( root -> right , l + 2 , minDist ) ; return l + 1 ; } int r = findThroughParent ( root -> right , x , minDist ) ; if ( r != -1 ) { findLeafDown ( root -> left , r + 2 , minDist ) ; return r + 1 ; } return -1 ; } int minimumDistance ( Node * root , Node * x ) { int minDist = INT_MAX ; findLeafDown ( x , 0 , & minDist ) ; findThroughParent ( root , x , & minDist ) ; return minDist ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 12 ) ; root -> right = newNode ( 13 ) ; root -> right -> left = newNode ( 14 ) ; root -> right -> right = newNode ( 15 ) ; root -> right -> left -> left = newNode ( 21 ) ; root -> right -> left -> right = newNode ( 22 ) ; root -> right -> right -> left = newNode ( 23 ) ; root -> right -> right -> right = newNode ( 24 ) ; root -> right -> left -> left -> left = newNode ( 1 ) ; root -> right -> left -> left -> right = newNode ( 2 ) ; root -> right -> left -> right -> left = newNode ( 3 ) ; root -> right -> left -> right -> right = newNode ( 4 ) ; root -> right -> right -> left -> left = newNode ( 5 ) ; root -> right -> right -> left -> right = newNode ( 6 ) ; root -> right -> right -> right -> left = newNode ( 7 ) ; root -> right -> right -> right -> right = newNode ( 8 ) ; Node * x = root -> right ; cout << " The ▁ closest ▁ leaf ▁ to ▁ the ▁ node ▁ with ▁ value ▁ " << x -> key << " ▁ is ▁ at ▁ a ▁ distance ▁ of ▁ " << minimumDistance ( root , x ) << endl ; return 0 ; }
Find the time which is palindromic and comes after the given time | C ++ implementation of the approach ; Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; string getTime ( string s , int n ) { string res ; int h = stoi ( s . substr ( 0 , 2 ) ) ; int m = stoi ( s . substr ( 3 , 2 ) ) ; int rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; string rev_hs = to_string ( rev_h ) ; if ( h == 23 && m >= 32 ) { res = " - 1" ; } else if ( m < rev_h ) { string temp ; if ( h < 10 ) temp = "0" ; temp = temp + to_string ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } else { h ++ ; rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; rev_hs = to_string ( rev_h ) ; string temp ; if ( h < 10 ) temp = "0" ; temp = temp + to_string ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } return res ; } int main ( ) { string s = "21:12" ; int n = s . length ( ) ; cout << getTime ( s , n ) ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the count of valid sub - strings ; Variable ans to store all the possible substrings Initialize its value as total number of substrings that can be formed from the given string ; Stores recent index of the characters ; If character is a update a 's index  and the variable ans ; If character is b update b 's index  and the variable ans ; If character is c update c 's index  and the variable ans ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' a ' ) { a_index = i + 1 ; ans -= min ( b_index , c_index ) ; } else if ( str [ i ] == ' b ' ) { b_index = i + 1 ; ans -= min ( a_index , c_index ) ; } else { c_index = i + 1 ; ans -= min ( a_index , b_index ) ; } } return ans ; } int main ( ) { char str [ ] = " babac " ; int n = strlen ( str ) ; cout << CountSubstring ( str , n ) ; return 0 ; }
Find the numbers of strings that can be formed after processing Q queries | C ++ program to implement above approach ; To store the size of string and number of queries ; To store parent and rank of ith place ; To store maximum interval ; Function for initialization ; Function to find parent ; Function to make union ; Power function to calculate a raised to m1 under modulo 10000007 ; Function to take maxmium interval ; Function to find different possible strings ; make union of all chracters which are meant to be same ; find number of different sets formed ; return the required answer ; Driver Code ; queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2005 NEW_LINE #define mod  (int)(1e9 + 7) NEW_LINE int n , q ; int par [ N ] , Rank [ N ] ; map < int , int > m ; void initialize ( ) { for ( int i = 0 ; i <= n ; i ++ ) { par [ i ] = i ; Rank [ i ] = 0 ; } } int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } void Union ( int x , int y ) { int xpar = find ( x ) ; int ypar = find ( y ) ; if ( Rank [ xpar ] < Rank [ ypar ] ) par [ xpar ] = ypar ; else if ( Rank [ xpar ] > Rank [ ypar ] ) par [ ypar ] = xpar ; else { par [ ypar ] = xpar ; Rank [ xpar ] ++ ; } } int power ( long long a , long long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1LL * a * a ) % mod ; else if ( m1 & 1 ) return ( 1LL * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } void query ( int l , int r ) { m [ l + r ] = max ( m [ l + r ] , r ) ; } int possiblestrings ( ) { initialize ( ) ; for ( auto i = m . begin ( ) ; i != m . end ( ) ; i ++ ) { int x = i -> first - i -> second ; int y = i -> second ; while ( x < y ) { Union ( x , y ) ; x ++ ; y -- ; } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( par [ i ] == i ) ans ++ ; return power ( 26 , ans ) % mod ; } int main ( ) { n = 4 ; query ( 1 , 3 ) ; query ( 2 , 4 ) ; cout << possiblestrings ( ) ; return 0 ; }
Iterative Search for a key ' x ' in Binary Tree | Iterative level order traversal based method to search in Binary Tree ; A binary tree node has data , left child and right child ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; An iterative process to search an element x in a given binary tree ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; Queue based level order traversal ; See if current node is same as x ; Remove current node and enqueue its children ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; bool iterativeSearch ( node * root , int x ) { if ( root == NULL ) return false ; queue < node * > q ; q . push ( root ) ; while ( q . empty ( ) == false ) { node * node = q . front ( ) ; if ( node -> data == x ) return true ; q . pop ( ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; } return false ; } int main ( ) { node * NewRoot = NULL ; node * root = new node ( 2 ) ; root -> left = new node ( 7 ) ; root -> right = new node ( 5 ) ; root -> left -> right = new node ( 6 ) ; root -> left -> right -> left = new node ( 1 ) ; root -> left -> right -> right = new node ( 11 ) ; root -> right -> right = new node ( 9 ) ; root -> right -> right -> left = new node ( 4 ) ; iterativeSearch ( root , 6 ) ? cout << " Found STRNEWLINE " : cout << " Not ▁ Found STRNEWLINE " ; iterativeSearch ( root , 12 ) ? cout << " Found STRNEWLINE " : cout << " Not ▁ Found STRNEWLINE " ; return 0 ; }
Populate Inorder Successor for all nodes | C ++ program to populate inorder traversal of all nodes ; An implementation that doesn 't use static variable A wrapper over populateNextRecur ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; Set next of all descendents of p by traversing them in reverse Inorder ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in right subtree
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node * next ; } ; void populateNext ( node * root ) { node * next = NULL ; populateNextRecur ( root , & next ) ; } void populateNextRecur ( node * p , node * * next_ref ) { if ( p ) { populateNextRecur ( p -> right , next_ref ) ; p -> next = * next_ref ; * next_ref = p ; populateNextRecur ( p -> left , next_ref ) ; } }
Check if a binary string contains consecutive same or not | C ++ implementation of the approach ; Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( string str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } int main ( ) { string str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) cout << " Valid " ; else cout << " Invalid " ; return 0 ; }
Minimum K such that every substring of length atleast K contains a character c | CPP Program to find minimum K such that every substring of length atleast K contains some character c ; This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string s , int K ) { for ( int ch = 0 ; ch < 26 ; ch ++ ) { char c = ' a ' + ch ; int last = -1 ; bool found = true ; for ( int i = 0 ; i < K ; i ++ ) if ( s [ i ] == c ) last = i ; if ( last == -1 ) continue ; for ( int i = K ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == c ) last = i ; if ( last <= ( i - K ) ) { found = false ; break ; } } if ( found ) return 1 ; } return 0 ; } int binarySearch ( string s ) { int low = 1 , high = ( int ) s . size ( ) ; int ans ; while ( low <= high ) { int mid = ( high + low ) >> 1 ; if ( check ( s , mid ) ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } int32_t main ( ) { string s = " abcde " ; cout << binarySearch ( s ) << endl ; s = " aaaa " ; cout << binarySearch ( s ) << endl ; return 0 ; }
Check if number can be displayed using seven segment led | C ++ implementation of above approach ; Pre - computed values of segment used by digit 0 to 9. ; Check if it is possible to display the number ; Finding sum of the segments used by each digit of the number ; Driven Program ; Function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int seg [ 10 ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; string LedRequired ( string s , int led ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { count += seg [ int ( s [ i ] ) - 48 ] ; } if ( count <= led ) return " YES " ; else return " NO " ; } int main ( ) { string S = "123456789" ; int led = 20 ; cout << LedRequired ( S , led ) << endl ; return 0 ; }
Count the number of vowels occurring in all the substrings of given string | C ++ implementation of the above approach ; Returns the total sum of occurrences of all vowels ; No . of occurrences of 0 th character in all the substrings ; No . of occurrences of the ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int vowel_calc ( string s ) { int n = s . length ( ) ; vector < int > arr ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) arr . push_back ( n ) ; else arr . push_back ( ( n - i ) + arr [ i - 1 ] - i ) ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) sum += arr [ i ] ; return sum ; } int main ( ) { string s = " daceh " ; cout << vowel_calc ( s ) << endl ; return 0 ; }
Iterative Search for a key ' x ' in Binary Tree | An iterative method to search an item in Binary Tree ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; iterative process to search an element x in a given binary tree ; Base Case ; Create an empty stack and push root to it ; Do iterative preorder traversal to search x ; See the top item from stack and check if it is same as x ; Push right and left children of the popped node to stack ; Driver program
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; struct node { int data ; struct node * left , * right ; } ; struct node * newNode ( int data ) { struct node * node = new struct node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } bool iterativeSearch ( node * root , int x ) { if ( root == NULL ) return false ; stack < node * > nodeStack ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { struct node * node = nodeStack . top ( ) ; if ( node -> data == x ) return true ; nodeStack . pop ( ) ; if ( node -> right ) nodeStack . push ( node -> right ) ; if ( node -> left ) nodeStack . push ( node -> left ) ; } return false ; } int main ( void ) { struct node * NewRoot = NULL ; struct node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; iterativeSearch ( root , 6 ) ? cout << " Found STRNEWLINE " : cout << " Not ▁ Found STRNEWLINE " ; iterativeSearch ( root , 12 ) ? cout << " Found STRNEWLINE " : cout << " Not ▁ Found STRNEWLINE " ; return 0 ; }
Check whether two strings can be made equal by increasing prefixes | C ++ implementation of above approach ; check whether the first string can be converted to the second string by increasing the ASCII value of prefix string of first string ; length of two strings ; If lengths are not equal ; store the difference of ASCII values ; difference of first element ; traverse through the string ; the ASCII value of the second string should be greater than or equal to first string , if it is violated return false . ; store the difference of ASCII values ; the difference of ASCII values should be in descending order ; if the difference array is not in descending order ; if all the ASCII values of characters of first string is less than or equal to the second string and the difference array is in descending order , return true ; Driver code ; create two strings ; check whether the first string can be converted to the second string
#include <iostream> NEW_LINE using namespace std ; bool find ( string s1 , string s2 ) { int len = s1 . length ( ) , len_1 = s2 . length ( ) ; if ( len != len_1 ) return false ; int d [ len ] = { 0 } ; d [ 0 ] = s2 [ 0 ] - s1 [ 0 ] ; for ( int i = 1 ; i < len ; i ++ ) { if ( s1 [ i ] > s2 [ i ] ) return false ; else { d [ i ] = s2 [ i ] - s1 [ i ] ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( d [ i ] < d [ i + 1 ] ) return false ; } return true ; } int main ( ) { string s1 = " abcd " , s2 = " bcdd " ; if ( find ( s1 , s2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count all palindrome which is square of a palindrome | C ++ implementation of the above approach ; check if a number is a palindrome ; Function to return required count of palindromes ; Upper limit ; count odd length palindromes ; if s = '1234' ; then , t = '1234321' ; count even length palindromes ; if s = '1234' ; then , t = '12344321' ; Return count of super - palindromes ; Driver Code ; function call to get required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ispalindrome ( int x ) { int ans = 0 ; int temp = x ; while ( temp > 0 ) { ans = 10 * ans + temp % 10 ; temp = temp / 10 ; } return ans == x ; } int SuperPalindromes ( int L , int R ) { int LIMIT = 100000 ; int ans = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = to_string ( i ) ; string rs = s . substr ( 0 , s . size ( ) - 1 ) ; reverse ( rs . begin ( ) , rs . end ( ) ) ; string p = s + rs ; int p_sq = pow ( stoi ( p ) , 2 ) ; if ( p_sq > R ) break ; if ( p_sq >= L and ispalindrome ( p_sq ) ) ans = ans + 1 ; } for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = to_string ( i ) ; string rs = s ; reverse ( rs . begin ( ) , rs . end ( ) ) ; string p = s + rs ; int p_sq = pow ( stoi ( p ) , 2 ) ; if ( p_sq > R ) break ; if ( p_sq >= L and ispalindrome ( p_sq ) ) ans = ans + 1 ; } return ans ; } int main ( ) { string L = "4" ; string R = "1000" ; printf ( " % d STRNEWLINE " , SuperPalindromes ( stoi ( L ) , stoi ( R ) ) ) ; return 0 ; }
N | C ++ program to find the N - th character in the string "1234567891011 . . " ; Function that returns the N - th character ; initially null string ; starting integer ; add integers in string ; one digit numbers added ; more than 1 digit number , generate equivalent number in a string s1 and concatenate s1 into s . ; add the number in string ; reverse the string ; attach the number ; if the length exceeds N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char NthCharacter ( int n ) { string s = " " ; int c = 1 ; for ( int i = 1 ; ; i ++ ) { if ( c < 10 ) s += char ( 48 + c ) ; else { string s1 = " " ; int dup = c ; while ( dup ) { s1 += char ( ( dup % 10 ) + 48 ) ; dup /= 10 ; } reverse ( s1 . begin ( ) , s1 . end ( ) ) ; s += s1 ; } c ++ ; if ( s . length ( ) >= n ) { return s [ n - 1 ] ; } } } int main ( ) { int n = 11 ; cout << NthCharacter ( n ) ; return 0 ; }
Sub | C ++ implementation of above approach ; Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; map to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string s , int k ) { int count = 0 , length = 0 , pos = 0 ; map < char , int > m ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { m [ s [ i ] ] ++ ; length ++ ; if ( length > k ) { m [ s [ pos ++ ] ] -- ; length -- ; } if ( length == k && m [ s [ i ] ] == length ) count ++ ; } cout << count << endl ; } int main ( ) { string s = " aaaabbbccdddd " ; int k = 4 ; solve ( s , k ) ; return 0 ; }
Program to replace every space in a string with hyphen | C ++ program to replace space with - ; Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int main ( ) { string str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str [ i ] == ' ▁ ' ) { str [ i ] = ' - ' ; } } cout << str << endl ; return 0 ; }
Minimum operation require to make first and last character same | CPP program to find minimum operation require to make first and last character same ; Function to find minimum operation require to make first and last character same ; Base conditions ; If answer found ; If string is already visited ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Store the minimum value ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = INT_MAX ; map < string , int > m ; int Min = INT_MAX ; int minOperation ( string & s , int i , int j , int count ) { if ( ( i >= s . size ( ) && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] || ( count >= Min ) ) return count ; string str = to_string ( i ) + " | " + to_string ( j ) ; if ( m . find ( str ) == m . end ( ) ) { if ( i >= s . size ( ) ) m [ str ] = minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) m [ str ] = minOperation ( s , i + 1 , j , count + 1 ) ; else m [ str ] = min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } if ( m [ str ] < Min ) Min = m [ str ] ; return m [ str ] ; } int main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . size ( ) - 1 , 0 ) ; if ( ans == MAX ) cout << -1 ; else cout << ans ; }