Question

Java 8

Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example, [0l is a valid groupingofbraces but [)IC is not. Function Description Complete the function braces in the editor below. The function must return an array of strings where the string at each index i denotes whether or not the braces were balanced in a values The array should consist of strings YES or NO aligned with their indexes in values. braces has the following parameter(s) valuesfvalues.. valueso-1): an array of strings to analyze Constraints 1sns 15 1 s length of values,s 100 It is guaranteed that each values, consists of (), L. I. L, and ]only

Input Format For Custom Testing Input from stdin will be processed as follows and passed to the function: The first line contains an integer n, the number of elements in values. Each of the next n lines contains a string describing values,where 0 si<n ▼ Sample Case。 Sample Input For Custom Testing IN Sample Output YES NO Explanation valueso: IHK) meets the criteria for a balanced string, so index 0 in our return array should contain the string YES values: contains unmatched braces between a matched pair in the substrings [.I), and [)l. so index 1 in our return array should contain the string NO

YOUR ANSWER Draft saved 02:10 pm Original Code Java 8 1 import java.io.; 12 13 /I Complete the braces function below 14 static String[1 braces(String1 values) 15 16 17 18 19private static final Scanner scanner new Scanner (System.in);

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
import java.util.Stack;

public class Braces {

    public static String[] braces(String[] values) {
        String[] result = new String[values.length];
        String s;
        for(int i = 0; i < values.length; i++) {
            s = values[i];
            Stack<Character> stack = new Stack<>();
            boolean status = true;
            for(int j = 0; j < s.length(); j++) {
                if(s.charAt(j) == '[' || s.charAt(j) == '{' || s.charAt(j) == '(') {
                    stack.push(s.charAt(j));
                } else if(s.charAt(j) == ']') {
                    if(stack.isEmpty() || stack.pop() != '[') {
                        status = false;
                    }
                } else if(s.charAt(j) == '}') {
                    if(stack.isEmpty() || stack.pop() != '{') {
                        status = false;
                    }
                } else if(s.charAt(j) == ')') {
                    if(stack.isEmpty() || stack.pop() != '(') {
                        status = false;
                    }
                }
            }
            status = status & stack.isEmpty();
            if(status) {
                result[i] = "YES";
            } else {
                result[i] = "NO";
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] data = new String[n];
        for(int i = 0; i < data.length; i++) {
            data[i] = in.next();
        }
        String[] result = braces(data);
        for(int i = 0; i < data.length; i++) {
            System.out.println(result[i]);
        }
        in.close();
    }

}
Add a comment
Know the answer?
Add Answer to:
Java 8 Braces You are designing a compiler for a C++ program and need to check...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Input Format For Custom Testing The first line contains an integer, n, that denotes the...

    Java Input Format For Custom Testing The first line contains an integer, n, that denotes the number of elements in s. Each line i of the n subsequent lines (where 0 si<n) contains a string that describes s[i]. Sample Case 0 Sample Input For Custom Testing 4 code aaagmnrs anagrams doce Sample Output aaagmnrs code Explanation aaagmnrs and anagrams are anagrams, code and doce are anagrams. After sorting aaagmnrs comes first. Sample Case 1 Sample Input For Custom Testing 4...

  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • in C++ and comments please for better understanding 3. How Will You Compare? Write a Comparator...

    in C++ and comments please for better understanding 3. How Will You Compare? Write a Comparator class with the following 3 overloaded compare methods: 1. boolean compare(int a, int b): Return true if int a = int b, otherwise return false. 2. boolean compare(string a, string b): Return true if string a = string b, otherwise return false. 3. boolean compare(int[] a, int[] b): Return true if both of the following conditions hold true: O Arrays a and b are...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the...

    Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the values of the array into two subsets, A and B, whose intersection is null and where the addition of the two subsets equals the entire array. The sum of values in set A must be strictly greater than the sum of values in set B, and the number of elements in set A must be minimal. Return the values in set A in increasing...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT