codeparrot/xlcost-text-to-code huggingface dataset:
Find the count of sub | CPP implementation of the approach ; Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOcc ( string s ) { int cnt = 0 ; for ( int i = 0 ; i < s . length ( ) - 3 ; i ++ ) { int c = 0 , l = 0 , a = 0 , p = 0 ; for ( int j = i ; j < i + 4 ; j ++ ) { switch ( s [ j ] ) { case ' c ' : c ++ ; break ; case ' l ' : l ++ ; break ; case ' a ' : a ++ ; break ; case ' p ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } int main ( ) { string s = " clapc " ; transform ( s . begin ( ) , s . end ( ) , s . begin ( ) , :: tolower ) ; cout << ( countOcc ( s ) ) ; }
Count of pairs of strings which differ in exactly one position | CPP implementation of the approach ; Function to return the count of same pairs ; Function to return total number of strings which satisfy required condition ; Dictionary changed will store strings with wild cards Dictionary same will store strings that are equal ; Iterating for all strings in the given array ; If we found the string then increment by 1 Else it will get default value 0 ; Iterating on a single string ; Incrementing the string if found Else it will get default value 0 ; Return counted pairs - equal pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pairCount ( map < string , int > & d ) { int sum = 0 ; for ( auto i : d ) sum += ( i . second * ( i . second - 1 ) ) / 2 ; return sum ; } int difference ( vector < string > & array , int m ) { map < string , int > changed , same ; for ( auto s : array ) { same [ s ] ++ ; for ( int i = 0 ; i < m ; i ++ ) { changed [ t ] ++ ; } } return pairCount ( changed ) - pairCount ( same ) * m ; } int main ( ) { int n = 3 , m = 3 ; vector < string > array = { " abc " , " abd " , " bbd " } ; cout << difference ( array , m ) << endl ; return 0 ; }
Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | C ++ implementation of the approach ; Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateWays ( string s , int n , int l , int r ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < l i > r ) freq [ s [ i ] - ' a ' ] ++ ; } int ways = 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( freq [ s [ i ] - ' a ' ] ) { ways = ways * freq [ s [ i ] - ' a ' ] ; freq [ s [ i ] - ' a ' ] -- ; } else { ways = 0 ; break ; } } return ways ; } int main ( ) { string s = " cabcaab " ; int n = s . length ( ) ; int l = 1 , r = 3 ; cout << calculateWays ( s , n , l , r ) ; return 0 ; }
Program to find the kth character after decrypting a string | C ++ implementation of the approach ; Function to print kth character of String s after decrypting it ; Get the length of string ; Initialise pointer to character of input string to zero ; Total length of resultant string ; Traverse the string from starting and check if each character is alphabet then increment total_len ; If total_leg equal to k then return string else increment i ; Parse the number ; Update next_total_len ; Get the position of kth character ; Position not found then update position with total_len ; Recursively find the kth position ; Else update total_len by next_total_len ; Return - 1 if character not found ; Driver code
#include <cstdlib> NEW_LINE #include <iostream> NEW_LINE using namespace std ; char findKthChar ( string s , int k ) { int len = s . length ( ) ; int i = 0 ; int total_len = 0 ; while ( i < len ) { if ( isalpha ( s [ i ] ) ) { total_len ++ ; if ( total_len == k ) return s [ i ] ; i ++ ; } else { int n = 0 ; while ( i < len && ! isalpha ( s [ i ] ) ) { n = n * 10 + ( s [ i ] - '0' ) ; i ++ ; } int next_total_len = total_len * n ; if ( k <= next_total_len ) { int pos = k % total_len ; if ( ! pos ) { pos = total_len ; } return findKthChar ( s , pos ) ; } else { total_len = next_total_len ; } } } return -1 ; } int main ( ) { string s = " ab2c3" ; int k = 5 ; cout << findKthChar ( s , k ) ; return 0 ; }
Check whether the Average Character of the String is present or not | CPP program to check if the average character is present in the string or not ; Checks if the character is present ; Get the length of string ; Iterate from i = 0 to the length of the string to check if the character is present in the string ; Finds the average character of the string ; Calculate the sum of ASCII values of each character ; Calculate average of ascii values ; Convert the ASCII value to character and return it ; Driver code ; Get the average character ; Check if the average character is present in string or not
#include <bits/stdc++.h> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool check_char ( char * st , char ch ) { int l = strlen ( st ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( st [ i ] == ch ) return true ; } return false ; } char find_avg ( char * st ) { int i , sm = 0 ; int l = strlen ( st ) ; char ch ; for ( i = 0 ; i < l ; i ++ ) { ch = st [ i ] ; sm = sm + ( int ) ( ch ) ; } int avg = ( int ) ( floor ( sm / l ) ) ; return ( ( char ) ( avg ) ) ; } int main ( ) { char st [ ] = " ag23sdfa " ; char ch = find_avg ( st ) ; cout << ch << endl ; if ( check_char ( st , ch ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Convert the ASCII value sentence to its equivalent string | C ++ implementation of the approach ; Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void asciiToSentence ( string str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str [ i ] - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; cout << ch ; num = 0 ; } } } int main ( ) { string str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; return 0 ; }
Distinct state codes that appear in a string as contiguous sub | C ++ implementation of the approach ; Function to return the count of distinct state codes ; Insert every sub - string of length 2 in the set ; Return the size of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistinctCode ( string str ) { set < string > codes ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) codes . insert ( str . substr ( i , 2 ) ) ; return codes . size ( ) ; } int main ( ) { string str = " UPUP " ; cout << countDistinctCode ( str ) ; return 0 ; }
Count of buttons pressed in a keypad mobile | C ++ implementation of the approach ; Array to store how many times a button has to be pressed for typing a particular character ; Function to return the count of buttons pressed to type the given string ; Count the key presses ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 } ; int countKeyPressed ( string str , int len ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) count = count + arr [ str [ i ] - ' a ' ] ; return count ; } int main ( ) { string str = " abcdef " ; int len = str . length ( ) ; cout << countKeyPressed ( str , len ) ; return 0 ; }
First string from the given array whose reverse is also present in the same array | CPP implementation of the approach ; Function that returns true if s1 is equal to reverse of s2 ; If both the strings differ in length ; In case of any character mismatch ; Function to return the first word whose reverse is also present in the array ; Check every string ; Pair with every other string appearing after the current string ; If first string is equal to the reverse of the second string ; No such string exists ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isReverseEqual ( string s1 , string s2 ) { if ( s1 . length ( ) != s2 . length ( ) ) return false ; int len = s1 . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( s1 [ i ] != s2 [ len - i - 1 ] ) return false ; return true ; } string getWord ( string str [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isReverseEqual ( str [ i ] , str [ j ] ) ) return str [ i ] ; return " - 1" ; } int main ( ) { string str [ ] = { " geeks " , " for " , " skeeg " } ; cout << ( getWord ( str , 3 ) ) ; }
Check if the given string is K | CPP implementation of the approach ; Function that return true if sub - string of length k starting at index i is also a prefix of the string ; k length sub - string cannot start at index i ; Character mismatch between the prefix and the sub - string starting at index i ; Function that returns true if str is K - periodic ; Check whether all the sub - strings str [ 0 , k - 1 ] , str [ k , 2 k - 1 ] ... are equal to the k length prefix of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrefix ( string str , int len , int i , int k ) { if ( i + k > len ) return false ; for ( int j = 0 ; j < k ; j ++ ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; } return true ; } bool isKPeriodic ( string str , int len , int k ) { for ( int i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } int main ( ) { string str = " geeksgeeks " ; int len = str . length ( ) ; int k = 5 ; if ( isKPeriodic ( str , len , k ) ) cout << ( " Yes " ) ; else cout << ( " No " ) ; }
Minimum number of letters needed to make a total of n | C ++ implementation of the approach ; Function to return the minimum letters required to make a total of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLettersNeeded ( int n ) { if ( n % 26 == 0 ) return ( n / 26 ) ; else return ( ( n / 26 ) + 1 ) ; } int main ( ) { int n = 52 ; cout << minLettersNeeded ( n ) ; return 0 ; }
Find the first maximum length even word from a string | C ++ program to find maximum length even word ; Function to find maximum length even word ; To store length of current word . ; To store length of maximum length word . ; To store starting index of maximum length word . ; If current character is space then word has ended . Check if it is even length word or not . If yes then compare length with maximum length found so far . ; Set currlen to zero for next word . ; Update length of current word . ; Check length of last word . ; If no even length word is present then return - 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findMaxLenEven ( string str ) { int n = str . length ( ) ; int i = 0 ; int currlen = 0 ; int maxlen = 0 ; int st = -1 ; while ( i < n ) { if ( str [ i ] == ' ▁ ' ) { if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } currlen = 0 ; } else { currlen ++ ; } i ++ ; } if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } if ( st == -1 ) return " - 1" ; return str . substr ( st , maxlen ) ; } int main ( ) { string str = " this ▁ is ▁ a ▁ test ▁ string " ; cout << findMaxLenEven ( str ) ; return 0 ; }
Minimum length substring with exactly K distinct characters | C ++ program to find minimum length substring having exactly k distinct character . ; Function to find minimum length substring having exactly k distinct character . ; Starting index of sliding window . ; Ending index of sliding window . ; To store count of character . ; To store count of distinct character in current sliding window . ; To store length of current sliding window . ; To store minimum length . ; To store starting index of minimum length substring . ; Increment count of current character If this count is one then a new distinct character is found in sliding window . ; If number of distinct characters is is greater than k , then move starting point of sliding window forward , until count is k . ; Remove characters from the beginning of sliding window having count more than 1 to minimize length . ; Compare length with minimum length and update if required . ; Return minimum length substring . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findMinLenStr ( string str , int k ) { int n = str . length ( ) ; int st = 0 ; int end = 0 ; int cnt [ 26 ] ; memset ( cnt , 0 , sizeof ( cnt ) ) ; int distEle = 0 ; int currlen ; int minlen = n ; int startInd = -1 ; while ( end < n ) { cnt [ str [ end ] - ' a ' ] ++ ; if ( cnt [ str [ end ] - ' a ' ] == 1 ) distEle ++ ; if ( distEle > k ) { while ( st < end && distEle > k ) { if ( cnt [ str [ st ] - ' a ' ] == 1 ) distEle -- ; cnt [ str [ st ] - ' a ' ] -- ; st ++ ; } } if ( distEle == k ) { while ( st < end && cnt [ str [ st ] - ' a ' ] > 1 ) { cnt [ str [ st ] - ' a ' ] -- ; st ++ ; } currlen = end - st + 1 ; if ( currlen < minlen ) { minlen = currlen ; startInd = st ; } } end ++ ; } return str . substr ( startInd , minlen ) ; } int main ( ) { string str = " efecfefd " ; int k = 4 ; cout << findMinLenStr ( str , k ) ; return 0 ; }
Minimum number of replacements to make the binary string alternating | Set 2 | C ++ implementation of the approach ; Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minReplacement ( string s , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s [ i ] == '1' ) ans ++ ; if ( i % 2 == 1 && s [ i ] == '0' ) ans ++ ; } return min ( ans , len - ans ) ; } int main ( ) { string s = "1100" ; int len = s . size ( ) ; cout << minReplacement ( s , len ) ; return 0 ; }
Deletions of "01" or "10" in binary string to make it free from "01" or "10" | C ++ implementation of the approach ; Function to return the count of deletions of sub - strings "01" or "10" ; To store the count of 0 s and 1 s ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int substrDeletion ( string str , int len ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) count0 ++ ; else count1 ++ ; } return min ( count0 , count1 ) ; } int main ( ) { string str = "010" ; int len = str . length ( ) ; cout << substrDeletion ( str , len ) ; return 0 ; }
Group consecutive characters of same type in a string | C ++ implementation of the approach ; Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string groupCharacters ( string s , int len ) { string temp = " " ; for ( int i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ▁ ' ) temp = temp + s [ i ] ; len = temp . length ( ) ; string ans = " " ; int i = 0 ; while ( i < len ) { if ( int ( temp [ i ] ) >= int ( ' A ' ) && int ( temp [ i ] ) <= int ( ' Z ' ) ) { while ( i < len && int ( temp [ i ] ) >= int ( ' A ' ) && int ( temp [ i ] ) <= int ( ' Z ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else if ( int ( temp [ i ] ) >= int ( '0' ) && int ( temp [ i ] ) <= int ( '9' ) ) { while ( i < len && int ( temp [ i ] ) >= int ( '0' ) && int ( temp [ i ] ) <= int ( '9' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else { while ( i < len && int ( temp [ i ] ) >= int ( ' * ' ) && int ( temp [ i ] ) <= int ( ' / ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } } return ans ; } int main ( ) { string s = "34FTG234 + ▁ + - ▁ * " ; int len = s . length ( ) ; cout << groupCharacters ( s , len ) ; return 0 ; }
Find the minimum number of preprocess moves required to make two strings equal | C ++ implementation of the approach ; Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; Run a loop upto n / 2 ; To store frequency of 4 characters ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Preprocess ( string A , string B ) { int n = A . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { map < char , int > mp ; mp [ A [ i ] ] ++ ; mp [ A [ n - i - 1 ] ] ++ ; mp [ B [ i ] ] ++ ; mp [ B [ n - i - 1 ] ] ++ ; int sz = mp . size ( ) ; if ( sz == 4 ) ans += 2 ; else if ( sz == 3 ) ans += 1 + ( A [ i ] == A [ n - i - 1 ] ) ; else if ( sz == 2 ) ans += mp [ A [ i ] ] != 2 ; } if ( n % 2 == 1 && A [ n / 2 ] != B [ n / 2 ] ) ans ++ ; return ans ; } int main ( ) { string A = " abacaba " , B = " bacabaa " ; cout << Preprocess ( A , B ) ; return 0 ; }
Check whether two strings are equivalent or not according to given condition | CPP Program to find whether two strings are equivalent or not according to given condition ; This function returns the least lexicogr aphical string obtained from its two halves ; Base Case - If string size is 1 ; Divide the string into its two halves ; Form least lexicographical string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string leastLexiString ( string s ) { if ( s . size ( ) & 1 ) return s ; string x = leastLexiString ( s . substr ( 0 , s . size ( ) / 2 ) ) ; string y = leastLexiString ( s . substr ( s . size ( ) / 2 ) ) ; return min ( x + y , y + x ) ; } bool areEquivalent ( string a , string b ) { return ( leastLexiString ( a ) == leastLexiString ( b ) ) ; } int main ( ) { string a = " aaba " ; string b = " abaa " ; if ( areEquivalent ( a , b ) ) cout << " YES " << endl ; else cout << " NO " << endl ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Check if a string can be converted to another string by replacing vowels and consonants | C ++ program to check if a string can be converted to other string by replacing vowels and consonants ; Function to check if the character is vowel or not ; Function that checks if a string can be converted to another string ; Find length of string ; If length is not same ; Iterate for every character ; If both vowel ; Both are consonants ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } bool checkPossibility ( string s1 , string s2 ) { int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( l1 != l2 ) return false ; for ( int i = 0 ; i < l1 ; i ++ ) { if ( isVowel ( s1 [ i ] ) && isVowel ( s2 [ i ] ) ) continue ; else if ( ! ( isVowel ( s1 [ i ] ) ) && ! ( isVowel ( s2 [ i ] ) ) ) continue ; else return false ; } return true ; } int main ( ) { string S1 = " abcgle " , S2 = " ezggli " ; if ( checkPossibility ( S1 , S2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | C ++ implementation of the approach ; Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateString ( int A , int B ) { string rt ; while ( 0 < A 0 < B ) { if ( A < B ) { if ( 0 < B -- ) rt . push_back ( ' b ' ) ; if ( 0 < B -- ) rt . push_back ( ' b ' ) ; if ( 0 < A -- ) rt . push_back ( ' a ' ) ; } else if ( B < A ) { if ( 0 < A -- ) rt . push_back ( ' a ' ) ; if ( 0 < A -- ) rt . push_back ( ' a ' ) ; if ( 0 < B -- ) rt . push_back ( ' b ' ) ; } else { if ( 0 < A -- ) rt . push_back ( ' a ' ) ; if ( 0 < B -- ) rt . push_back ( ' b ' ) ; } } cout << rt ; } int main ( ) { int A = 2 , B = 6 ; generateString ( A , B ) ; return 0 ; }
Number of strings that satisfy the given condition | C ++ implementation of the approach ; Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStrings ( int n , int m , string s [ ] ) { unordered_set < int > ind ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = max ( mx , ( int ) s [ i ] [ j ] - '0' ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] [ j ] - '0' == mx ) ind . insert ( i ) ; } return ind . size ( ) ; } int main ( ) { string s [ ] = { "223" , "232" , "112" } ; int m = s [ 0 ] . length ( ) ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; cout << countStrings ( n , m , s ) ; }
Lexicographically largest sub | C ++ implementation of the approach ; Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getSubSeq ( string s , int n ) { string res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s [ cr ] ; for ( int i = cr + 1 ; i < n ; i ++ ) mx = max ( mx , s [ i ] ) ; int lst = cr ; for ( int i = cr ; i < n ; i ++ ) if ( s [ i ] == mx ) { res += s [ i ] ; lst = i ; } cr = lst + 1 ; } return res ; } int main ( ) { string s = " geeksforgeeks " ; int n = s . length ( ) ; cout << getSubSeq ( s , n ) ; }
Validation of Equation Given as String | C ++ implementation of the approach ; Function that returns true if the equation is valid ; If it is an integer then add it to another string array ; Evaluation of 1 st operator ; Evaluation of 2 nd operator ; Evaluation of 3 rd operator ; If the LHS result is equal to the RHS ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( string str ) { int k = 0 ; string operands [ 5 ] = " " ; char operators [ 4 ] ; long ans = 0 , ans1 = 0 , ans2 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] != ' + ' && str [ i ] != ' = ' && str [ i ] != ' - ' ) operands [ k ] += str [ i ] ; else { operators [ k ] = str [ i ] ; if ( k == 1 ) { if ( operators [ k - 1 ] == ' + ' ) ans += stol ( operands [ k - 1 ] ) + stol ( operands [ k ] ) ; if ( operators [ k - 1 ] == ' - ' ) ans += stol ( operands [ k - 1 ] ) - stol ( operands [ k ] ) ; } if ( k == 2 ) { if ( operators [ k - 1 ] == ' + ' ) ans1 += ans + stol ( operands [ k ] ) ; if ( operators [ k - 1 ] == ' - ' ) ans1 -= ans - stol ( operands [ k ] ) ; } if ( k == 3 ) { if ( operators [ k - 1 ] == ' + ' ) ans2 += ans1 + stol ( operands [ k ] ) ; if ( operators [ k - 1 ] == ' - ' ) ans2 -= ans1 - stol ( operands [ k ] ) ; } k ++ ; } } if ( ans2 == stol ( operands [ 4 ] ) ) return true ; else return false ; } int main ( ) { string str = "2 + 5 + 3 + 1 = 11" ; if ( isValid ( str ) ) cout << " Valid " ; else cout << " Invalid " ; return 0 ; }