Created
June 7, 2025 00:58
-
-
Save romatou18/5f0beee70e62cf2b89901e29b86b3345 to your computer and use it in GitHub Desktop.
HackerRank Balanced String lower-than greaterthan '<>' and with replacements iterations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iterator> | |
#include <vector> | |
#include <string> | |
#include <sstream> | |
#include <iomanip> | |
#include <numeric> | |
#include <algorithm> | |
#include <set> | |
#include <stack> | |
using namespace std; | |
void debugInit(std::string expression, const vector<int>& replacementsCnt) | |
{ | |
cout<< expression << endl; | |
cout << " replacements : ["; | |
for(auto i : replacementsCnt) | |
{ | |
cout << i << ","; | |
} | |
cout << "]" << endl; | |
} | |
// replacement : replace '>' by '<>' once | |
// e.g. <<>><> ==> 1 replacement ==> <<>> | |
// <>><<> | |
/** | |
* A recent HackerRank challenge I had for a job interview with Mindrift AI: | |
* | |
* A string is balanced when each pair of < and > are opening and closing the current pair e.g.acoshl | |
* <> is balanced | |
* <><> is balanced | |
* <<><> is NOT balanced | |
* <><>> is NOT balanced however : if having replacement attempts, whenever finding an '>' which causes imbalance | |
* then you have repCnt attempts at inserting '<' to balance the string e.g. | |
* <><>> with 1 replacement bullets : becomes <><><> which is balanced | |
* e.g. <>>><> would require 2 replacement bullets to become <><><><> | |
* write the following function to check if exp is balanced, if nt perfom from 0, to repCnt attempts to see if the string becomes balanced | |
* | |
*/ | |
bool checkStringWithReplacementValue(int& repCnt, std::string& exp) | |
{ | |
std::stack<char> st; | |
for(size_t i=0; i<exp.size(); i++) | |
{ | |
switch (exp[i]) | |
{ | |
case '<': | |
st.push(exp[i]); | |
break; | |
default: // > | |
if (!st.empty() && st.top() == '<') { // <> | |
st.pop(); | |
break; | |
} | |
else if((st.empty() || st.top() == '>') && repCnt == 0) // starting with > instead of < or else 2nd > in a row. | |
{ | |
return false; | |
} | |
else if( repCnt > 0) // >> or >< but we have a replacement so try again... | |
{ | |
size_t p = i; | |
std::cout << exp << endl; | |
exp.insert(p, string{"<"}); | |
// std::cout << endl << " pos" << p << ", " << exp; | |
repCnt--; | |
st.pop(); | |
return checkStringWithReplacementValue(repCnt, exp); | |
} | |
} | |
} | |
return st.empty(); | |
} | |
// returns the number of replacement used to balance the string. | |
int checkStringIsBalancedWithReplacements(const std::string& expression, const vector<int>& replacementsCnt) | |
{ | |
if(expression.empty()) | |
{ | |
cerr << "string is empty !" << endl; | |
return 0; | |
} | |
debugInit(expression, replacementsCnt); | |
std::string expCopy = expression; | |
int repIndex = 0; | |
for( auto& repCnt : replacementsCnt) | |
{ | |
repIndex = repCnt; | |
bool isBalanced = checkStringWithReplacementValue(repIndex, expCopy); | |
if(isBalanced) | |
{ //debug | |
cout << endl << " original before replacement:" << endl << expression; | |
cout << endl << expCopy << " is BALANCED! with #" << repCnt << " replacement bullets" << endl; | |
break; | |
} | |
else | |
{ | |
if(repIndex == 0) | |
{ | |
cout << "string cannot be balanced !" << endl; | |
} | |
} | |
} | |
return repIndex+1; // 0 based index needs bumped up into count. | |
} | |
void testAttempts(int cnt, int expected) | |
{ | |
if(cnt != expected) | |
{ | |
cerr << " Expected " << expected << "replacement attempts, got " << cnt << "instead..."; | |
} | |
} | |
int main() | |
{ | |
std::cout << "hello" << std::endl; | |
int attempts = checkStringIsBalancedWithReplacements("<>><>", vector<int>{1,2,3}); // should be balanced after 1 | |
testAttempts(attempts, 1); | |
attempts = checkStringIsBalancedWithReplacements("<>>><>", vector<int>{1,2,3}); // should be balanced after 2 attempts. | |
testAttempts(attempts, 2); | |
attempts = checkStringIsBalancedWithReplacements("<>>><>>>", vector<int>{1,2,4,5}); // should be balanced after 4 attempts | |
testAttempts(attempts, 4); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment