An open API service indexing awesome lists of open source software.

https://github.com/emahtab/balanced-brackets

Balanced Brackets
https://github.com/emahtab/balanced-brackets

balanced-brackets balanced-parentheses problem-solving stack

Last synced: 5 months ago
JSON representation

Balanced Brackets

Awesome Lists containing this project

README

          

# Balanced Brackets (Valid Parentheses)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if: Open brackets are closed by the same type of brackets and in the correct order.

#### Note that an empty string is also considered valid.
```
Example 1:
Input: "()"
Output: true

Example 2:
Input: "()[]{}"
Output: true

Example 3:
Input: "(]"
Output: false
```

## Implementation
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class App {
public static void main(String[] args) {
String s = ")[]}";
System.out.println(isValid(s));
}

public static boolean isValid(String s) {
Stack stack = new Stack();
Map map = new HashMap();
map.put("]", "["); map.put(")", "("); map.put("}", "{");
for(int i = 0; i < s.length(); i++) {
String st = Character.toString(s.charAt(i));
if(!map.containsKey(st)) {
stack.push(st);
} else {
if(stack.isEmpty()) {
return false;
} else {
String peek = stack.peek();
if(peek.equals(map.get(st))) {
stack.pop();
} else {
return false;
}
}
}
}
return stack.isEmpty();
}
}

```