일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- aggregation
- springboot
- 차트
- zip 암호화
- MySQL
- API
- Test
- plugin
- query
- zip 파일 암호화
- token filter test
- high level client
- 900gle
- analyzer test
- licence delete curl
- Kafka
- flask
- Mac
- ELASTIC
- License
- Python
- TensorFlow
- docker
- 파이썬
- aggs
- Elasticsearch
- Java
- matplotlib
- license delete
- sort
Archives
- Today
- Total
개발잡부
[codility] Brackets 본문
반응형
A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:
S is empty;
S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
S has the form "VW" where V and W are properly nested strings.
For example, the string "{[()()]}" is properly nested but "([)()]" is not.
Write a function:
class Solution { public int solution(String S); }
that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.
For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..200,000];
string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")".
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Stack<Character> stack = new Stack<>();
char lastChar;
for (char c : S.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else {
if (stack.isEmpty()) {
return 0;
}
lastChar = stack.pop();
if (c == ')' && lastChar != '(') {
return 0;
}
if (c == ']' && lastChar != '[') {
return 0;
}
if (c == '}' && lastChar != '{') {
return 0;
}
}
}
return stack.isEmpty() ? 1 : 0;
}
}
반응형
'이직' 카테고리의 다른 글
hadoop 준비 (1) | 2022.07.27 |
---|---|
[codility] Nesting (0) | 2022.07.26 |
[codility] NumberOfDiscIntersections (0) | 2022.07.25 |
[codility] Triangle (0) | 2022.07.25 |
[codility] MaxProductOfThree (0) | 2022.07.25 |
Comments