๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

โฌ› [Programmers] [Java] [Level2] JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ

Dbswnstjd 2023. 12. 23. 17:22

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12951?language=java

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

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);
    }

}