Last active
July 4, 2019 02:36
-
-
Save vikashvverma/4650142 to your computer and use it in GitHub Desktop.
This is my solution for Facebook Hacker Cup 2013 in java :Balanced Smileys
This file contains 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
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 |
This file contains 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
//Please visit: http://vikash-thiswillgoaway.blogspot.in/2013/01/facebook-hacker-cup-2013-balanced.html for the solutions | |
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"; | |
} | |
} |
On a general note you can try using java.util.Scanner to read files.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why would you post this when the competition is still not finished?