Question

In: Computer Science

Suppose you are given a string containing only the characters ( and ). In this problem,...

Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero credit.

Balanced parentheses means that every open parenthesis has exactly one close parenthesis corresponding to it and vice versa, and that for each pair the opening parenthesis precedes the closed parenthesis. The following strings have balanced parentheses: () (()()) ((())())

The following strings do not have balanced parentheses: )( (() ())(()))())

We consider the empty string to have balanced parentheses, as there is no imbalance. Your program should accept as input a single string containing only the characters ) and (, and output a single line stating true or false .

The functionality for reading and printing answers is written in the class; your task is to complete the hasBalancedParentheses() method.

PLEASE START WITH THIS CODE

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// Read the input string
String input = sc.nextLine();
  
BalancedParentheses bp = new BalancedParentheses();
// Print whether the string has balanced parentheses
System.out.println(bp.hasBalancedParentheses(input));
}
}

class BalancedParentheses {
public boolean hasBalancedParentheses(String input) {
// Remove this and implement code
throw new UnsupportedOperationException();
}
}

Solutions

Expert Solution

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

// Read the input string
        String input = sc.nextLine();

        BalancedParentheses bp = new BalancedParentheses();
// Print whether the string has balanced parentheses
        System.out.println(bp.hasBalancedParentheses(input));
    }
}

class BalancedParentheses {
    public boolean hasBalancedParentheses(String input) {
        int count = 0;
        char ch;
        for (int i = 0; i < input.length(); i++) {
            ch = input.charAt(i);
            if (ch == '(') {
                count++;
            } else if (ch == ')') {
                if (count == 0) {
                    return false;
                }
                count--;
            }
        }
        return count == 0;
    }
}


Related Solutions

Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed). You...
You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed). You have to print all permutations of string with the added constraint that you can’t change the uppercase alphabets positions. Respond in Python please
C++: A string construction problem defined as follows: - You are given as input a target...
C++: A string construction problem defined as follows: - You are given as input a target string - Starting with an empty string, you add characters to it, until your new string is same as the target. You have two options to add characters to a string: - You can append an arbitrary character to your new string, with cost x - You can clone any substring of your new string so far, and append it to the end of...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
You are given a black box containing an RL series circuit. Using only an oscilloscope and...
You are given a black box containing an RL series circuit. Using only an oscilloscope and a function generator and an extra resistor, what equations would you use to find the following: Unknown inductor value: Unknown Inductor impedance: Unknown black box Resistor value: Once you have these, mathematically, find a power factor of at least .97 by adding a capacitor in parallel to the black box.
Python: If I am given a string consisting only of parentheses - (, ), {, },...
Python: If I am given a string consisting only of parentheses - (, ), {, }, [, and ], how would I write a Boolean function that takes a string and decides if it consists of valid nested parenthesis? One of the hints given was to use stack. For example --> {()[{}]}' is valid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT