Valid Parentheses Problem in JavaA 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.
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. ApproachIt 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 StackFileName: 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India