Skip to content

Instantly share code, notes, and snippets.

@masious
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masious/33d6dc3a35b8521cd76c to your computer and use it in GitHub Desktop.
Save masious/33d6dc3a35b8521cd76c to your computer and use it in GitHub Desktop.
DS Questions - Question 1
package q1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Stack;
public class Main {
private static HashMap set;
public static void main(String[] args) {
set = new HashMap();
set.put(new Character('}'), new Character('{'));
set.put(new Character(')'), new Character('('));
set.put(new Character(']'), new Character('['));
String a = null;
Stack myStack = new Stack();
try {
a = new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isWrong = false;
for(int i=0;i<a.length();i++){
Character c = new Character(a.charAt(i));
if (set.containsValue(c)){
myStack.push(c);
} else if (set.containsKey(c)){
Character c2 = null;
try{
c2 = (Character) myStack.pop();
} catch(EmptyStackException e){
isWrong = true;
break;
}
if(!c2.equals(set.get(c))){
isWrong = true;
break;
}
}
}
if(!myStack.isEmpty())
isWrong = true;
if(isWrong)
System.out.println("Wrong!");
else
System.out.println("Right!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment