Programming Geek
Rated 4.7/5 based on 1446 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.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.util.ArrayList;

/**
 *
 * @author VIK http://www.facebook.com/vikashvverma
 * 
 */
public class Problem2 {

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

    public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("balanced_smileystxt.txt");
        LineNumberReader lnr = new LineNumberReader(fr);
        int t = Integer.parseInt(lnr.readLine().trim());
        ArrayList < String > list = new ArrayList < String >();
        FileWriter fw = new FileWriter(f);
        for (int i = 0; i < t; i++) {
            list.add(lnr.readLine().trim());
            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 lead = "";
            String trail = "";
            if (s.charAt(i) == '(' && s.charAt(s.length() - i - 1) == ')') {
                if (i > 0) {
                    lead = s.substring(0, i);
                    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.

7 comments :

  1. is this message '(first) (second)' had balanced parentheses?

    ReplyDelete
  2. yeah ofcourse, A message with balanced parentheses followed by another message with balanced parentheses is balanced... :D

    ReplyDelete
  3. coool! running your code give as result: "Case #1: NO"

    ReplyDelete
  4. Sorry for the bad news, but this case breaks your program: () :( ()

    If we consider the middle smiley face as a "real" smiley, then it would be balanced. Sorry again, better luck next time.

    ReplyDelete
  5. @Tonynater, in this case you can not take middle :( as smiley, you have to first check for the balanced parenthesis and then for smileys. If you have a string consisting of colon and parenthesis then you have to first check out for parenthesis and not smileys as you can see the last condition for balanced smileys.

    ReplyDelete
  6. But your program prints "NO" when it should be "YES". Am I correct?

    ReplyDelete
  7. The above program doesnt report the correct output for "(()):):)"

    ReplyDelete