일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- ELASTIC
- springboot
- matplotlib
- aggregation
- Java
- 파이썬
- MySQL
- query
- analyzer test
- zip 암호화
- Test
- flask
- TensorFlow
- Python
- API
- Mac
- Elasticsearch
- plugin
- zip 파일 암호화
- License
- license delete
- 900gle
- token filter test
- licence delete curl
- Kafka
- aggs
- sort
- 차트
- docker
- high level client
Archives
- Today
- Total
개발잡부
[codility] Nesting 본문
반응형
A string S consisting of N characters is called properly nested if:
S is empty;
S has the form "(U)" where U is a properly nested string;
S has the form "VW" where V and W are properly nested strings.
For example, string "(()(())())" is properly nested but string "())" isn't.
Write a function:
class Solution { public int solution(String S); }
that, given a string S consisting of N characters, returns 1 if string 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..1,000,000];
string S consists only of the 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<>();
for (char c : S.toCharArray()){
if(c =='('){
stack.push(c);
} else {
if(stack.empty()) {
return 0;
}
if (c == '(' && stack.pop() != ')'){
return 0;
}
if (c == ')' && stack.pop() != '('){
return 0;
}
}
}
return stack.empty()? 1 : 0;
}
}
반응형
'이직' 카테고리의 다른 글
TwoSum (0) | 2022.07.27 |
---|---|
hadoop 준비 (1) | 2022.07.27 |
[codility] Brackets (0) | 2022.07.25 |
[codility] NumberOfDiscIntersections (0) | 2022.07.25 |
[codility] Triangle (0) | 2022.07.25 |
Comments