Skip to content

Instantly share code, notes, and snippets.

@dazza
Created July 10, 2009 08:51
Show Gist options
  • Save dazza/144353 to your computer and use it in GitHub Desktop.
Save dazza/144353 to your computer and use it in GitHub Desktop.
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
class CompletingBrackets {
public:
string complete(string text)
{
int nLeft = 0;//']'
int nRight = 0;
for(int i = 0 ; i < text.size(); i++)
{
if(text[i] == '[')
{
nRight++;
}
else
{
if (nRight==0)
nLeft++;
else
nRight--;
}
}
return string(nLeft,'[')+text+string(nRight,']');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment