Bharat
Bharat

Reputation: 1082

Getting different output for same testcase in hackerrank

I'm trying balanced paranthesis problem in hackerrank.

I'm getting different output for same test case which is very weird !!

This is the sub test case :

()[{}()]([[][]()[[]]]{()})([]()){[]{}}{{}}{}(){([[{}([]{})]])}

If I run this alone I'm getting the correct answer(In my case Output is "YES")

When this test case is ran along with other test cases I'm getting "NO" as output.

This is the actual testcase that I purchased for 5 hackos :

21
()[{}()]([[][]()[[]]]{()})([]()){[]{}}{{}}{}(){([[{}([]{})]])}
{][({(}]][[[{}]][[[())}[)(]([[[)][[))[}[]][()}))](]){}}})}[{]{}{((}]}{{)[{[){{)[]]}))]()]})))[
[)](][[([]))[)
]}]){[{{){
{[(}{)]]){(}}(][{{)]{[(((}{}{)}[({[}[}((}{()}[]})]}]]))((]][[{{}[(}})[){()}}{(}{{({{}[[]})]{((]{[){[
()}}[(}])][{]{()([}[}{}[{[]{]](]][[))(()[}(}{[{}[[]([{](]{}{[){()[{[{}}{[{()(()({}([[}[}[{(]})
){[])[](){[)}[)]}]]){](]()]({{)(]])(]{(}(}{)}])){[{}((){[({(()[[}](]})]}({)}{)]{{{
[(})])}{}}]{({[]]]))]})]
[{
{}([{()[]{{}}}])({})
{({}{[({({})([[]])}({}))({})]})}
()[]
{)[])}]){){]}[(({[)[{{[((]{()[]}][([(]}{](])()(}{(]}{})[)))[](){({)][}()((
[][(([{}])){}]{}[()]{([[{[()]({}[])()()}[{}][]]])}
(}]}
(([{()}]))[({[{}{}[]]{}})]{((){}{()}){{}}}{}{{[{[][]([])}[()({}())()({[]}{{[[]]([])}})()]]}}
[(([){[](}){){]]}{}([](([[)}[)})[(()[]){})}}]][({[}])}{(({}}{{{{])({]]}[[{{(}}][{)([)]}}
()()[()([{[()][]{}(){()({[]}[(((){(())}))]()){}}}])]
({)}]}[}]{({))}{)]()(](])})][(]{}{({{}[]{][)){}{}))]()}((][{]{]{][{}[)}}{)()][{[{{[[
)}(()[])(}]{{{}[)([})]()}()]}(][}{){}}})}({](){([()({{(){{
}([]]][[){}}[[)}[(}(}]{(}[{}][{}](}]}))]{][[}(({(]}[]{[{){{(}}[){[][{[]{[}}[)]}}]{}}(}

The sub testcase that I've shown is first one in this big one...

This is my java code :

package Java.Stacks.Hackerrank_Problems;

import java.util.Scanner;
import java.util.Stack;

/**
 * Created by BK on 06-08-2017.
 */

public class BalancedParanthesis {
    public static void main(String... strings) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        for (int i = 0; i < tc; i++) {
            printAnswer(sc.next());
        }
    }

    private static void printAnswer(String input) {
        Stack<Character> stack = new Stack<>();
        boolean isValid=true;
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (currentChar == '{' || currentChar == '(' || currentChar == '[') stack.push(currentChar);
            else if (currentChar == '}') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '{') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ')') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '(') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ']') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '[') {
                        isValid=false;
                    }
                }
            }
        }
        System.out.println(isValid?"YES":"NO");
    }
}

Please help me to get rid of this... Thankyou :)

This says it's not working...

Error Image

Upvotes: 1

Views: 273

Answers (2)

marvel308
marvel308

Reputation: 10458

You forgot to check if stack is empty at the end, this is the code which was accepted at hackerrank

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String isBalanced(String s) {
        Stack<Character> stack = new Stack<>();
        boolean isValid=true;
        for (int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (currentChar == '{' || currentChar == '(' || currentChar == '[') stack.push(currentChar);
            else if (currentChar == '}') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '{') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ')') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '(') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ']') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '[') {
                        isValid=false;
                    }
                }
            }
        }
        return (isValid && stack.empty()?"YES":"NO");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            String s = in.next();
            String result = isBalanced(s);
            System.out.println(result);
        }
        in.close();
    }
}

notice the small change

(isValid && stack.empty()?"YES":"NO");

Upvotes: 2

Udyan Sharma
Udyan Sharma

Reputation: 134

Suggested edit.Please try this.See if it works.

int tc = sc.nextInt();
String s=sc.nextLine();//Just for storing the escape character
    for (int i = 0; i < tc; i++) {
        printAnswer(sc.next());
    }

Upvotes: 1

Related Questions