# Facebook Hacker Cup 2013: Solution to Balanced Smileys

Below the second problem of the qualification round of the Facebook Hacker Cup 2013:

——–
Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go 🙁

Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

A message has balanced parentheses if it consists of one of the following:

– An empty string “”
– One or more of the following characters: ‘a’ to ‘z’, ‘ ‘ (a space) or ‘:’ (a colon)
– An open parenthesis ‘(‘, followed by a message with balanced parentheses, followed by a close parenthesis ‘)’.
– A message with balanced parentheses followed by another message with balanced parentheses.
– A smiley face “:)” or a frowny face “:(”
Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

Input
The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases.
The following T lines each contain a message of length s that you got from John.

Output
For each of the test cases numbered in order from 1 to T, output “Case #i: ” followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be “YES”, else it should be “NO” (all quotes for clarity only)

Constraints
1 ≤ length of s ≤ 100

Sample Input
5
:((
i am sick today (:()
(:)
hacker cup: started :):)
)(

Sample Output
Case #1: NO
Case #2: YES
Case #3: YES
Case #4: YES
Case #5: NO
——–

## My Solution

I created an algorithm using a stack, and I would push and pop parentheses on the stack as they would appear on the string. I also made the algorithm consider that a given parenthesis could be part of a smiley or not. It managed to solve the sample input, but for some reason it failed with the larger input file, so I didn’t get points for this one:

``````import java.io.BufferedReader;
import java.io.IOException;
import java.util.Stack;

public class Main{

public static void main(String[] args){
int i,j;
j = 0;
String x = "";

try{
}
catch(IOException e){
e.printStackTrace();
}
for(i=0;i<j;i++){
try{
}
catch(IOException e){
e.printStackTrace();
}
System.out.println("Case #"+(i+1)+": "+rpn(x));
}
}

public static String rpn(String x){
String result = "";
char c = 'a';
char c2 = 'a';
int i;
Stack<Character> s = new Stack<Character>();
boolean colon = false;
boolean bonus = false;
boolean previous = false;
int bonus2 = 0;

for(i=0;i<x.length();i++){
c = x.charAt(i);
if(colon)
previous = true;
if(c==':')
colon=true;
else if(c=='('&&colon==false){
s.push(c);
}
else if(c==')'&&colon==false){
if(s.empty()){
if(bonus)
bonus = false;
else
return "NO";
}
else{
c = s.peek();
if(c=='(')
c = s.pop();
else if(bonus)
bonus = false;
else
return "NO";
}
}
else if(c==')'){
colon=false;
if(!s.empty()){
c2 = s.peek();
if(c2=='('){
c = s.pop();
bonus = true;
}
}
}
else if(c=='('){
s.push(c);
colon=false;
bonus2++;
}
if(previous){
colon = false;
previous = false;
}
}

if(s.empty())
return "YES";
else{
if(s.size()==bonus2)
return "YES";
else
return "NO";
}
}

}``````