Java Stack - HackerRank Solution
Hello Friends, How are you? Today I will solve the HackerRank Java Stack - Problem with a straightforward explanation. In this article, you will get more than one approach to solve this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Java Stack - Problem Statement
In computer science, a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added. (Wikipedia)
A string containing only parentheses is balanced if the following is true: 1. if it is an empty string 2. if A and B are correct, AB is correct, 3. if A is correct, (A) and {A} and [A] are also correct.
Examples of some correctly balanced strings are: "{}()", "[{()}]", "({()})"
Examples of some unbalanced strings are: "{}(", "({)}", "[[", "}{" etc.
Given a string, determine if it is balanced or not.
Input Format
There will be multiple lines in the input file, each having a single non-empty string. You should read the input till end-of-file.
The part of the code that handles input operation is already provided in the editor.
Output Format
For each case, print 'true' if the string is balanced, 'false' otherwise.
Sample Input
{}() ({()}) {}( [] {codeBox}
Sample Output
true true false true {codeBox}
Java Stack HackerRank Solution
Java Stack - Hacker Rank Solution
Approach I: Java Stack - Solution HackerRank
// ========================
// Information
// ========================
// Name: Java Stack HackerRank Problem
// Direct Link: https://www.hackerrank.com/challenges/java-stack/problem
// Difficulty: Medium
// Max Score: 20
// Language: Java 8
// ========================
// Solution Start
// ========================
// Java Stack - Hacker Rank Solution Start
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
System.out.println(isBalanced(in.next()));
}
in.close();
}
static boolean isBalanced(String parentheses) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < parentheses.length(); i++) {
char ch = parentheses.charAt(i);
if (ch == '(' || ch == '[' || ch == '{') {
stack.push(ch);
} else if (stack.empty()) {
return false;
} else {
char top = stack.pop();
if ((top == '(' && ch != ')') || (top == '[' && ch != ']')
|| (top == '{' && ch != '}')) {
return false;
}
}
}
return stack.empty();
}
}
// Java Stack Hacker Rank Solution END
// MyEduWaves
Approach II: Java Stack - Solution HackerRank
// ========================
// Information
// ========================
// Name: Java Stack HackerRank Problem
// Direct Link: https://www.hackerrank.com/challenges/java-stack/problem
// Difficulty: Medium
// Max Score: 20
// Language: Java 8
// ========================
// Solution Start
// ========================
// Java Stack - Hacker Rank Solution Start
import java.util.*;
class Solution{
private static boolean isBalanced(String query) {
Stack<Character> s = new Stack<Character>();
for (int i = 0; i < query.length(); i++) {
if (query.charAt(i) == '(' || query.charAt(i) == '[' || query.charAt(i) == '{') {
s.push(query.charAt(i));
}
if (query.charAt(i) == ')' || query.charAt(i) == ']' || query.charAt(i) == '}') {
if (s.empty()) {
return false;
}
if (query.charAt(i) == ')') {
if (s.peek() == '(') {
s.pop();
} else {
return false;
}
}
if (query.charAt(i) == ']') {
if (s.peek() == '[') {
s.pop();
} else {
return false;
}
}
if (query.charAt(i) == '}') {
if (s.peek() == '{') {
s.pop();
} else {
return false;
}
}
}
}
return s.empty();
}
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input=sc.next();
System.out.println(Solution.isBalanced(input));
}
}
}
// Java Stack Hacker Rank Solution END
// MyEduWaves
Also Check:
Disclaimer: The above Problem ( Java Stack ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java Stack - Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Java