Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- two pointers
- Binary Tree
- Math
- Data Structure
- string
- dynamic programming
- 구현
- 파이썬
- hash table
- geometry
- Matrix
- Number Theory
- Counting
- Class
- Binary Search
- 코딩테스트
- SQL
- Stack
- Tree
- 자바
- simulation
- bit manipulation
- implement
- sorting
- database
- java
- 코테
- array
- greedy
- Method
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1047. Remove All Adjacent Duplicates In String 본문
1. Input
1) String s
2. Output
1) 다음과 같은 규칙으로 문자열을 지워나갈 때, 최종 문자열을 반환
- 인접한 2개의 문자가 같으면 지운다.
- 더이상 지울 문자가 없을 때까지 반복한다.
3. Constraint
1) 1 <= s.length <= 10^5
2) s는 영어 소문자로만 이루어져 있다.
4. Example
Input: s = "abbaca" -> Output: "ca"
Input: s = "azxxzy" -> Output: "ay"
5. Code
1) 첫 코드(2023/04/04)
for(int i=0 ; i<s.length()-1 ; i++){
if(s.charAt(i)==s.charAt(i+1)){
s = s.substring(0,i) + s.substring(i+2,s.length());
i = Math.max(-1,i-2);
}
}
return s;
- 느리다. 메모리는 그럭저럭
2) 스택으로 구현해본 코드(2023/04/04)
Stack<Character> st = new Stack<>();
for(int i=0 ; i<s.length() ; i++){
char c = s.charAt(i);
if(!st.empty() && st.peek()==c)
st.pop();
else
st.push(c);
}
s = "";
while(!st.empty())
s += st.pop();
return new StringBuffer(s).reverse().toString();
- 1번보다 약간 빨라짐. 메모리는 더 먹음
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1160. Find Words That Can Be Formed by Characters (0) | 2023.04.06 |
---|---|
[LeetCode/Easy] 1184. Distance Between Bus Stops (0) | 2023.04.05 |
[LeetCode/Easy] 1037. Valid Boomerang (0) | 2023.04.04 |
[백준 온라인 저지] 9506. 약수들의 합 (0) | 2023.04.04 |
[백준 온라인 저지] 5086. 배수와 약수 (0) | 2023.04.04 |