Deep_Dev
article thumbnail

 


์ฒ˜์Œ์—” ๋Œ€๋ฌธ์ž ๋ฐฐ์—ด, ์†Œ๋ฌธ์ž ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด๋†“๊ณ  index๋ฅผ ์ด์šฉํ•ด์•ผํ–ˆ๋”๋‹ˆ 

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

๊ทธ๋ƒฅ ๋ฐ”๋กœ ์•„์Šคํ‚ค๋„˜๋ฒ„๋ฅผ ์ด์šฉํ•˜๋Š”๊ฒƒ์ด๋‹ค.

 

 

class Solution {
    public String solution(String s, int n) {
          String answer = "";
        
        for(int i=0; i<s.length(); i++){
            char ch = s.charAt(i);
            if(Character.isLowerCase(ch)){
                ch = (char)((ch - 'a' + n) % 26 + 'a');
            } // else๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๊ณต๋ฐฑ๊ณผ ๋Œ€๋ฌธ์ž๋ฅผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. 
            else if(Character.isUpperCase(ch)){
                ch = (char)((ch - 'A' + n) % 26 + 'A');
            }
            answer += ch;
        }
        return answer;
    }
}