🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this blog post, we'll explore a common problem in string processing and stack usage - the "Valid Parentheses" problem. This problem is a typical example in coding interviews to assess understanding of basic data structures in Java.
Problem
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', the task is to determine if the input string is valid. An input string is considered valid if:
1. Open brackets are closed by the same type of brackets.
2. Open brackets are closed in the correct order.
Note: An empty string is also considered valid.
2. Solution Steps
1. Initialize a stack to keep track of opening brackets.
2. Iterate through each character in the string.
3. If an opening bracket is encountered, push it onto the stack.
4. If a closing bracket is encountered, check if it matches the top element of the stack.
5. If it matches, pop the top element from the stack; otherwise, return false.
6. After the iteration, if the stack is empty, return true; otherwise, return false.
3. Code Program
import java.util.Stack;
public class Solution {
// Main method for testing
public static void main(String[] args) {
String input1 = "({[]})";
String input2 = "([)]";
System.out.println("Input: " + input1 + " - Valid: " + isValid(input1));
System.out.println("Input: " + input2 + " - Valid: " + isValid(input2));
}
// Method to check if the string has valid parentheses
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); // Stack to keep track of opening brackets
for (char c : s.toCharArray()) {
// Push corresponding opening bracket onto stack if closing bracket is found
if (c == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop();
} else if (c == '}' && !stack.isEmpty() && stack.peek() == '{') {
stack.pop();
} else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop();
} else if (c == '(' || c == '{' || c == '[') {
stack.push(c); // Push opening bracket onto stack
} else {
return false; // Invalid case
}
}
return stack.isEmpty(); // Return true if stack is empty, false otherwise
}
}
Output:
Input: ({[]}) - Valid: true
Input: ([)] - Valid: false
Explanation:
The input "({[]})" is valid as each type of opening bracket is matched and closed by the same type of closing bracket in the correct order.
The method isValid uses a stack to keep track of the opening brackets. When a closing bracket is encountered, it checks whether it matches the last opened bracket.
Conversely, the input "([)]" is invalid as the brackets are not closed in the correct order, demonstrated by the round bracket closing before the square bracket.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment