ํšŒ๋ฌธ ๋ฌธ์ž์—ด = ํŒฐ๋ฆฐ๋“œ๋กฌ

ํšŒ๋ฌธ ๋ฌธ์ž์—ด(ํŒฐ๋ฆฐ๋“œ๋กฌ) : ์•ž์—์„œ๋ถ€ํ„ฐ ์ฝ์œผ๋‚˜ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฝ์œผ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด.

 

๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ. ๋ฌธ์ž์—ด์— ์–ด๋– ํ•œ ์กฐ๊ฑด์„ ์ฃผ๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

 

1. ๋ฌธ์ œ

์•ž์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๋’ค์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด "YES", ์•„๋‹ˆ๋ฉด “NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•  ๋•Œ ์•ŒํŒŒ๋ฒณ๋งŒ ๊ฐ€์ง€๊ณ  ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•˜๋ฉฐ, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•ŒํŒŒ๋ฒณ ์ด์™ธ์˜ ๋ฌธ์ž๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. 

 

์ž…๋ ฅ์˜ˆ์ œ | found7, time: study; Yduts; emit, 7 Dnuof

์ถœ๋ ฅ์˜ˆ์ œ | YES

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

ํšŒ๋ฌธ ๋ฌธ์ž์—ด์„ ํ’€์—ˆ๋˜ ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™๋‹ค.

ํšŒ๋ฌธ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ œ์ผ ์•ž๊ณผ ์ œ์ผ ๋์ด ๊ฐ™๋‹ค๋Š” ๋ง์ด๊ณ ,

์•ž์—์„œ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž์™€ ๋’ค์—์„œ ๋ถ€ํ„ฐ ํ•œ๊ธ€์ž๋ฅผ ๋น„๊ตํ•ด์•ผํ•œ๋‹ค. 

์•ŒํŒŒ๋ฒณ๋งŒ ๊ฐ€์ง€๊ณ  ํšŒ๋ฌธ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์•ผํ•˜๋ฏ€๋กœ ๋ฌธ์ž์—ด์— ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

 

 

2. ๋ฌธ์ œ ํ’€์ด

1. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž or ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค.

2. ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด ๋‚ด์— ์•ŒํŒŒ๋ฒณ์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. 

2. for ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋Œ๋ฉด์„œ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์™€ ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž ์ง์ง€์–ด์„œ ๋น„๊ตํ•ด์ค€๋‹ค. 

function solution(str){
	let answer = "YES";
    str = str.toLowerCase().replace(/[^a-z]/g, '');
    // ์ฒ˜์Œ๋ถ€ํ„ฐ ์†Œ๋ฌธ์ž๋‚˜ ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ์‹œํ‚จ๋‹ค.
    // ์†Œ๋ฌธ์ž๋กœ ํ†ต์ผํ•˜๊ณ  ์—ฌ๊ธฐ๋‹ค๊ฐ€ replace๋ฅผ ํ•ด์„œ ์†Œ๋ฌธ์ž๋งŒ ๋‘๊ณ  ๋‚˜๋จธ์ง€ ์ œ๊ฑฐ -> ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ
	// ์ •๊ทœํ‘œํ˜„์‹์—์„œ ^ ํ‘œ์‹œ -> ๋ถ€์ • 
	// [^a-z/g] -> ์†Œ๋ฌธ์ž a-z๊นŒ์ง€ ์•„๋‹Œ๊ฑธ ์ฐพ์•„๋ผ global -> ์ „์ฒด ์˜์—ญ์—์„œ
    let len = str.length;
    for(let i=0; i<len; i++){
    	if(str[i] !== str[len-i-1]) return "NO";
    }
    return answer;
}

let str = "found7, time: study; Yduts; emit, 7Dnuof";
console.log(solution(str));
function solution(str){
	let answer = "YES";
    str = str.toLowerCase().replace(/[^a-z]/g, '');
    let len = str.length;
    for(let i=0; i<len; i++){
    	if(str !== str.split('').reverse('').join('')) return "NO";
    }
    return answer;
}

let str = "found7, time: study; Yduts; emit, 7Dnuof";
console.log(solution(str));

 

3. ์‚ฌ์šฉ๊ฐœ๋…

 

์ •๊ทœํ‘œํ˜„์‹

์ •๊ทœ์‹์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• 1. ์ •๊ทœ์‹ ๋ฆฌํ„ฐ๋Ÿด์‚ฌ์šฉ (์Šฌ๋ž˜์‹œ / ๋กœ ๊ฐ์‹ธ๋Š” ํŒจํ„ด)

์ •๊ทœ์‹์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• 2. RegExp ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

+ Recent posts