codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all LCP of maximum length by selecting any two Strings at a time | C ++ program to find Sum of all LCP of maximum length by selecting any two Strings at a time ; Using map to store the pointers of children nodes for dynamic implementation , for making the program space efiicient ; Counts the number of times the node is visited while making the trie ; Initially visited value for all nodes is zero ; Head node of the trie is initialize as ' \0' , after this all strings add ; Function to insert the strings in the trie ; Inserting character - by - character ; If the node of ch is not present in map make a new node and add in map ; Recursive function to calculate the answer argument is passed by reference ; To store changed visited values from children of this node i . e . number of nodes visited by its children ; Updating the visited variable , telling number of nodes that have already been visited by its children ; If node -> visited > 1 , means more than one string has prefix up till this node common in them ; Number of string pair with current node common in them ; Updating visited variable of current node ; Returning the total number of nodes already visited that needs to be updated to previous node ; Function to run the dfs function for the first time and give the answer variable ; Stores the final answer as sum of all depths ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; class TrieNode { public : char val ; map < char , TrieNode * > children ; int visited ; TrieNode ( char x ) { val = x ; visited = 0 ; } } ; class Trie { public : TrieNode * head ; Trie ( ) { head = new TrieNode ( ' \0' ) ; } void addWord ( string s ) { TrieNode * temp = head ; const unsigned int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s [ i ] ; if ( ! temp -> children [ ch ] ) { temp -> children [ ch ] = new TrieNode ( ch ) ; } temp = temp -> children [ ch ] ; temp -> visited ++ ; } } int dfs ( TrieNode * node , int & ans , int depth ) { int vis = 0 ; for ( auto child : node -> children ) { vis += dfs ( child . second , ans , depth + 1 ) ; } node -> visited -= vis ; int string_pair = 0 ; if ( node -> visited > 1 ) { string_pair = ( node -> visited / 2 ) ; ans += ( depth * string_pair ) ; node -> visited -= ( 2 * string_pair ) ; } return ( 2 * string_pair + vis ) ; } int dfshelper ( ) { int ans = 0 ; dfs ( head , ans , 0 ) ; return ans ; } } ; int main ( ) { Trie T ; string str [ ] = { " babab " , " ababb " , " abbab " , " aaaaa " , " babaa " , " babbb " } ; int n = 6 ; for ( int i = 0 ; i < n ; i ++ ) { T . addWord ( str [ i ] ) ; } int ans = T . dfshelper ( ) ; cout << ans << endl ; return 0 ; }
Print all possible combinations of words from Dictionary using Trie | C ++ implementation of the approach ; Trie node ; isEndOfWord is true if node represents the end of the word ; Returns new trie node ; If not present , inserts key into trie If the key is prefix of trie node , marks the node as leaf node ; Mark node as leaf ; Returns true if the key is present in the trie ; Result stores the current prefix with spaces between words ; Process all prefixes one by one ; Extract substring from 0 to i in prefix ; If trie conatins this prefix then check for the remaining string . Otherwise ignore this prefix ; If no more elements are there then print ; Add this element to the previous prefix ; If ( result == word ) then return If you don 't want to print last word ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int ALPHABET_SIZE = 26 ; struct TrieNode { struct TrieNode * children [ ALPHABET_SIZE ] ; bool isEndOfWord ; } ; struct TrieNode * getNode ( void ) { struct TrieNode * pNode = new TrieNode ; pNode -> isEndOfWord = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode -> children [ i ] = NULL ; return pNode ; } void insert ( struct TrieNode * root , string key ) { struct TrieNode * pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key [ i ] - ' a ' ; if ( ! pCrawl -> children [ index ] ) pCrawl -> children [ index ] = getNode ( ) ; pCrawl = pCrawl -> children [ index ] ; } pCrawl -> isEndOfWord = true ; } bool search ( struct TrieNode * root , string key ) { struct TrieNode * pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key [ i ] - ' a ' ; if ( ! pCrawl -> children [ index ] ) return false ; pCrawl = pCrawl -> children [ index ] ; } return ( pCrawl != NULL && pCrawl -> isEndOfWord ) ; } void wordBreakAll ( TrieNode * root , string word , int n , string result ) { for ( int i = 1 ; i <= n ; i ++ ) { string prefix = word . substr ( 0 , i ) ; if ( search ( root , prefix ) ) { if ( i == n ) { result += prefix ; cout << " TABSYMBOL " << result << endl ; return ; } wordBreakAll ( root , word . substr ( i , n - i ) , n - i , result + prefix + " ▁ " ) ; } } } int main ( ) { struct TrieNode * root = getNode ( ) ; string dictionary [ ] = { " sam " , " sung " , " samsung " } ; int n = sizeof ( dictionary ) / sizeof ( string ) ; for ( int i = 0 ; i < n ; i ++ ) { insert ( root , dictionary [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << dictionary [ i ] << " : ▁ STRNEWLINE " ; wordBreakAll ( root , dictionary [ i ] , dictionary [ i ] . length ( ) , " " ) ; } return 0 ; }
Jaro and Jaro | C ++ implementation of above approach ; Function to calculate the Jaro Similarity of two strings ; If the strings are equal ; Length of two strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first string ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second string ; Return the Jaro Similarity ; Driver code ; Print jaro Similarity of two strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; double jaro_distance ( string s1 , string s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . length ( ) , len2 = s2 . length ( ) ; int max_dist = floor ( max ( len1 , len2 ) / 2 ) - 1 ; int match = 0 ; int hash_s1 [ s1 . length ( ) ] = { 0 } , hash_s2 [ s2 . length ( ) ] = { 0 } ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = max ( 0 , i - max_dist ) ; j < min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 [ i ] != s2 [ point ++ ] ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } int main ( ) { string s1 = " CRATE " , s2 = " TRACE " ; cout << jaro_distance ( s1 , s2 ) << endl ; return 0 ; }
Print characters and their frequencies in order of occurrence using Binary Tree | C ++ implementation of the above approach ; Node in the tree where data holds the character of the string and cnt holds the frequency ; Function to add a new node to the Binary Tree ; Create a new node and populate its data part , set cnt as 1 and left and right children as NULL ; Function to add a node to the Binary Tree in level order ; Use the queue data structure for level order insertion and push the root of tree to Queue ; If the character to be inserted is present , update the cnt ; If the left child is empty add a new node as the left child ; If the character is present as a left child , update the cnt and exit the loop ; Add the left child to the queue for further processing ; If the right child is empty , add a new node to the right ; If the character is present as a right child , update the cnt and exit the loop ; Add the right child to the queue for further processing ; Function to print the level order traversal of the Binary Tree ; Add the root to the queue ; If the cnt of the character is more then one , display cnt ; If the cnt of character is one , display character only ; Add the left child to the queue for further processing ; Add the right child to the queue for further processing ; Driver code ; Add individual characters to the string one by one in level order ; Print the level order of the constructed binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { char data ; int cnt ; node * left , * right ; } ; node * add ( char data ) { node * newnode = new node ; newnode -> data = data ; newnode -> cnt = 1 ; newnode -> left = newnode -> right = NULL ; return newnode ; } node * addinlvlorder ( node * root , char data ) { if ( root == NULL ) { return add ( data ) ; } queue < node * > Q ; Q . push ( root ) ; while ( ! Q . empty ( ) ) { node * temp = Q . front ( ) ; Q . pop ( ) ; if ( temp -> data == data ) { temp -> cnt ++ ; break ; } if ( temp -> left == NULL ) { temp -> left = add ( data ) ; break ; } else { if ( temp -> left -> data == data ) { temp -> left -> cnt ++ ; break ; } Q . push ( temp -> left ) ; } if ( temp -> right == NULL ) { temp -> right = add ( data ) ; break ; } else { if ( temp -> right -> data == data ) { temp -> right -> cnt ++ ; break ; } Q . push ( temp -> right ) ; } } return root ; } void printlvlorder ( node * root ) { queue < node * > Q ; Q . push ( root ) ; while ( ! Q . empty ( ) ) { node * temp = Q . front ( ) ; if ( temp -> cnt > 1 ) { cout << temp -> data << temp -> cnt ; } else { cout << temp -> data ; } Q . pop ( ) ; if ( temp -> left != NULL ) { Q . push ( temp -> left ) ; } if ( temp -> right != NULL ) { Q . push ( temp -> right ) ; } } } int main ( ) { string s = " geeksforgeeks " ; node * root = NULL ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { root = addinlvlorder ( root , s [ i ] ) ; } printlvlorder ( root ) ; return 0 ; }
Find the resultant String after replacing X with Y and removing Z | C ++ program to find the resultant String after replacing X with Y and removing Z ; Function to replace and remove ; Two pointer start and end points to beginning and end position in the string ; If start is having Z find X pos in end and replace Z with another character ; Find location for having different character instead of Z ; If found swap character at start and end ; Else increment start Also checkin for X at start position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void replaceRemove ( string & s , char X , char Y , char Z ) { int start = 0 , end = s . size ( ) - 1 ; while ( start <= end ) { if ( s [ start ] == Z ) { while ( end >= 0 && s [ end ] == Z ) { end -- ; } if ( end > start ) { swap ( s [ start ] , s [ end ] ) ; if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } else { if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } while ( s . size ( ) > 0 && s [ s . size ( ) - 1 ] == Z ) { s . pop_back ( ) ; } } int main ( ) { string str = " batman " ; char X = ' a ' , Y = ' d ' , Z = ' b ' ; replaceRemove ( str , X , Y , Z ) ; if ( str . size ( ) == 0 ) { cout << -1 ; } else { cout << str ; } return 0 ; }
Check if all bits can be made same by flipping two consecutive bits | C ++ program for the above approach ; Function to check if Binary string can be made equal ; Counting occurence of zero and one in binary string ; From above observation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string canMake ( string & s ) { int o = 0 , z = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] - '0' == 1 ) o ++ ; else z ++ ; } if ( o % 2 == 1 && z % 2 == 1 ) return " NO " ; else return " YES " ; } int main ( ) { string s = "01011" ; cout << canMake ( s ) << ' ' ; return 0 ; }
Count of non | C ++ implementation of the above approach ; Function to find the count of the number of strings ; Loop to iterate over the frequency of character of string ; reduce the frequency of current element ; recursive call ; freq [ i ] ++ ; backtrack ; Function to count the number of non - empty sequences ; store the frequency of each character ; Maintain the frequency ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNumberOfStringsUtil ( vector < int > & freq , int & count ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) { freq [ i ] -- ; count ++ ; countNumberOfStringsUtil ( freq , count ) ; } } } int countNumberOfStrings ( string s ) { vector < int > freq ( 26 , 0 ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { freq [ s [ i ] - ' A ' ] ++ ; } int count = 0 ; countNumberOfStringsUtil ( freq , count ) ; return count ; } int main ( ) { string s = " AAABBC " ; cout << countNumberOfStrings ( s ) ; return 0 ; }
Minimum swaps required to make a binary string divisible by 2 ^ k | C ++ implementation of the approach ; Function to return the minimum swaps required ; To store the final answer ; To store the count of one and zero ; Loop from end of the string ; If s [ i ] = 1 ; If s [ i ] = 0 ; If c_zero = k ; If the result can 't  be achieved ; Return the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinSwaps ( string s , int k ) { int ans = 0 ; int c_one = 0 , c_zero = 0 ; for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) c_one ++ ; if ( s [ i ] == '0' ) c_zero ++ , ans += c_one ; if ( c_zero == k ) break ; } if ( c_zero < k ) return -1 ; return ans ; } int main ( ) { string s = "100111" ; int k = 2 ; cout << findMinSwaps ( s , k ) ; return 0 ; }
Remove vowels from a string stored in a Binary Tree | C ++ program for the above approach ; Structure Representing the Node in the Binary tree ; Function to perform a level order insertion of a new Node in the Binary tree ; If the root is empty , make it point to the new Node ; In case there are elements in the Binary tree , perform a level order traversal using a Queue ; If the left child does not exist , insert the new Node as the left child ; In case the right child does not exist , insert the new Node as the right child ; Function to print the level order traversal of the Binary tree ; Function to check if the character is a vowel or not . ; Function to remove the vowels in the new Binary tree ; Declaring the root of the new tree ; If the given character is not a vowel , add it to the new Binary tree ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { char data ; Node * left , * right ; Node ( char _val ) { data = _val ; left = right = NULL ; } } ; Node * addinBT ( Node * root , char data ) { if ( root == NULL ) { root = new Node ( data ) ; } else { queue < Node * > Q ; Q . push ( root ) ; while ( ! Q . empty ( ) ) { Node * temp = Q . front ( ) ; Q . pop ( ) ; if ( temp -> left == NULL ) { temp -> left = new Node ( data ) ; break ; } else Q . push ( temp -> left ) ; if ( temp -> right == NULL ) { temp -> right = new Node ( data ) ; break ; } else Q . push ( temp -> right ) ; } } return root ; } void print ( Node * root ) { queue < Node * > Q ; Q . push ( root ) ; while ( Q . size ( ) ) { Node * temp = Q . front ( ) ; Q . pop ( ) ; cout << temp -> data ; if ( temp -> left ) Q . push ( temp -> left ) ; if ( temp -> right ) Q . push ( temp -> right ) ; } } bool checkvowel ( char ch ) { ch = tolower ( ch ) ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } else { return false ; } } Node * removevowels ( Node * root ) { queue < Node * > Q ; Q . push ( root ) ; Node * root1 = NULL ; while ( ! Q . empty ( ) ) { Node * temp = Q . front ( ) ; Q . pop ( ) ; if ( ! checkvowel ( temp -> data ) ) { root1 = addinBT ( root1 , temp -> data ) ; } if ( temp -> left ) { Q . push ( temp -> left ) ; } if ( temp -> right ) { Q . push ( temp -> right ) ; } } return root1 ; } int main ( ) { string s = " geeks " ; Node * root = NULL ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { root = addinBT ( root , s [ i ] ) ; } root = removevowels ( root ) ; print ( root ) ; return 0 ; }
Number of sub | C ++ implementation of the approach ; Function to return the count of the required substrings ; To store the final answer ; Loop to find the answer ; Condition to update the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubStr ( string str , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) ans += ( i + 1 ) ; } return ans ; } int main ( ) { string str = "10010" ; int len = str . length ( ) ; cout << countSubStr ( str , len ) ; return 0 ; }
Smallest non | C ++ implementation of the approach ; Function to return the length of the smallest substring divisible by 2 ^ k ; To store the final answer ; Left pointer ; Right pointer ; Count of the number of zeros and ones in the current substring ; Loop for two pointers ; Condition satisfied ; Updated the answer ; Update the pointer and count ; Update the pointer and count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLength ( string s , int k ) { int ans = INT_MAX ; int l = 0 ; int r = 0 ; int cnt_zero = 0 , cnt_one = 0 ; while ( l < s . size ( ) and r <= s . size ( ) ) { if ( cnt_zero >= k and cnt_one >= 1 ) { ans = min ( ans , r - l ) ; l ++ ; if ( s [ l - 1 ] == '0' ) cnt_zero -- ; else cnt_one -- ; } else { if ( r == s . size ( ) ) break ; if ( s [ r ] == '0' ) cnt_zero ++ ; else cnt_one ++ ; r ++ ; } } if ( ans == INT_MAX ) return -1 ; return ans ; } int main ( ) { string s = "100" ; int k = 2 ; cout << findLength ( s , k ) ; return 0 ; }
Largest sub | C ++ implementation of the approach ; Function to return the largest substring divisible by 2 ; While the last character of the string is '1' , pop it ; If the original string had no '0' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestSubStr ( string s ) { while ( s . size ( ) and s [ s . size ( ) - 1 ] == '1' ) s . pop_back ( ) ; if ( s . size ( ) == 0 ) return " - 1" ; else return s ; } int main ( ) { string s = "11001" ; cout << largestSubStr ( s ) ; return 0 ; }
Find whether X exists in Y after jumbling X | C ++ implementation of the approach ; Function that returns true if both the arrays have equal values ; Test the equality ; Function that returns true if any permutation of X exists as a substring in Y ; Base case ; To store cumulative frequency ; Finding the frequency of characters in X ; Finding the frequency of characters in Y upto the length of X ; Equality check ; Two pointer approach to generate the entire cumulative frequency ; Remove the first character of the previous window ; Add the last character of the current window ; Equality check ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; bool areEqual ( int * a , int * b ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( a [ i ] != b [ i ] ) return false ; return true ; } bool xExistsInY ( string x , string y ) { if ( x . size ( ) > y . size ( ) ) return false ; int cnt_x [ MAX ] = { 0 } ; int cnt [ MAX ] = { 0 } ; for ( int i = 0 ; i < x . size ( ) ; i ++ ) cnt_x [ x [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < x . size ( ) ; i ++ ) cnt [ y [ i ] - ' a ' ] ++ ; if ( areEqual ( cnt_x , cnt ) ) return true ; for ( int i = 1 ; i < y . size ( ) - x . size ( ) + 1 ; i ++ ) { cnt [ y [ i - 1 ] - ' a ' ] -- ; cnt [ y [ i + x . size ( ) - 1 ] - ' a ' ] ++ ; if ( areEqual ( cnt , cnt_x ) ) return true ; } return false ; } int main ( ) { string x = " skege " ; string y = " geeksforgeeks " ; if ( xExistsInY ( x , y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Reverse substrings between each pair of parenthesis | C ++ implementation of the approach ; Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substring starting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reverseParentheses ( string str , int len ) { stack < int > st ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ( ' ) { st . push ( i ) ; } else if ( str [ i ] == ' ) ' ) { reverse ( str . begin ( ) + st . top ( ) + 1 , str . begin ( ) + i ) ; st . pop ( ) ; } } string res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ) ' && str [ i ] != ' ( ' ) res += ( str [ i ] ) ; } return res ; } int main ( ) { string str = " ( skeeg ( for ) skeeg ) " ; int len = str . length ( ) ; cout << reverseParentheses ( str , len ) ; return 0 ; }
Count of times second string can be formed from the characters of first string | C ++ implementation of the approach ; Function to update the freq [ ] array to store the frequencies of all the characters of str ; Update the frequency of the characters ; Function to return the maximum count of times patt can be formed using the characters of str ; To store the frequencies of all the characters of str ; To store the frequencies of all the characters of patt ; To store the result ; For every character ; If the current character doesn 't appear in patt ; Update the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; void updateFreq ( string str , int freq [ ] ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } } int maxCount ( string str , string patt ) { int strFreq [ MAX ] = { 0 } ; updateFreq ( str , strFreq ) ; int pattFreq [ MAX ] = { 0 } ; updateFreq ( patt , pattFreq ) ; int ans = INT_MAX ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } int main ( ) { string str = " geeksforgeeks " ; string patt = " geeks " ; cout << maxCount ( str , patt ) ; return 0 ; }
Reduce the number to minimum multiple of 4 after removing the digits | C ++ implementation of the approach ; Function to return the minimum number that can be formed after removing the digits which is a multiple of 4 ; For every digit of the number ; Check if the current digit is divisible by 4 ; If any subsequence of two digits is divisible by 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int TEN = 10 ; int minNum ( string str , int len ) { int res = INT_MAX ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '4' str [ i ] == '8' ) { res = min ( res , str [ i ] - '0' ) ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { int num = ( str [ i ] - '0' ) * TEN + ( str [ j ] - '0' ) ; if ( num % 4 == 0 ) { res = min ( res , num ) ; } } } return ( ( res == INT_MAX ) ? -1 : res ) ; } int main ( ) { string str = "17" ; int len = str . length ( ) ; cout << minNum ( str , len ) ; return 0 ; }
Check if two strings can be made equal by swapping one character among each other | C ++ implementation of the approach ; Function that returns true if the string can be made equal after one swap ; A and B are new a and b after we omit the same elements ; Take only the characters which are different in both the strings for every pair of indices ; If the current characters differ ; The strings were already equal ; If the lengths of the strings are two ; If swapping these characters can make the strings equal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeEqual ( string a , string b , int n ) { vector < char > A , B ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . push_back ( a [ i ] ) ; B . push_back ( b [ i ] ) ; } } if ( A . size ( ) == B . size ( ) and B . size ( ) == 0 ) return true ; if ( A . size ( ) == B . size ( ) and B . size ( ) == 2 ) { if ( A [ 0 ] == A [ 1 ] and B [ 0 ] == B [ 1 ] ) return true ; } return false ; } int main ( ) { string A = " SEEKSFORGEEKS " ; string B = " GEEKSFORGEEKG " ; if ( canBeEqual ( A , B , A . size ( ) ) ) printf ( " Yes " ) ; else printf ( " No " ) ; }