Javatpoint Logo
Javatpoint Logo

Valid Parentheses Problem in Java

A string inputStr is given to us. The string inputStr only contains '[', ']', '{', '}', '(', and ')'. Our task is to determine whether the string inputStr is a valid string or not. For the string to be valid, the following criteria have to be satisfied.

  • Every open bracket should be closed by the same bracket type.
  • The order in which brackets are closed must be in the correct order.
  • Each close bracket should have mapped with the corresponding open bracket of the same type. It is evident that the closing bracket should come after the opening bracket.

Example 1:

Input

String inputStr = "(())"

Output: The input string is a valid string.

Explanation: The open bracket has been closed appropriately, i.e., in the correct order. Hence, the given string is a valid string.

Example 2:

Input

String a1 = "(({)})"

Output: The input string is not a valid string.

Explanation: The curly bracket { has been opened correctly. But before the closing curly bracket }, we have the closing round bracket ). Therefore, the given string is not a valid string.

Example 3:

String a1 = "(({}))"

Output: The input string is a valid string.

Explanation: All the brackets have been opened and closed correctly. Hence, the given string is the valid input string.

Example 4:

String a1 = ")([{}])"

Output: The input string is a not valid string.

Explanation: All the brackets, barring one, have been opened and closed correctly. However, that one bracket, at last, is a opened round bracket and is not correctly mapped with the closing round bracket. Hence, the given string is not a valid input string.

Example 5:

String a1 = "()(({}))("

Output: The input string is a not valid string.

Explanation: All the brackets, barring one, have been opened and closed correctly. However, that one bracket, at last, is an opened round bracket and is not correctly mapped with the closing round bracket. Hence, the given string is not a valid input string.

Approach

It is evident that every opening bracket has the corresponding closing bracket. Therefore, the count of brackets in the string should be even, and this serves as our base case. We can reject those strings whose sizes are not even. For strings of the size even, we have to check for each open bracket whether the corresponding closing bracket (of the same type) is available or not. If it is available, the string is a valid string; otherwise, invalid.

To check for each open bracket, we can use either recursion or iteration. Let's first use a recursive way to solve the problem.

FileName: ValidString.java

Output:

The string "(())" is a valid string.

The string "(({)})" is not a valid string.

The string "(({}))" is a valid string.

The string ")([{}])" is not a valid string.

The string "()(({}))(" is not a valid string.

Complexity Analysis: At max, each character of the string is traversed only one time. Thus, making the time complexity of the program O(N). Also, the statements written after the recursive calls go into the stack, making the space complexity of the program O(N), where N is the total number of characters present in the input string.

Now let's see the iterative implementation.

Implementation: Using Stack

FileName: ValidString1.java

Output:

The string "(())" is a valid string.

The string "(({)})" is not a valid string.

The string "(({}))" is a valid string.

The string ")([{}])" is not a valid string.

The string "()(({}))(" is not a valid string.

Complexity Analysis: The time, as well as space complexity of the program, is the same as the previous program.


Next TopicDAO Class in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA