fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. /*
  11.  * Complete the function below.
  12.  */
  13.  
  14. static int[] balancedOrNot(String[] expressions, int[] maxReplacements) {
  15. int[] ans = new int[maxReplacements.length];
  16. for(int i=0; i<expressions.length;i++){
  17. ans[i] = Checker(expressions[i], maxReplacements[i]);
  18. }
  19. return ans;
  20. }
  21.  
  22. static int Checker(String expression, int maxReplacement){
  23. //<<
  24. Stack<Character> stack = new Stack<Character>();
  25. int count = 0;
  26. for(char ch : expression.toCharArray()){
  27. if(ch == '<'){
  28. stack.push('<');
  29. }
  30. else if(stack.empty()){
  31. count++;
  32. }
  33. else{
  34. stack.pop();
  35. }
  36. }
  37.  
  38.  
  39. if(!stack.empty()){
  40. count+= stack.size();
  41. }else{
  42. return 0;
  43. }
  44.  
  45. return count <= maxReplacement ? 1 : 0;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. public static void main(String[] args) {
  52. String[] input = {"<<<><><>", "<><>>"}; //<> > <>
  53. int[] in = {2, 2};
  54. int[] res = balancedOrNot(input, in);
  55. for(int i : res){
  56. System.out.println(i);
  57. }
  58. }
  59. }
Success #stdin #stdout 0.08s 27800KB
stdin
Standard input is empty
stdout
1
0