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 |
Tags
- Matrix
- Tree
- string
- geometry
- Counting
- Data Structure
- Stack
- Number Theory
- Class
- SQL
- hash table
- two pointers
- database
- java
- bit manipulation
- array
- 코테
- simulation
- 파이썬
- Binary Tree
- 자바
- greedy
- Math
- dynamic programming
- sorting
- 구현
- Method
- 코딩테스트
- implement
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1576. Replace All ?'s to Avoid Consecutive Repeating Characters 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1576. Replace All ?'s to Avoid Consecutive Repeating Characters
무지맘 2023. 4. 13. 12:381. Input
1) String s
2. Output
1) 다음 조건을 만족하면서 s에 있는 ?를 다른 영어 소문자로 바꾼 결과를 반환
- ?은 인접한 문자와 같은 것으로 바꿀 수 없다.
- ? 이외의 문자는 바꿀 수 없다.
3. Constraint
1) 1 <= s.length <= 100
2) s는 영어 소문자와 ?으로만 이루어져 있다.
4. Example
Input: s = "?zs" -> Output: "azs"
설명: bzs, czs, ..., yzs 모두 가능하다.
5. Code
1) 첫 코드(2023/04/13)
if(s.equals("?"))
return "a";
char[] c = s.toCharArray();
for(int i=0 ; i<c.length ; i++){
if(c[i]=='?'){
if(0<i && i<c.length-1){
c[i] = 'a';
while(c[i]==c[i-1] || c[i]==c[i+1])
c[i] = (char)((int)(Math.random()*26)+'a');
} else if(i==0){
if(c[1]=='a') c[i] = 'b';
else c[i] = 'a';
} else{
if(c[i-1]=='a') c[i] = 'b';
else c[i] = 'a';
}
}
}
return new String(c);
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1592. Rearrange Spaces Between Words (0) | 2023.04.13 |
---|---|
[LeetCode/Easy] 1582. Special Positions in a Binary Matrix (0) | 2023.04.13 |
[LeetCode/Easy] 1560. Most Visited Sector in a Circular Track (0) | 2023.04.13 |
[LeetCode/Easy] 1556. Thousand Separator (0) | 2023.04.12 |
[LeetCode/Easy] 1518. Water Bottles (0) | 2023.04.12 |