Programming Geek
Rated 4.1/5 based on 446 reviews

### Facebook Hacker Cup 2013 Qualification Round: Balanced Snileys

Facebook Hacker Cup 2013: “Balanced Smileys” Solution

The problem statement is given below:
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:
1.  An empty string ""
2. One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
3.  An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
4. A message with balanced parentheses followed by another message with balanced parentheses.
5. 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
` `
`Here goes my solution for the “Beautiful Strings” problem:`
```
```
//Problem2.java
```import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

/**
*
*
*/
public class Problem2 {

private static File f = new File("Solution2.txt");

public static void main(String[] args) throws Exception {
ArrayList < String > list = new ArrayList < String >();
FileWriter fw = new FileWriter(f);
for (int i = 0; i < t; i++) {
String res = printResult(list.get(i), i + 1);
if ( i < t - 1 ) {
fw.write("Case #" + (i + 1) + ": " + res + "\n");
} else {
fw.write("Case #" + (i + 1) + ": " + res);
}
fw.flush();
}

}
```
``` private static String printResult ( String string, int index) {
String s = string.replaceAll ( "[^:\\(\\)]", "" );

for (int i = 0; i  <  s.length() / 2; i++) {
String trail = "";
if (s.charAt(i) == '(' && s.charAt(s.length() - i - 1) == ')') {
if (i > 0) {
trail = s.substring (s.length() - i, s.length());
}
s = lead + s.substring ( i + 1, s.length() - i - 1 ) + trail;
--i;
}
}
s = s.replaceAll( ":\\(", "" );
s = s.replaceAll( ":\\)", "" );

for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
return "NO";
}
}

return "YES";
}
}```

`Download Source Code Here`

```The solution is quite efficient and generated output file for my input file provided by Facebook in less than one second.
Do leave comment regarding any issue or suggestion.
```