1.๋ฌธ์ œ

์†Œ๋ฌธ์ž๋กœ ๋œ ํ•œ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์ œ๊ฑฐ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋Š” ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | ksekkset

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

 

 

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

์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ์œผ๋ ค๋ฉด, for๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•œ๋‹ค. 

๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์”ฉ indexOf()๋กœ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋งŒ ๋ฐ˜ํ™˜ํ•ด์„œ anser์— ๋ฌธ์ž ๋ˆ„์ ํ•ด์ค€๋‹ค. 

function solution(str){
	let answer = "";
    for(let i=0; i<str.length; i++){
    	if(str.indexOf(str[i]) === 1) answer += str[i];
    }
    return answer;
}

console.log(solution("ksekkset"));

"keskket"๋ฅผ indexOf(str[i]) ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•œ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋‹ˆ๊นŒ

๋ฐ˜ํ™˜๋œ ์ธ๋ฑ์Šค

k/0

e/1

s/2

k/0

k/0

e/1

s/2

t/7

์›๋ž˜ ์ธ๋ฑ์Šค

k/0

e/1

s/2

k/3

k/4

e/5

s/6

t/7

 

 

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

String.indexOf(์ธ๋ฑ์Šค)

๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฉ”์†Œ๋“œ

 

String.indexOf()๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€  ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. 

function solution(str){
	let answer = 0;
    let position = str.indexOf('k');
    // k์˜ ์ฒซ๋ฒˆ์งธ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค
	while(position !== -1){
    	answer++; // 0์— ๋ˆ„์ ํ•ด์„œ ์นด์šดํŒ…ํ•ด์ค€๋‹ค
        position - s.indexOf('k', positon+1);
    }
    return answer;
}

console.log(solution("ksekkset"));

+ Recent posts