codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations required to make two strings equal | C ++ implementation to find the minimum number of operations to make two strings equal ; Function to find out parent of an alphabet ; Function to merge two different alphabets ; Merge a and b using rank compression ; Function to find the minimum number of operations required ; Initializing parent to i and rank ( size ) to 1 ; We will store our answerin this vector ; Traversing strings ; If they have different parents ; Find their respective parents and merge them ; Store this in our Answer vector ; Number of operations ; Driver Code ; Two strings S1 and S2 ; Function Call
#include <bits/stdc++.h> NEW_LINE #define MAX  500001 NEW_LINE using namespace std ; int parent [ MAX ] ; int Rank [ MAX ] ; int find ( int x ) { return parent [ x ] = parent [ x ] == x ? x : find ( parent [ x ] ) ; } void merge ( int r1 , int r2 ) { if ( r1 != r2 ) { if ( Rank [ r1 ] > Rank [ r2 ] ) { parent [ r2 ] = r1 ; Rank [ r1 ] += Rank [ r2 ] ; } else { parent [ r1 ] = r2 ; Rank [ r2 ] += Rank [ r1 ] ; } } } void minimumOperations ( string s1 , string s2 ) { for ( int i = 1 ; i <= 26 ; i ++ ) { parent [ i ] = i ; Rank [ i ] = 1 ; } vector < pair < char , char > > ans ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( find ( s1 [ i ] - 96 ) != find ( s2 [ i ] - 96 ) ) { int x = find ( s1 [ i ] - 96 ) ; int y = find ( s2 [ i ] - 96 ) ; merge ( x , y ) ; ans . push_back ( { s1 [ i ] , s2 [ i ] } ) ; } } } cout << ans . size ( ) << endl ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] . first << " - > " << ans [ i ] . second << endl ; } int main ( ) { string s1 , s2 ; s1 = " abb " ; s2 = " dad " ; minimumOperations ( s1 , s2 ) ; return 0 ; }
Find the N | C ++ program to find the N - th lexicographic permutation of string using Factroid method ; Function to calculate nth permutation of string ; Creating an empty stack ; Subtracting 1 from N because the permutations start from 0 in factroid method ; Loop to generate the factroid of the sequence ; Loop to generate nth permutation ; Remove 1 - element in each cycle ; Final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void string_permutation ( long long int n , string str ) { stack < int > s ; string result ; n = n - 1 ; for ( int i = 1 ; i < str . size ( ) + 1 ; i ++ ) { s . push ( n % i ) ; n = n / i ; } for ( int i = 0 ; i < str . size ( ) ; i ++ ) { int a = s . top ( ) ; result += str [ a ] ; int j ; for ( j = a ; j < str . length ( ) ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j + 1 ] = ' \0' ; s . pop ( ) ; } cout << result << endl ; } int main ( ) { string str = " abcde " ; long long int n = 11 ; string_permutation ( n , str ) ; return 0 ; }
Defanged Version of Internet Protocol Address | C ++ implementation to find the defanged version of the IP address ; Function to generate a defanged version of IP address . ; Loop to iterate over the characters of the string ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string GeberateDefangIP ( string str ) { string defangIP = " " ; for ( char c : str ) ( c == ' . ' ) ? defangIP += " [ . ] " : defangIP += c ; return defangIP ; } int main ( ) { string str = "255.100.50.0" ; cout << GeberateDefangIP ( str ) ; return 0 ; }
Longest palindromic string formed by concatenation of prefix and suffix of a string | C ++ implementation of the above approach ; Function used to calculate the longest prefix which is also a suffix ; Traverse the string ; Update the lps size ; Returns size of lps ; Function to calculate the length of longest palindromic substring which is either a suffix or prefix ; Append a character to separate the string and reverse of the string ; Reverse the string ; Append the reversed string ; Function to find the Longest palindromic string formed from concatenation of prefix and suffix of a given string ; Calculating the length for which prefix is reverse of suffix ; Append prefix to the answer ; Store the remaining string ; If the remaining string is not empty that means that there can be a palindrome substring which can be added between the suffix & prefix ; Calculate the length of longest prefix palindromic substring ; Reverse the given string to find the longest palindromic suffix ; Calculate the length of longest prefix palindromic substring ; If the prefix palindrome is greater than the suffix palindrome ; Append the prefix to the answer ; If the suffix palindrome is greater than the prefix palindrome ; Append the suffix to the answer ; Finally append the suffix to the answer ; Return the answer string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kmp ( string s ) { vector < int > lps ( s . size ( ) , 0 ) ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { int previous_index = lps [ i - 1 ] ; while ( previous_index > 0 && s [ i ] != s [ previous_index ] ) { previous_index = lps [ previous_index - 1 ] ; } lps [ i ] = previous_index + ( s [ i ] == s [ previous_index ] ? 1 : 0 ) ; } return lps [ lps . size ( ) - 1 ] ; } int remainingStringLongestPallindrome ( string s ) { string t = s + " ? " ; reverse ( s . begin ( ) , s . end ( ) ) ; t += s ; return kmp ( t ) ; } string longestPrefixSuffixPallindrome ( string s ) { int length = 0 ; int n = s . size ( ) ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { if ( s [ i ] != s [ j ] ) { break ; } length ++ ; } string ans = s . substr ( 0 , length ) ; string remaining = s . substr ( length , ( n - ( 2 * length ) ) ) ; if ( remaining . size ( ) ) { int longest_prefix = remainingStringLongestPallindrome ( remaining ) ; reverse ( remaining . begin ( ) , remaining . end ( ) ) ; int longest_suffix = remainingStringLongestPallindrome ( remaining ) ; if ( longest_prefix > longest_suffix ) { reverse ( remaining . begin ( ) , remaining . end ( ) ) ; ans += remaining . substr ( 0 , longest_prefix ) ; } else { ans += remaining . substr ( 0 , longest_suffix ) ; } } ans += s . substr ( n - length , length ) ; return ans ; } int main ( ) { string str = " rombobinnimor " ; cout << longestPrefixSuffixPallindrome ( str ) << endl ; }
Generate a string with maximum possible alphabets with odd frequencies | C ++ program to generate a string of length n with maximum possible alphabets with each of them occuring odd number of times . ; Function to generate a string of length n with maximum possible alphabets each occuring odd number of times . ; If n is odd ; Add all characters from b - y ; Append a to fill the remaining length ; If n is even ; Add all characters from b - z ; Append a to fill the remaining length ; Driven code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generateTheString ( int n ) { string ans = " " ; if ( n % 2 ) { for ( int i = 0 ; i < min ( n , 24 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 24 ) { for ( int i = 0 ; i < ( n - 24 ) ; i ++ ) ans += ' a ' ; } } else { for ( int i = 0 ; i < min ( n , 25 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 25 ) { for ( int i = 0 ; i < ( n - 25 ) ; i ++ ) ans += ' a ' ; } } return ans ; } int main ( ) { int n = 34 ; cout << generateTheString ( n ) ; return 0 ; }
Move all occurrence of letter ' x ' from the string s to the end using Recursion | C ++ implementation to Move all occurrence of letter x from the string s to the end using Recursion ; Function to move all ' x ' in the end ; Store current character ; Check if current character is not ' x ' ; recursive function call ; Check if current character is ' x ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void moveAtEnd ( string s , int i , int l ) { if ( i >= l ) return ; char curr = s [ i ] ; if ( curr != ' x ' ) cout << curr ; moveAtEnd ( s , i + 1 , l ) ; if ( curr == ' x ' ) cout << curr ; return ; } int main ( ) { string s = " geekxsforgexxeksxx " ; int l = s . length ( ) ; moveAtEnd ( s , 0 , l ) ; return 0 ; }
Construct a string of length L such that each substring of length X has exactly Y distinct letters | C ++ implementation to construct a string of length L such that each substring of length X has exactly Y distinct letters . ; Initialize p equal to the ASCII value of a ; Iterate till the length of the string ; Driver code
#include <iostream> NEW_LINE using namespace std ; void String ( int l , int x , int y ) { int p = 97 ; for ( int j = 0 ; j < l ; j ++ ) { char ans = ( char ) ( p + ( j % y ) ) ; cout << ans ; } } int main ( ) { int l = 6 ; int x = 5 ; int y = 3 ; String ( l , x , y ) ; return 0 ; }
Find the final co | C ++ implementation of the above approach ; Function to print the final position of the point after traversing through the given directions ; Traversing through the given directions ; If its north or south the point will move left or right ; If its west or east the point will move upwards or downwards ; Returning the final position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalCoordinates ( int SX , int SY , string D ) { for ( int i = 0 ; i < D . length ( ) ; i ++ ) { if ( D [ i ] == ' N ' ) SY += 1 ; else if ( D [ i ] == ' S ' ) SY -= 1 ; else if ( D [ i ] == ' E ' ) SX += 1 ; else SX -= 1 ; } string ans = ' ( ' + to_string ( SX ) + ' , ' + to_string ( SY ) + ' ) ' ; cout << ans << endl ; } int main ( ) { int SX = 2 , SY = 2 ; string D = " NSSE " ; finalCoordinates ( SX , SY , D ) ; }
Frequency of smallest character in first sentence less than that of second sentence | C ++ program for the above approach ; Function to count the frequency of minimum character ; Sort the string s ; Return the count with smallest character ; Function to count number of frequency of smallest character of string arr1 [ ] is less than the string in arr2 [ ] ; To store the frequency of smallest character in each string of arr2 ; Traverse the arr2 [ ] ; Count the frequency of smallest character in string s ; Append the frequency to freq [ ] ; Sort the frequency array ; Traverse the array arr1 [ ] ; Count the frequency of smallest character in string s ; find the element greater than f ; Find the count such that arr1 [ i ] < arr2 [ j ] ; Print the count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinFreq ( string s ) { sort ( s . begin ( ) , s . end ( ) ) ; return count ( s . begin ( ) , s . end ( ) , s [ 0 ] ) ; } void countLessThan ( vector < string > & arr1 , vector < string > & arr2 ) { vector < int > freq ; for ( string s : arr2 ) { int f = countMinFreq ( s ) ; freq . push_back ( f ) ; } sort ( freq . begin ( ) , freq . end ( ) ) ; for ( string s : arr1 ) { int f = countMinFreq ( s ) ; auto it = upper_bound ( freq . begin ( ) , freq . end ( ) , f ) ; int cnt = freq . size ( ) - ( it - freq . begin ( ) ) ; cout << cnt << ' ▁ ' ; } } int main ( ) { vector < string > arr1 , arr2 ; arr1 = { " yyy " , " zz " } ; arr2 = { " x " , " xx " , " xxx " , " xxxx " } ; countLessThan ( arr1 , arr2 ) ; return 0 ; }
Lexicographically all Shortest Palindromic Substrings from a given string | C ++ program to find Lexicographically all Shortest Palindromic Substrings from a given string ; Function to find all lexicographically shortest palindromic substring ; Array to keep track of alphabetic characters ; Iterate to print all lexicographically shortest substring ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shortestPalindrome ( string s ) { int abcd [ 26 ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) abcd [ s [ i ] - 97 ] = 1 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( abcd [ i ] == 1 ) cout << char ( i + 97 ) << " ▁ " ; } } int main ( ) { string s = " geeksforgeeks " ; shortestPalindrome ( s ) ; return 0 ; }
Remove duplicates from string keeping the order according to last occurrences | C ++ program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicates ( string str ) { int n = str . length ( ) ; string res = " " ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = i + 1 ; j < n ; j ++ ) if ( str [ i ] == str [ j ] ) break ; if ( j == n ) res = res + str [ i ] ; } return res ; } int main ( ) { string str = " geeksforgeeks " ; cout << removeDuplicates ( str ) ; return 0 ; }
Remove duplicates from string keeping the order according to last occurrences | C ++ program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Create an empty hash table ; Traverse through all characters from right to left ; If current character is not in ; Reverse the result string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicates ( string str ) { int n = str . length ( ) ; unordered_set < char > s ; string res = " " ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . find ( str [ i ] ) == s . end ( ) ) { res = res + str [ i ] ; s . insert ( str [ i ] ) ; } } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string str = " geeksforgeeks " ; cout << removeDuplicates ( str ) ; return 0 ; }
Count of numbers in a range that does not contain the digit M and which is divisible by M . | C ++ implementation to illustrate the program ; Function to count all the numbers which does not contain the digit ' M ' and is divisible by M ; Storing all the distinct digits of a number ; Checking if the two conditions are satisfied or not ; Driver code ; Lower Range ; Upper Range ; The digit
#include <bits/stdc++.h> NEW_LINE using namespace std ; void contain ( int L , int U , int M ) { int count = 0 ; for ( int j = L ; j < U ; j ++ ) { set < string > num ; string str = to_string ( j ) ; num . insert ( str ) ; if ( j % M == 0 and num . find ( to_string ( M ) ) == num . end ( ) ) { count += 1 ; } } cout << count - 2 ; } int main ( ) { int L = 106 ; int U = 200 ; int M = 7 ; contain ( L , U , M ) ; }
Longest string which is prefix string of at least two strings | C ++ program to find longest string which is prefix string of at least two strings ; Function to find Max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current string or not ; If atleast 2 string have that character ; Recursive call to i + 1 ; Driver code ; Initialising strings ; push strings into vectors .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max1 = 0 ; int MaxLength ( vector < string > v , int i , int m ) { if ( i >= m ) { return m - 1 ; } for ( int k = 0 ; k < 26 ; k ++ ) { char c = ' a ' + k ; vector < string > v1 ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v [ j ] [ i ] == c ) { v1 . push_back ( v [ j ] ) ; } } if ( v1 . size ( ) >= 2 ) { max1 = max ( max1 , MaxLength ( v1 , i + 1 , m ) ) ; } else { max1 = max ( max1 , i - 1 ) ; } } return max1 ; } int main ( ) { string s1 , s2 , s3 , s4 , s5 ; s1 = " abcde " ; s2 = " abcsd " ; s3 = " bcsdf " ; s4 = " abcda " ; s5 = " abced " ; vector < string > v ; v . push_back ( s1 ) ; v . push_back ( s2 ) ; v . push_back ( s3 ) ; v . push_back ( s4 ) ; v . push_back ( s5 ) ; int m = v [ 0 ] . size ( ) ; cout << MaxLength ( v , 0 , m ) + 1 << endl ; return 0 ; }
Periodic Binary String With Minimum Period and a Given Binary String as Subsequence . | C ++ implementation to find the periodic string with minimum period ; Function to find the periodic string with minimum period ; Print the string S if it consists of similar elements ; Find the required periodic string with period 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPeriodicString ( string S ) { int l = 2 * S . length ( ) ; int count = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S [ i ] == '1' ) count ++ ; } if ( count == S . length ( ) count == 0 ) cout << S << " STRNEWLINE " ; else { char arr [ l ] ; for ( int i = 0 ; i < l ; i += 2 ) { arr [ i ] = '1' ; arr [ i + 1 ] = '0' ; } for ( int i = 0 ; i < l ; i ++ ) cout << arr [ i ] ; cout << " STRNEWLINE " ; } } int main ( ) { string S = "1111001" ; findPeriodicString ( S ) ; return 0 ; }
Print Strings In Reverse Dictionary Order Using Trie | C ++ program to print array of string in reverse dictionary order using trie ; Trie node ; endOfWord is true if the node represents end of a word ; Function will return the new node initialized NULL ; Initialize null to the all child ; Function will insert the string in a trie recursively ; Create a new node ; Recursive call for insertion of string ; Make the endOfWord true which represents the end of string ; Function call to insert a string ; Function call with necessary arguments ; Function to check whether the node is leaf or not ; Function to display the content of trie ; If node is leaf node , it indicates end of string , so a null character is added and string is displayed ; Assign a null character in temporary string ; check if NON NULL child is found add parent key to str and call the display function recursively for child node ; Function call for displaying content ; Driver code ; After inserting strings , trie will look like root / \ a t | | n h | \ | s y e | | \ w i r | | | e r e | r
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define CHILDREN  26 NEW_LINE #define MAX  100 NEW_LINE struct trie { trie * child [ CHILDREN ] ; bool endOfWord ; } ; trie * createNode ( ) { trie * temp = new trie ( ) ; temp -> endOfWord = false ; for ( int i = 0 ; i < CHILDREN ; i ++ ) { temp -> child [ i ] = NULL ; } return temp ; } void insertRecursively ( trie * itr , string str , int i ) { if ( i < str . length ( ) ) { int index = str [ i ] - ' a ' ; if ( itr -> child [ index ] == NULL ) { itr -> child [ index ] = createNode ( ) ; } insertRecursively ( itr -> child [ index ] , str , i + 1 ) ; } else { itr -> endOfWord = true ; } } void insert ( trie * itr , string str ) { insertRecursively ( itr , str , 0 ) ; } bool isLeafNode ( trie * root ) { return root -> endOfWord != false ; } void displayContent ( trie * root , char str [ ] , int level ) { if ( isLeafNode ( root ) ) { str [ level ] = ' \0' ; cout << str << endl ; } for ( int i = CHILDREN - 1 ; i >= 0 ; i -- ) { if ( root -> child [ i ] ) { str [ level ] = i + ' a ' ; displayContent ( root -> child [ i ] , str , level + 1 ) ; } } } void display ( trie * itr ) { int level = 0 ; char str [ MAX ] ; displayContent ( itr , str , level ) ; } int main ( ) { trie * root = createNode ( ) ; insert ( root , " their " ) ; insert ( root , " there " ) ; insert ( root , " answer " ) ; insert ( root , " any " ) ; display ( root ) ; return 0 ; }
Check if two strings after processing backspace character are equal or not | C ++ implementation to Check if two strings after processing backspace character are equal or not ; function to compare the two strings ; the index of character in string which would be removed when backspace is encountered ; checks if a backspace is encountered or not . In case the first character is # , no change in string takes place ; the character after the # is added after the character at position rem_ind1 ; checks if a backspace is encountered or not ; check if the value of rem_ind1 and rem_ind2 is same , if not then it means they have different length ; check if resultant strings are empty ; check if each character in the resultant string is same ; Driver code ; initialise two strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( string s , string t ) { int ps , pt , i ; ps = -1 ; for ( i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' # ' && ps != -1 ) ps -= 1 ; else if ( s [ i ] != ' # ' ) { s = s [ i ] ; ps += 1 ; } } pt = -1 ; for ( i = 0 ; i < t . size ( ) ; i ++ ) { if ( t [ i ] == ' # ' && pt != -1 ) pt -= 1 ; else if ( t [ i ] != ' # ' ) { t [ pt + 1 ] = t [ i ] ; pt += 1 ; } } if ( pt != ps ) return false ; else if ( ps == -1 && pt == -1 ) return true ; else { for ( i = 0 ; i <= pt ; i ++ ) { if ( s [ i ] != t [ i ] ) return false ; } return true ; } } int main ( ) { string s = " geee # e # ks " ; string t = " gee # # eeks " ; if ( compare ( s , t ) ) cout << " True " ; else cout << " False " ; }
Queries to find total number of duplicate character in range L to R in the string S | CPP implementation to Find the total number of duplicate character in a range L to R for Q number of queries in a string S ; Vector of vector to store position of all characters as they appear in string ; Function to store position of each character ; Inserting position of each character as they appear ; Function to calculate duplicate characters for Q queries ; Variable to count duplicates ; Iterate over all 26 characters ; Finding the first element which is less than or equal to L ; Check if first pointer exists and is less than R ; Incrementing first pointer to check if the next duplicate element exists ; Check if the next element exists and is less than R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > v ( 26 ) ; void calculate ( string s ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { v [ s [ i ] - ' a ' ] . push_back ( i ) ; } } void query ( int L , int R ) { int duplicates = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { auto first = lower_bound ( v [ i ] . begin ( ) , v [ i ] . end ( ) , L - 1 ) ; if ( first != v [ i ] . end ( ) && * first < R ) { first ++ ; if ( first != v [ i ] . end ( ) && * first < R ) duplicates ++ ; } } cout << duplicates << endl ; } int main ( ) { string s = " geeksforgeeks " ; int Q = 2 ; int l1 = 1 , r1 = 5 ; int l2 = 4 , r2 = 8 ; calculate ( s ) ; query ( l1 , r1 ) ; query ( l2 , r2 ) ; return 0 ; }
Count the minimum number of groups formed in a string | C ++ implementation to Count the minimum number of groups formed in a string by replacing consecutive characters with same single character ; Function to count the minimum number of groups formed in the given string s ; Initializing count as one since the string is not NULL ; Comparing adjacent characters ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void group_formed ( string S ) { int count = 1 ; for ( int i = 0 ; i < S . size ( ) - 1 ; i ++ ) { if ( S [ i ] != S [ i + 1 ] ) count += 1 ; } cout << ( count ) ; } int main ( ) { string S = " TTWWW " ; group_formed ( S ) ; }
Find the Substring with maximum product | C ++ program to find the maximum product substring ; Function to return the value of a character ; Function to find the maximum product substring ; To store substrings ; Check if current product is maximum possible or not ; If product is 0 ; Return the substring with maximum product ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int value ( char x ) { return ( int ) ( x - ' a ' ) ; } string maximumProduct ( string str , int n ) { string answer = " " , curr = " " ; long long maxProduct = 0 , product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product *= 1LL * value ( str [ i ] ) ; curr += str [ i ] ; if ( product >= maxProduct ) { maxProduct = product ; answer = curr ; } if ( product == 0 ) { product = 1 ; curr = " " ; } } return answer ; } int main ( ) { string str = " sdtfakdhdahdzz " ; int n = str . size ( ) ; cout << maximumProduct ( str , n ) << endl ; return 0 ; }
Sum of minimum and the maximum difference between two given Strings | C ++ program to find the sum of the minimum and the maximum difference between two given strings ; Function to find the sum of the minimum and the maximum difference between two given strings ; Variables to store the minimum difference and the maximum difference ; Iterate through the length of the string as both the given strings are of the same length ; For the maximum difference , we can replace " + " in both the strings with different char ; For the minimum difference , we can replace " + " in both the strings with the same char ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string a , string b ) { int l = a . length ( ) ; int min = 0 , max = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] == ' + ' b [ i ] == ' + ' a [ i ] != b [ i ] ) max ++ ; if ( a [ i ] != ' + ' && b [ i ] != ' + ' && a [ i ] != b [ i ] ) min ++ ; } cout << min + max << endl ; } int main ( ) { string s1 = " a + c " , s2 = " + + b " ; solve ( s1 , s2 ) ; return 0 ; }