codeparrot/xlcost-text-to-code huggingface dataset:
Convert the string into palindrome string by changing only one character | CPP program to Check if it is possible to convert the string into palindrome string by changing only one character . ; Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPalindrome ( string str ) { int n = str . length ( ) ; int count = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) if ( str [ i ] != str [ n - i - 1 ] ) ++ count ; return ( count <= 1 ) ; } int main ( ) { string str = " abccaa " ; if ( checkPalindrome ( str ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Counting even decimal value substrings in a binary string | Program to count all even decimal value substring . ; function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generate by this character produce even decimal value . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenDecimalValue ( string str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { result += ( n - i ) ; } } return result ; } int main ( ) { string str = "10010" ; int n = 5 ; cout << evenDecimalValue ( str , n ) << endl ; return 0 ; }
Number of substrings with odd decimal value in a binary string | CPP program to count substrings with odd decimal value ; function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; store count of 1 's before  i-th index ; variable to store answer ; traverse the string reversely to calculate number of odd substrings before i - th index ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countSubstr ( string s ) { int n = s . length ( ) ; int auxArr [ n ] = { 0 } ; if ( s [ 0 ] == '1' ) auxArr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) auxArr [ i ] = auxArr [ i - 1 ] + 1 ; else auxArr [ i ] = auxArr [ i - 1 ] ; } int count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) if ( s [ i ] == '1' ) count += auxArr [ i ] ; return count ; } int main ( ) { string s = "1101" ; cout << countSubstr ( s ) ; return 0 ; }
Find the starting indices of the substrings in string ( S ) which is made by concatenating all words from a list ( L ) | CPP program to calculate the starting indices of substrings inside S which contains all the words present in List L . ; Returns an integer vector consisting of starting indices of substrings present inside the string S ; Number of a characters of a word in list L . ; Number of words present inside list L . ; Total characters present in list L . ; Resultant vector which stores indices . ; If the total number of characters in list L is more than length of string S itself . ; Map stores the words present in list L against it 's occurrences inside list L ; Traverse the substring ; Extract the word ; If word not found or if frequency of current word is more than required simply break . ; Else decrement the count of word from hash_map ; Store the starting index of that substring when all the words in the list are in substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findSubstringIndices ( string S , const vector < string > & L ) { int size_word = L [ 0 ] . size ( ) ; int word_count = L . size ( ) ; int size_L = size_word * word_count ; vector < int > res ; if ( size_L > S . size ( ) ) return res ; unordered_map < string , int > hash_map ; for ( int i = 0 ; i < word_count ; i ++ ) hash_map [ L [ i ] ] ++ ; for ( int i = 0 ; i <= S . size ( ) - size_L ; i ++ ) { unordered_map < string , int > temp_hash_map ( hash_map ) ; int j = i , count = word_count ; while ( j < i + size_L ) { string word = S . substr ( j , size_word ) ; if ( hash_map . find ( word ) == hash_map . end ( ) temp_hash_map [ word ] == 0 ) break ; else { temp_hash_map [ word ] -- ; count -- ; } j += size_word ; } if ( count == 0 ) res . push_back ( i ) ; } return res ; } int main ( ) { string S = " barfoothefoobarman " ; vector < string > L = { " foo " , " bar " } ; vector < int > indices = findSubstringIndices ( S , L ) ; for ( int i = 0 ; i < indices . size ( ) ; i ++ ) cout << indices [ i ] << " ▁ " ; return 0 ; }
Check whether second string can be formed from characters of first string | CPP program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in str1 . ; Now traverse through str2 to check if every character has enough counts ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 256 ; bool canMakeStr2 ( string str1 , string str2 ) { int count [ MAX ] = { 0 } ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count [ str1 [ i ] ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] -- ; } return true ; } int main ( ) { string str1 = " geekforgeeks " ; string str2 = " for " ; if ( canMakeStr2 ( str1 , str2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Position of robot after given movements | C ++ implementation to find final position of robot after the complete movement ; Function to find final position of robot after the complete movement ; Traverse the instruction string ' move ' ; For each movement increment its respective counter ; Required final position of robot ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalPosition ( string move ) { int l = move . size ( ) ; int countUp = 0 , countDown = 0 ; int countLeft = 0 , countRight = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' U ' ) countUp ++ ; else if ( move [ i ] == ' D ' ) countDown ++ ; else if ( move [ i ] == ' L ' ) countLeft ++ ; else if ( move [ i ] == ' R ' ) countRight ++ ; } cout << " Final ▁ Position : ▁ ( " << ( countRight - countLeft ) << " , ▁ " << ( countUp - countDown ) << " ) " << endl ; } int main ( ) { string move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( move ) ; return 0 ; }
Length of longest balanced parentheses prefix | CPP Program to find length of longest balanced parentheses prefix . ; Return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxbalancedprefix ( char str [ ] , int n ) { int sum = 0 ; int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) sum += 1 ; else sum -= 1 ; if ( sum < 0 ) break ; if ( sum == 0 ) maxi = i + 1 ; } return maxi ; } int main ( ) { char str [ ] = " ( ( ( ) ( ) ) ( ) ) ( ( " ; int n = strlen ( str ) ; cout << maxbalancedprefix ( str , n ) << endl ; return 0 ; }
Minimum cost to convert string into palindrome | CPP program to find minimum cost to make a palindrome . ; Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cost ( string str ) { int len = str . length ( ) ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str [ i ] != str [ j ] ) res += min ( str [ i ] , str [ j ] ) - ' a ' + 1 ; return res ; } int main ( ) { string str = " abcdef " ; cout << cost ( str ) << endl ; return 0 ; }
Encoding a word into Pig Latin | C ++ program to encode a word to a Pig Latin . ; the index of the first vowel is stored . ; Pig Latin is possible only if vowels is present ; Take all characters after index ( including index ) . Append all characters which are before index . Finally append " ay " ; 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 ' c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } string pigLatin ( string s ) { int len = s . length ( ) ; int index = -1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( s [ i ] ) ) { index = i ; break ; } } if ( index == -1 ) return " - 1" ; return s . substr ( index ) + s . substr ( 0 , index ) + " ay " ; } int main ( ) { string str = pigLatin ( " graphic " ) ; if ( str == " - 1" ) cout << " No ▁ vowels ▁ found . ▁ Pig ▁ Latin ▁ not ▁ possible " ; else cout << str ; }
Possibility of a word from a given set of characters | CPP program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; bool isPresent ( string s , string q ) { int freq [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s [ i ] ] ++ ; for ( int i = 0 ; i < q . length ( ) ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } return true ; } int main ( ) { string s = " abctd " ; string q = " cat " ; if ( isPresent ( s , q ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum reduce operations to convert a given string into a palindrome | CPP program to count minimum reduce operations to make a palindrome ; Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countReduce ( string & str ) { int n = str . length ( ) ; int res = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) res += abs ( str [ i ] - str [ n - i - 1 ] ) ; return res ; } int main ( ) { string str = " abcd " ; cout << countReduce ( str ) ; return 0 ; }
Minimal operations to make a number magical | CPP program to make a number magical ; function to calculate the minimal changes ; maximum digits that can be changed ; nested loops to generate all 6 digit numbers ; counter to count the number of change required ; if first digit is equal ; if 2 nd digit is equal ; if 3 rd digit is equal ; if 4 th digit is equal ; if 5 th digit is equal ; if 6 th digit is equal ; checks if less then the previous calculate changes ; returns the answer ; driver program to test the above function ; number stored in string ; prints the minimum operations
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int calculate ( string s ) { int ans = 6 ; for ( int i = 0 ; i < 10 ; ++ i ) { for ( int j = 0 ; j < 10 ; ++ j ) { for ( int k = 0 ; k < 10 ; ++ k ) { for ( int l = 0 ; l < 10 ; ++ l ) { for ( int m = 0 ; m < 10 ; ++ m ) { for ( int n = 0 ; n < 10 ; ++ n ) { if ( i + j + k == l + m + n ) { int c = 0 ; if ( i != s [ 0 ] - '0' ) c ++ ; if ( j != s [ 1 ] - '0' ) c ++ ; if ( k != s [ 2 ] - '0' ) c ++ ; if ( l != s [ 3 ] - '0' ) c ++ ; if ( m != s [ 4 ] - '0' ) c ++ ; if ( n != s [ 5 ] - '0' ) c ++ ; if ( c < ans ) ans = c ; } } } } } } } return ans ; } int main ( ) { string s = "123456" ; cout << calculate ( s ) ; }
Check if a two character string can be made using given words | CPP code to check if a two character string can be made using given strings ; Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool makeAndCheckString ( vector < string > words , string str ) { int n = words . size ( ) ; bool first = false , second = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( words [ i ] == str ) return true ; if ( str [ 0 ] == words [ i ] [ 1 ] ) first = true ; if ( str [ 1 ] == words [ i ] [ 0 ] ) second = true ; if ( first && second ) return true ; } return false ; } int main ( ) { string str = " ya " ; vector < string > words = { " ah " , " oy " , " to " , " ha " } ; if ( makeAndCheckString ( words , str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Print N | C ++ program to print all N - bit binary ; Function to get the binary representation of the number N ; loop for each bit ; generate numbers in the range of ( 2 ^ N ) - 1 to 2 ^ ( N - 1 ) inclusive ; longest prefix check ; if counts of 1 is greater than counts of zero ; do sub - prefixes check ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; string getBinaryRep ( int N , int num_of_bits ) { string r = " " ; num_of_bits -- ; while ( num_of_bits >= 0 ) { if ( N & ( 1 << num_of_bits ) ) r . append ( "1" ) ; else r . append ( "0" ) ; num_of_bits -- ; } return r ; } vector < string > NBitBinary ( int N ) { vector < string > r ; int first = 1 << ( N - 1 ) ; int last = first * 2 ; for ( int i = last - 1 ; i >= first ; -- i ) { int zero_cnt = 0 ; int one_cnt = 0 ; int t = i ; int num_of_bits = 0 ; while ( t ) { if ( t & 1 ) one_cnt ++ ; else zero_cnt ++ ; num_of_bits ++ ; t = t >> 1 ; } if ( one_cnt >= zero_cnt ) { bool all_prefix_match = true ; int msk = ( 1 << num_of_bits ) - 2 ; int prefix_shift = 1 ; while ( msk ) { int prefix = ( msk & i ) >> prefix_shift ; int prefix_one_cnt = 0 ; int prefix_zero_cnt = 0 ; while ( prefix ) { if ( prefix & 1 ) prefix_one_cnt ++ ; else prefix_zero_cnt ++ ; prefix = prefix >> 1 ; } if ( prefix_zero_cnt > prefix_one_cnt ) { all_prefix_match = false ; break ; } prefix_shift ++ ; msk = msk & ( msk << 1 ) ; } if ( all_prefix_match ) { r . push_back ( getBinaryRep ( i , num_of_bits ) ) ; } } } return r ; } int main ( ) { int n = 4 ; vector < string > results = NBitBinary ( n ) ; for ( int i = 0 ; i < results . size ( ) ; ++ i ) cout << results [ i ] << " ▁ " ; cout << endl ; return 0 ; }
Find winner of an election where votes are represented as candidate names | C ++ ++ program to find winner in an election . ; We have four Candidates with name as ' John ' , ' Johnny ' , ' jamie ' , ' jackie ' . The votes in String array are as per the votes casted . Print the name of candidates received Max vote . ; Insert all votes in a hashmap ; Traverse through map to find the candidate with maximum votes . ; If there is a tie , pick lexicographically smaller . ; Driver code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void findWinner ( vector < string > & votes ) { unordered_map < string , int > mapObj ; for ( auto & str : votes ) { mapObj [ str ] ++ ; } int maxValueInMap = 0 ; string winner ; for ( auto & entry : mapObj ) { string key = entry . first ; int val = entry . second ; if ( val > maxValueInMap ) { maxValueInMap = val ; winner = key ; } else if ( val == maxValueInMap && winner > key ) winner = key ; } cout << winner << endl ; } int main ( ) { vector < string > votes = { " john " , " johnny " , " jackie " , " johnny " , " john " , " jackie " , " jamie " , " jamie " , " john " , " johnny " , " jamie " , " johnny " , " john " } ; findWinner ( votes ) ; return 0 ; }
Luhn algorithm | C ++ program to implement Luhn algorithm ; Returns true if given card number is valid ; We add two digits to handle cases that make two digits after doubling ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkLuhn ( const string & cardNo ) { int nDigits = cardNo . length ( ) ; int nSum = 0 , isSecond = false ; for ( int i = nDigits - 1 ; i >= 0 ; i -- ) { int d = cardNo [ i ] - '0' ; if ( isSecond == true ) d = d * 2 ; nSum += d / 10 ; nSum += d % 10 ; isSecond = ! isSecond ; } return ( nSum % 10 == 0 ) ; } int main ( ) { string cardNo = "79927398713" ; if ( checkLuhn ( cardNo ) ) printf ( " This ▁ is ▁ a ▁ valid ▁ card " ) ; else printf ( " This ▁ is ▁ not ▁ a ▁ valid ▁ card " ) ; return 0 ; }
Distributing all balls without repetition | CPP program to find if its possible to distribute balls without repitiion ; function to find if its possible to distribute balls or not ; count array to count how many times each color has occurred ; increasing count of each color every time it appears ; to check if any color appears more than K times if it does we will print NO ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool distributingBalls ( int k , int n , string str ) { int a [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } int main ( ) { long long int n = 6 , k = 3 ; string str = " aacaab " ; if ( distributingBalls ( k , n , str ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find substrings that contain all vowels | CPP program to find all substring that contain all vowels ; Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Outer loop picks starting character and inner loop picks ending character . ; If current character is not vowel , then no more result substrings possible starting from str [ i ] . ; If vowel , then we insert it in hash ; If all vowels are present in current substring ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } void FindSubstring ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( str [ j ] ) == false ) break ; hash . insert ( str [ j ] ) ; if ( hash . size ( ) == 5 ) cout << str . substr ( i , j - i + 1 ) << " ▁ " ; } hash . clear ( ) ; } } int main ( ) { string str = " aeoibsddaeiouudb " ; FindSubstring ( str ) ; return 0 ; }
Find if an array contains a string with one mismatch | C ++ program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < string > list , string s ) { int n = ( int ) list . size ( ) ; if ( n == 0 ) return false ; for ( int i = 0 ; i < n ; i ++ ) { if ( list [ i ] . size ( ) != s . size ( ) ) continue ; bool diff = false ; for ( int j = 0 ; j < ( int ) list [ i ] . size ( ) ; j ++ ) { if ( list [ i ] [ j ] != s [ j ] ) { if ( ! diff ) diff = true ; else { diff = false ; break ; } } } if ( diff ) return true ; } return false ; } int main ( ) { vector < string > s ; s . push_back ( " bana " ) ; s . push_back ( " apple " ) ; s . push_back ( " banacb " ) ; s . push_back ( " bonanza " ) ; s . push_back ( " banamf " ) ; cout << check ( s , " banana " ) ; return 0 ; }
Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | CPP program to find if a sentence is palindrome ; To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver program to test sentencePalindrome ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sentencePalindrome ( string str ) { int l = 0 , h = str . length ( ) - 1 ; for ( int i = 0 ; i <= h ; i ++ ) str [ i ] = tolower ( str [ i ] ) ; while ( l <= h ) { if ( ! ( str [ l ] >= ' a ' && str [ l ] <= ' z ' ) ) l ++ ; else if ( ! ( str [ h ] >= ' a ' && str [ h ] <= ' z ' ) ) h -- ; else if ( str [ l ] == str [ h ] ) l ++ , h -- ; else return false ; } return true ; } int main ( ) { string str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( str ) ) cout << " Sentence ▁ is ▁ palindrome . " ; else cout << " Sentence ▁ is ▁ not ▁ palindrome . " ; return 0 ; }
Ways to remove one element from a binary string so that XOR becomes zero | C ++ program to count number of ways to remove an element so that XOR of remaining string becomes 0. ; Return number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorZero ( string str ) { int one_count = 0 , zero_count = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } int main ( ) { string str = "11111" ; cout << xorZero ( str ) << endl ; return 0 ; }
Keyword Cipher | CPP program for encoding the string using classical cipher ; Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Driver code ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text
#include <bits/stdc++.h> NEW_LINE using namespace std ; string encoder ( string key ) { string encoded = " " ; bool arr [ 26 ] = { 0 } ; for ( int i = 0 ; i < key . size ( ) ; i ++ ) { if ( key [ i ] >= ' A ' && key [ i ] <= ' Z ' ) { if ( arr [ key [ i ] - 65 ] == 0 ) { encoded += key [ i ] ; arr [ key [ i ] - 65 ] = 1 ; } } else if ( key [ i ] >= ' a ' && key [ i ] <= ' z ' ) { if ( arr [ key [ i ] - 97 ] == 0 ) { encoded += key [ i ] - 32 ; arr [ key [ i ] - 97 ] = 1 ; } } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == 0 ) { arr [ i ] = 1 ; encoded += char ( i + 65 ) ; } } return encoded ; } string cipheredIt ( string msg , string encoded ) { string cipher = " " ; for ( int i = 0 ; i < msg . size ( ) ; i ++ ) { if ( msg [ i ] >= ' a ' && msg [ i ] <= ' z ' ) { int pos = msg [ i ] - 97 ; cipher += encoded [ pos ] ; } else if ( msg [ i ] >= ' A ' && msg [ i ] <= ' Z ' ) { int pos = msg [ i ] - 65 ; cipher += encoded [ pos ] ; } else { cipher += msg [ i ] ; } } return cipher ; } int main ( ) { string key ; key = " Computer " ; cout << " Keyword ▁ : ▁ " << key << endl ; string encoded = encoder ( key ) ; string message = " GeeksforGeeks " ; cout << " Message ▁ before ▁ Ciphering ▁ : ▁ " << message << endl ; cout << " Ciphered ▁ Text ▁ : ▁ " << cipheredIt ( message , encoded ) << endl ; return 0 ; }
Check if both halves of the string have same set of characters | C ++ program to check if it is possible to split string or not ; function to check if we can split string or not ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; Driver program to test above function ; String to be checked
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool checkCorrectOrNot ( string s ) { int count [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - ' a ' ] ++ ; count [ s [ j ] - ' a ' ] -- ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } int main ( ) { string s = " abab " ; if ( checkCorrectOrNot ( s ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }