codeparrot/xlcost-text-to-code huggingface dataset:
Common characters in n strings | CPP Program to find all the common characters in n strings ; primary array for common characters we assume all characters are seen before . ; for each string ; secondary array for common characters Initially marked false ; for every character of ith string ; if character is present in all strings before , mark it . ; copy whole secondary array into primary ; displaying common characters ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; void commonCharacters ( string str [ ] , int n ) { bool prim [ MAX_CHAR ] ; memset ( prim , true , sizeof ( prim ) ) ; for ( int i = 0 ; i < n ; i ++ ) { bool sec [ MAX_CHAR ] = { false } ; for ( int j = 0 ; str [ i ] [ j ] ; j ++ ) { if ( prim [ str [ i ] [ j ] - ' a ' ] ) sec [ str [ i ] [ j ] - ' a ' ] = true ; } memcpy ( prim , sec , MAX_CHAR ) ; } for ( int i = 0 ; i < 26 ; i ++ ) if ( prim [ i ] ) printf ( " % c ▁ " , i + ' a ' ) ; } int main ( ) { string str [ ] = { " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " } ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; commonCharacters ( str , n ) ; return 0 ; }
Number of positions where a letter can be inserted such that a string becomes palindrome | CPP code to find the no . of positions where a letter can be inserted to make it a palindrome ; Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if ( n % 2 == 0 ) if the length is even ; count = 2 * count + 1 ; sub - case - I ; count = 2 * count + 2 ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string & s , int i , int j ) { int p = j ; for ( int k = i ; k <= p ; k ++ ) { if ( s [ k ] != s [ p ] ) return false ; p -- ; } return true ; } int countWays ( string & s ) { int n = s . length ( ) ; int count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( int i = n / 2 ; i < n ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) count ++ ; else break ; } { count ++ ; } else } else { for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( s [ i ] != s [ n - 1 - i ] ) { int j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( s [ k ] != s [ j ] ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( int k = n - i ; k < n ; k ++ ) { if ( s [ k ] != s [ i ] ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } int main ( ) { string s = " abca " ; cout << countWays ( s ) << endl ; return 0 ; }
Count of substrings of a binary string containing K ones | C ++ program to find count of substring containing exactly K ones ; method returns total number of substring having K ones ; initialize index having zero sum as 1 ; loop over binary characters of string ; update countOfOne variable with value of ith character ; if value reaches more than K , then update result ; add frequency of indices , having sum ( current sum - K ) , to the result ; update frequency of one 's count ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfSubstringWithKOnes ( string s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int freq [ N + 1 ] = { 0 } ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s [ i ] - '0' ) ; if ( countOfOne >= K ) { res += freq [ countOfOne - K ] ; } freq [ countOfOne ] ++ ; } return res ; } int main ( ) { string s = "10010" ; int K = 1 ; cout << countOfSubstringWithKOnes ( s , K ) << endl ; return 0 ; }
Generate two output strings depending upon occurrence of character in input string . | CPP program to print two strings made of character occurring once and multiple times ; function to print two strings generated from single string one with characters occurring onces other with character occurring multiple of times ; initialize hashtable with zero entry ; perform hashing for input string ; generate string ( str1 ) consisting char occurring once and string ( str2 ) consisting char occurring multiple times ; print both strings ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; void printDuo ( string & str ) { int countChar [ MAX_CHAR ] = { 0 } ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) countChar [ str [ i ] - ' a ' ] ++ ; string str1 = " " , str2 = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( countChar [ i ] > 1 ) str2 = str2 + ( char ) ( i + ' a ' ) ; else if ( countChar [ i ] == 1 ) str1 = str1 + ( char ) ( i + ' a ' ) ; } cout << " String ▁ with ▁ characters ▁ occurring ▁ " << " once : STRNEWLINE " ; cout << str1 << " STRNEWLINE " ; cout << " String ▁ with ▁ characters ▁ occurring ▁ " << " multiple ▁ times : STRNEWLINE " ; cout << str2 << " STRNEWLINE " ; } int main ( ) { string str = " lovetocode " ; printDuo ( str ) ; return 0 ; }
Next higher palindromic number using the same set of digits | C ++ implementation to find next higher palindromic number using the same set of digits ; function to reverse the digits in the range i to j in ' num ' ; function to find next higher palindromic number using the same set of digits ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; Start from the ( mid - 1 ) th digit and find the first digit that is smaller than the digit next to it . ; If no such digit is found , then all digits are in descending order which means there cannot be a greater palindromic number with same set of digits ; Find the smallest digit on right side of ith digit which is greater than num [ i ] up to index ' mid ' ; swap num [ i ] with num [ smallest ] ; as the number is a palindrome , the same swap of digits should be performed in the 2 nd half of ' num ' ; reverse digits in the range ( i + 1 ) to mid ; if n is even , then reverse digits in the range mid + 1 to n - i - 2 ; else if n is odd , then reverse digits in the range mid + 2 to n - i - 2 ; required next higher palindromic number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverse ( char num [ ] , int i , int j ) { while ( i < j ) { swap ( num [ i ] , num [ j ] ) ; i ++ ; j -- ; } } void nextPalin ( char num [ ] , int n ) { if ( n <= 3 ) { cout << " Not ▁ Possible " ; return ; } int mid = n / 2 - 1 ; int i , j ; for ( i = mid - 1 ; i >= 0 ; i -- ) if ( num [ i ] < num [ i + 1 ] ) break ; if ( i < 0 ) { cout << " Not ▁ Possible " ; return ; } int smallest = i + 1 ; for ( j = i + 2 ; j <= mid ; j ++ ) if ( num [ j ] > num [ i ] && num [ j ] <= num [ smallest ] ) smallest = j ; swap ( num [ i ] , num [ smallest ] ) ; swap ( num [ n - i - 1 ] , num [ n - smallest - 1 ] ) ; reverse ( num , i + 1 , mid ) ; if ( n % 2 == 0 ) reverse ( num , mid + 1 , n - i - 2 ) ; else reverse ( num , mid + 2 , n - i - 2 ) ; cout << " Next ▁ Palindrome : ▁ " << num ; } int main ( ) { char num [ ] = "4697557964" ; int n = strlen ( num ) ; nextPalin ( num , n ) ; return 0 ; }
Print N | C ++ program to print all N - bit binary ; function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRec ( string number , int extraOnes , int remainingPlaces ) { if ( 0 == remainingPlaces ) { cout << number << " ▁ " ; return ; } printRec ( number + "1" , extraOnes + 1 , remainingPlaces - 1 ) ; if ( 0 < extraOnes ) printRec ( number + "0" , extraOnes - 1 , remainingPlaces - 1 ) ; } void printNums ( int n ) { string str = " " ; printRec ( str , 0 , n ) ; } int main ( ) { int n = 4 ; printNums ( n ) ; return 0 ; }
Longest Common Substring in an Array of Strings | C ++ program to find the stem of given list of words ; function to find the stem ( longest commonsubstring ) from the string array ; Determine size of the array ; Take first word from array as reference ; generating all possible substrings of our reference string arr [ 0 ] i . e s ; Check if the generated stem is common to all words ; If current substring is present in all strings and its length is greater than current result ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findstem ( vector < string > arr ) { int n = arr . size ( ) ; string s = arr [ 0 ] ; int len = s . length ( ) ; string res = " " ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j <= len ; j ++ ) { string stem = s . substr ( i , j ) ; int k = 1 ; for ( k = 1 ; k < n ; k ++ ) { if ( arr [ k ] . find ( stem ) == std :: string :: npos ) break ; } if ( k == n && res . length ( ) < stem . length ( ) ) res = stem ; } } return res ; } int main ( ) { vector < string > arr { " grace " , " graceful " , " disgraceful " , " gracefully " } ; string stems = findstem ( arr ) ; cout << stems << endl ; }
Make a string from another by deletion and rearrangement of characters | CPP program to find if it is possible to make a string from characters present in other string . ; Returns true if it is possible to make s1 from characters present in s2 . ; Count occurrences of all characters present in s2 . . ; For every character , present in s1 , reduce its count if count is more than 0. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; bool isPossible ( string & s1 , string & s2 ) { int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) count [ s2 [ i ] ] ++ ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( count [ s1 [ i ] ] == 0 ) return false ; count [ s1 [ i ] ] -- ; } return true ; } int main ( ) { string s1 = " GeeksforGeeks " , s2 = " rteksfoGrdsskGeggehes " ; if ( isPossible ( s1 , s2 ) ) cout << " Possible " ; else cout << " Not ▁ Possible STRNEWLINE " ; return 0 ; }
Next higher number using atmost one swap operation | C ++ implementation to find the next higher number using atmost one swap operation ; function to find the next higher number using atmost one swap operation ; to store the index of the largest digit encountered so far from the right ; to store the index of rightmost digit which has a digit greater to it on its right side ; finding the ' index ' of rightmost digit which has a digit greater to it on its right side ; required digit found , store its ' index ' and break ; if no such digit is found which has a larger digit on its right side ; to store the index of the smallest digit greater than the digit at ' index ' and right to it ; finding the index of the smallest digit greater than the digit at ' index ' and right to it ; swapping the digits ; required number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string nxtHighUsingAtMostOneSwap ( string num ) { int l = num . size ( ) ; int posRMax = l - 1 ; int index = -1 ; for ( int i = l - 2 ; i >= 0 ; i -- ) { if ( num [ i ] >= num [ posRMax ] ) posRMax = i ; else { index = i ; break ; } } if ( index == -1 ) return " Not ▁ Possible " ; int greatSmallDgt = -1 ; for ( int i = l - 1 ; i > index ; i -- ) { if ( num [ i ] > num [ index ] ) { if ( greatSmallDgt == -1 ) greatSmallDgt = i ; else if ( num [ i ] <= num [ greatSmallDgt ] ) greatSmallDgt = i ; } } char temp = num [ index ] ; num [ index ] = num [ greatSmallDgt ] ; num [ greatSmallDgt ] = temp ; return num ; } int main ( ) { string num = "218765" ; cout << " Original ▁ number : ▁ " << num << endl ; cout << " Next ▁ higher ▁ number : ▁ " << nxtHighUsingAtMostOneSwap ( num ) ; return 0 ; }
Longest substring of vowels | CPP program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; check previous value is greater then or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } int longestVowel ( string s ) { int count = 0 , res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) count ++ ; else { res = max ( res , count ) ; count = 0 ; } } return max ( res , count ) ; } int main ( ) { string s = " theeare " ; cout << longestVowel ( s ) << endl ; return 0 ; }
Number of substrings with count of each character as k | C ++ program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool check ( int freq [ ] , int k ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] && freq [ i ] != k ) return false ; return true ; } int substrings ( string s , int k ) { for ( int i = 0 ; s [ i ] ; i ++ ) { int freq [ MAX_CHAR ] = { 0 } ; for ( int j = i ; s [ j ] ; j ++ ) { int index = s [ j ] - ' a ' ; freq [ index ] ++ ; if ( freq [ index ] > k ) break ; else if ( freq [ index ] == k && check ( freq , k ) == true ) res ++ ; } } return res ; } int main ( ) { string s = " aabbcc " ; int k = 2 ; cout << substrings ( s , k ) << endl ; s = " aabbc " ; k = 2 ; cout << substrings ( s , k ) << endl ; }
Number of substrings with count of each character as k |
#include <iostream> NEW_LINE #include <map> NEW_LINE #include <set> NEW_LINE #include <string> NEW_LINE int min ( int a , int b ) { return a < b ? a : b ; } using namespace std ; bool have_same_frequency ( map < char , int > & freq , int k ) { for ( auto & pair : freq ) { if ( pair . second != k && pair . second != 0 ) { return false ; } } return true ; } int count_substrings ( string s , int k ) { int count = 0 ; int distinct = ( set < char > ( s . begin ( ) , s . end ( ) ) ) . size ( ) ; for ( int length = 1 ; length <= distinct ; length ++ ) { int window_length = length * k ; map < char , int > freq ; int window_start = 0 ; int window_end = window_start + window_length - 1 ; for ( int i = window_start ; i <= min ( window_end , s . length ( ) - 1 ) ; i ++ ) { freq [ s [ i ] ] ++ ; } while ( window_end < s . length ( ) ) { if ( have_same_frequency ( freq , k ) ) { count ++ ; } freq [ s [ window_start ] ] -- ; window_start ++ ; window_end ++ ; if ( window_length < s . length ( ) ) { freq [ s [ window_end ] ] ++ ; } } } return count ; } int main ( ) { string s = " aabbcc " ; int k = 2 ; cout << count_substrings ( s , k ) << endl ; s = " aabbc " ; k = 2 ; cout << count_substrings ( s , k ) << endl ; return 0 ; }
Frequency of a string in an array of strings | C ++ program to count number of times a string appears in an array of strings ; To store number of times a string is present . It is 0 is string is not present ; function to insert a string into the Trie ; calculation ascii value ; If the given node is not already present in the Trie than create the new node ; Returns count of occurrences of s in Trie ; inserting in Trie ; searching the strings in Trie ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; struct Trie { int cnt ; Trie * node [ MAX_CHAR ] ; Trie ( ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) node [ i ] = NULL ; cnt = 0 ; } } ; Trie * insert ( Trie * root , string s ) { Trie * temp = root ; int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int index = s [ i ] - ' a ' ; if ( ! root -> node [ index ] ) root -> node [ index ] = new Trie ( ) ; root = root -> node [ index ] ; } root -> cnt ++ ; return temp ; } int search ( Trie * root , string s ) { int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int index = s [ i ] - ' a ' ; if ( ! root -> node [ index ] ) return 0 ; root = root -> node [ index ] ; } return root -> cnt ; } void answerQueries ( string arr [ ] , int n , string q [ ] , int m ) { Trie * root = new Trie ( ) ; for ( int i = 0 ; i < n ; i ++ ) root = insert ( root , arr [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) cout << search ( root , q [ i ] ) << " ▁ " ; } int main ( ) { string arr [ ] = { " aba " , " baba " , " aba " , " xzxb " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string q [ ] = { " aba " , " xzxb " , " ab " } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; answerQueries ( arr , n , q , m ) ; return 0 ; }
Longest subsequence where every character appears at | C ++ program to Find longest subsequence where every character appears at - least k times ; Count frequencies of all characters ; Traverse given string again and print all those characters whose frequency is more than or equal to k . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHARS = 26 ; void longestSubseqWithK ( string str , int k ) { int n = str . size ( ) ; int freq [ MAX_CHARS ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] - ' a ' ] >= k ) cout << str [ i ] ; } int main ( ) { string str = " geeksforgeeks " ; int k = 2 ; longestSubseqWithK ( str , k ) ; return 0 ; }
Generating distinct subsequences of a given string in lexicographic order | C ++ program to print all distinct subsequences of a string . ; Finds and stores result in st for a given string s . ; If current string is not already present . ; Traverse current string , one by one remove every character and recur . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generate ( set < string > & st , string s ) { if ( s . size ( ) == 0 ) return ; if ( st . find ( s ) == st . end ( ) ) { st . insert ( s ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { string t = s ; t . erase ( i , 1 ) ; generate ( st , t ) ; } } return ; } int main ( ) { string s = " xyz " ; set < string > st ; set < string > :: iterator it ; generate ( st , s ) ; for ( auto it = st . begin ( ) ; it != st . end ( ) ; it ++ ) cout << * it << endl ; return 0 ; }
Print all subsequences of a string | CPP program to print all subsequence of a given string . ; set to store all the subsequences ; Function computes all the subsequence of an string ; Iterate over the entire string ; Iterate from the end of the string to generate substrings ; Drop kth character in the substring and if its not in the set then recur ; Drop character from the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < string > st ; void subsequence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = str . length ( ) ; j > i ; j -- ) { string sub_str = str . substr ( i , j ) ; st . insert ( sub_str ) ; for ( int k = 1 ; k < sub_str . length ( ) ; k ++ ) { string sb = sub_str ; sb . erase ( sb . begin ( ) + k ) ; subsequence ( sb ) ; } } } } int main ( ) { string s = " aabc " ; subsequence ( s ) ; for ( auto i : st ) cout << i << " ▁ " ; cout << endl ; return 0 ; }
Print all subsequences of a string | CPP program to generate power set in lexicographic order . ; str : Stores input string n : Length of str . curr : Stores current permutation index : Index in current permutation , curr ; base case ; backtracking ; Generates power set in lexicographic order . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubSeqRec ( string str , int n , int index = -1 , string curr = " " ) { if ( index == n ) return ; if ( ! curr . empty ( ) ) { cout << curr << " STRNEWLINE " ; } for ( int i = index + 1 ; i < n ; i ++ ) { curr += str [ i ] ; printSubSeqRec ( str , n , i , curr ) ; curr = curr . erase ( curr . size ( ) - 1 ) ; } return ; } void printSubSeq ( string str ) { printSubSeqRec ( str , str . size ( ) ) ; } int main ( ) { string str = " cab " ; printSubSeq ( str ) ; return 0 ; }
Recursive solution to count substrings with same first and last characters | c ++ program to count substrings with same first and last characters ; Function to count substrings with same first and last characters ; base cases ; driver code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int countSubstrs ( string str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str [ i ] == str [ j ] ) res ++ ; return res ; } int main ( ) { string str = " abcab " ; int n = str . length ( ) ; cout << countSubstrs ( str , 0 , n - 1 , n ) ; }
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | C ++ Program to find minimum number of manipulations required to make two strings identical ; Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countManipulations ( string s1 , string s2 ) { int count = 0 ; int char_count [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { char_count [ i ] = 0 ; } for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { char_count [ s2 [ i ] - ' a ' ] -- ; } for ( int i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += abs ( char_count [ i ] ) ; } } return count / 2 ; } int main ( ) { string s1 = " ddcf " ; string s2 = " cedk " ; cout << countManipulations ( s1 , s2 ) ; }
Least number of manipulations needed to ensure two strings have identical characters | C ++ program to count least number of manipulations to have two strings set of same characters ; return the count of manipulations required ; count the number of different characters in both strings ; check the difference in characters by comparing count arrays ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; int leastCount ( string s1 , string s2 , int n ) { int count1 [ MAX_CHAR ] = { 0 } ; int count2 [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { count1 [ s1 [ i ] - ' a ' ] += 1 ; count2 [ s2 [ i ] - ' a ' ] += 1 ; } int res = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( count1 [ i ] != 0 ) { res += abs ( count1 [ i ] - count2 [ i ] ) ; } } return res ; } int main ( ) { string s1 = " abc " ; string s2 = " cdd " ; int len = s1 . length ( ) ; int res = leastCount ( s1 , s2 , len ) ; cout << res << endl ; return 0 ; }
Given two strings check which string makes a palindrome first | Given two strings , check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; char stringPalindrome ( string A , string B ) { int countA [ MAX_CHAR ] = { 0 } ; int countB [ MAX_CHAR ] = { 0 } ; int l1 = A . length ( ) , l2 = B . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) countA [ A [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) countB [ B [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( ( countA [ i ] > 1 && countB [ i ] == 0 ) ) return ' A ' ; return ' B ' ; } int main ( ) { string a = " abcdea " ; string b = " bcdesg " ; cout << stringPalindrome ( a , b ) ; return 0 ; }