๊ฐœ๋ฐœ์ผ์ง€

[Java] ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ ๋ณธ๋ฌธ

Algorithm

[Java] ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ

O'mil 2024. 5. 10. 12:50
728x90

๐Ÿ”— ๋งํฌ

ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ

 

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

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

programmers.co.kr

๐Ÿ“Œ ๋ฌธ์ œ ์„ค๋ช…

  • ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ,
    ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ *์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด ๋ฆฌํ„ด

๐Ÿ‘€ ์˜ˆ์ œ

โœ๏ธ ํ’€์ด ์ˆœ์„œ

  1. ์ „ํ™”๋ฒˆํ˜ธ์—์„œ ๋’ค์— 4์ž๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ฉด *์ถ”๊ฐ€
  2. ๋’ค์— 4์ž๋ฆฌ๋ฉด ์ „ํ™”๋ฒˆํ˜ธ ์ถ”๊ฐ€

๐Ÿ’ป ์ฝ”๋“œ

class Solution {
    public String solution(String phone_number) {
        String answer = "";
        
        
       for (int i = 0; i < phone_number.length(); i++) {
           if (i < (phone_number.length() - 4)) answer += "*";
           else answer += phone_number.charAt(i);
       }
        return answer;
    }
}

๐Ÿ’ก ๊ฒฐ๊ณผ

 


๐Ÿ”’ ๋””๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

class Solution {
  public String solution(String phone_number) {
     char[] ch = phone_number.toCharArray();
     for(int i = 0; i < ch.length - 4; i ++){
         ch[i] = '*';
     }
     return String.valueOf(ch);
  }
}
  • toCharArray(): String ๋ฌธ์ž์—ด์„ charํ˜• ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์„œ ๋ฐ˜ํ™˜
  • ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค์„œ ๋’ค์— 4์ž๋ฆฌ ๋นผ๊ณ  *๋กœ ๋ฐ”๊ฟ”์ค€ ํ›„
  • String.valueOf() ์‚ฌ์šฉํ•ด์„œ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๋ฐ˜ํ™˜

๐Ÿ“šReference

์ž๋ฐ” toCharArray()

728x90
Comments