View

๋ฐ˜์‘ํ˜•

์‹œ์ € ์•”ํ˜ธ

https://school.programmers.co.kr/learn/courses/30/lessons/12926


๋ฌธ์ œ ์„ค๋ช…

์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "AB"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "BC"๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด "DE"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. "z"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "a"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • ๊ณต๋ฐฑ์€ ์•„๋ฌด๋ฆฌ ๋ฐ€์–ด๋„ ๊ณต๋ฐฑ์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • s์˜ ๊ธธ์ด๋Š” 8000์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • n์€ 1 ์ด์ƒ, 25์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

s n result

"AB" 1 "BC"
"z" 1 "a"
"a B z" 4 "e F d"

import java.util.*;

class Solution {
    public String solution(String s, int n) {
        
        /*
            1. ๊ณต๋ฐฑํฌํ•จ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ์˜ ๋ฐ˜ํ™˜ ๋ฐฐ์—ด ์ƒ์„ฑ
            2. ๋Œ€๋ฌธ์ž,์†Œ๋ฌธ์ž, ๊ณต๋ฐฑ ๋ถ„๋ฅ˜
                2-1. a-z ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ์‹œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€
                2-2. ๊ณต๋ฐฑ์ผ ๋•Œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€
         */
        
        char arr[] = new char[s.length()];
        
        for (int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            
            if (Character.isUpperCase(c)) //๋Œ€๋ฌธ์ž์ผ ๊ฒฝ์šฐ
            {
                c = (char)((c-'A'+n) % 26+'A');
                
            }
            else if (Character.isLowerCase(c)){
                
                 c = (char)((c-'a'+n) % 26+'a');
            }
            else c=' ';
            
            arr[i] = c;
        }
    
        String answer = String.valueOf(arr);
        return answer;
    }
}
๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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