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
- Stack
- 자바
- dynamic programming
- 코딩테스트
- Tree
- Math
- java
- string
- geometry
- Data Structure
- Number Theory
- database
- SQL
- sorting
- Class
- 구현
- simulation
- bit manipulation
- 코테
- Binary Tree
- implement
- Matrix
- hash table
- two pointers
- array
- Method
- 파이썬
- Binary Search
- Counting
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2124. Check if All A's Appears Before All B's 본문
1. Input
1) 문자열 s
2. Output
1) s에 있는 모든 a가 모든 b보다 앞에 위치하면 true, 하나라도 b보다 뒤에 있으면 false를 반환
3. Constraint
1) 1 <= s.length <= 100
2) s는 a와 b로 이루어져 있다.
4. Example
Input: s = "aaabbb" -> Output: true
Input: s = "abab" -> Output: false
5. Code
1) 첫 코드(2022/06/18)
int ai = -1;
int bi = -1;
boolean notFindB = true;
boolean notFindA = true;
int len = s.length();
for(int i=0 ; i<len ; i++){
if(s.charAt(i) == 'b' && notFindB){
bi = i;
notFindB = false;
}
if(s.charAt(len-1-i) == 'a' && notFindA){
ai = len-1-i;
notFindA = false;
}
}
if(ai == -1 || bi == -1)
return true;
return ai<bi;
2) 다시 풀어본 코드(2023/01/13)
int ai = -1, bi = -1;
for(int i=s.length()-1 ; i>=0 ; i--){
if(s.charAt(i)=='a'){
ai = i; break;
}
} // find last a
for(int i=ai-1 ; i>=0 ; i--){
if(s.charAt(i)=='b'){
bi = i; break;
}
} // find b in front of a
return bi==-1 || ai==-1 || ai<bi;
- 1번보다 훨씬 좋은 성능을 보여줬다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2138. Divide a String Into Groups of Size k (0) | 2023.01.13 |
---|---|
[LeetCode/Easy] 2129. Capitalize the Title (0) | 2023.01.13 |
[LeetCode/Easy] 2119. A Number After a Double Reversal (0) | 2023.01.13 |
[LeetCode/Easy] 2114. Maximum Number of Words Found in Sentences (0) | 2023.01.13 |
[LeetCode/Easy] 2108. Find First Palindromic String in the Array (0) | 2023.01.13 |