๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[JAVA] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ•ธ๋“œํฐ๋ฒˆํ˜ธ๊ฐ€๋ฆฌ๊ธฐ

by yunamom 2022. 4. 6.
๋ฐ˜์‘ํ˜•

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

๐Ÿ’ก๋ฌธ์ œ ์„ค๋ช…

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.

์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, 

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

 
์ œํ•œ ์กฐ๊ฑด
  • phone_number๋Š” ๊ธธ์ด 4 ์ด์ƒ, 20์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ
 
phone_number return
"01033334444" "*******4444"
"027778888" "*****8888"
package ํ•ธ๋“œํฐ๋ฒˆํ˜ธ๊ฐ€๋ฆฌ๊ธฐ;

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

public class Algorithm {
	
	public static void main(String[] args) {
		/* ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. */
		Solution test = new Solution();
		String phone_number="01033334444";
		
		System.out.println(test.solution(phone_number));
	}
}
// *******4444

 

๐Ÿ‘‡์ •๊ทœ์‹์„ ํ™œ์šฉํ•œ ๊ณ ์ˆ˜๋‹˜์˜ ํ’€์ด ( ๋ฉ‹์ ธ.. )

. ์ž„์˜์˜ ๋ฌธ์ž ํ•œ ๊ฐœ
 (?=.) ๋’ท์ชฝ์— ์ž„์˜์˜ ๋ฌธ์ž ํ•œ ๊ฐœ๋ฅผ ์ œ์™ธํ•˜๊ณ  ์„ ํƒ
{์ˆซ์ž} ์ˆซ์ž ๋งŒํผ์˜ ์ž๋ฆฟ์ˆ˜
.(?=.{4}) ๋’ค์ชฝ์—์„œ ์ž„์˜์˜ ๋ฌธ์ž 4๊ฐœ๋ฅผ ์ œ์™ธํ•œ ์ž„์˜์˜ ๋ฌธ์ž ํ•œ ๊ฐœ ์„ ํƒ
class Solution {
  public String solution(String phone_number) {
    return phone_number.replaceAll(".(?=.{4})", "*");
  }
}

๐Ÿ‘‡์•„๋ž˜ ๋ฐฉ๋ฒ•๋„ ๋„ˆ๋ฌด ์ข‹๋‹ค๐Ÿ˜„

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณผ๋•Œ๋งˆ๋‹ค ์—ฌ๋Ÿฌ ๋ฐฉ์‹์˜ ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ๊ณต๋ถ€๊ฐ€ ๋งŽ์ด ๋œ๋‹ค :)

 

300x250

์ฝ”๋“œ