๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/12951?language=java
ํ์ด
import java.util.*;
class Solution {
public String solution(String s) {
String answer = "";
// " " ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ์๋ผ์ ๋ฐฐ์ด์ ๋ฃ์
String[] arr = s.split(" ");
// ์๋ฆฐ ๋ฌธ์์ด์ ์์๋๋ก ์ฒ๋ฆฌ
for(int i=0; i<arr.length; i++) {
String now = arr[i];
// ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 0์ด๋ผ๋ฉด(๊ณต๋ฐฑ ์ด๋ผ๋ฉด)
// answer์ " "๋ง ์ถ๊ฐ
if(arr[i].length() == 0) {
answer += " ";
}
// ๋ฌธ์๊ฐ ์๋ค๋ฉด
else {
// 0๋ฒ์งธ ๋ฌธ์๋ ๋๋ฌธ์๋ก
answer += now.substring(0, 1).toUpperCase();
// 1๋ฒ์งธ ๋ฌธ์๋ถํฐ ๋ง์ง๋ง๊น์ง๋ ์๋ฌธ์๋ก
answer += now.substring(1, now.length()).toLowerCase();
// ๋ง์ง๋ง์ " " ์ถ๊ฐ
answer += " ";
}
}
// ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด์ ๋งจ ๋ง์ง๋ง์ด " " ๋ผ๋ฉด ๋ฐ๋ก answer ๋ฐํ
if(s.substring(s.length()-1, s.length()).equals(" ")){
return answer;
}
// ๋งจ ๋ง์ง๋ง " " ์ ๊ฑฐํ๊ณ answer ๋ฐํ
return answer.substring(0, answer.length()-1);
}
}
'๐๏ธ Algorithm > โฌ ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โฌ [Programmers] [Java] [Level2] ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ (1) | 2024.01.04 |
---|---|
โฌ [Programmers] [Java] [Level2] ์ต์๊ฐ ๋ง๋ค๊ธฐ (2) | 2024.01.03 |
โฌ [Programmers] [Java] [Level2] ์ต๋๊ฐ๊ณผ ์ต์๊ฐ (0) | 2023.12.14 |
โฌ [Programmers] [Java] [Level1] ์๋ฆฟ์ ๋ํ๊ธฐ (0) | 2023.12.11 |
โฌ [Programmers] [Python] [Level3] ์ฌํ ๊ฒฝ๋ก (1) | 2023.05.10 |