저장을 습관화

프로그래머스 LV.0 A로 B 만들기 본문

코딩 테스트/프로그래머스 - 자바스크립트

프로그래머스 LV.0 A로 B 만들기

ctrs 2023. 9. 22. 20:43

프로그래머스 LV.0 A로 B 만들기

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 명

A로 B 만들기


2. 문제 설명

문자열 before와 after가 매개변수로 주어질 때, before의 순서를 바꾸어 after를 만들 수 있으면 1을, 만들 수 없으면 0을 return 하도록 solution 함수를 완성해보세요.


3. 제한 사항

- 0 < before의 길이 == after의 길이 < 1,000

- before와 after는 모두 소문자로 이루어져 있습니다.


4. 예시

before after result
"olleh" "hello" 1
"allpe" "apple" 0


5. 기본 제공 코드

function solution(before, after) {
    var answer = 0;
    return answer;
}


6. 제출한 내 답

const solution = (before, after) => {
  return before.split("").sort().join("") === after.split("").sort().join("")
    ? 1
    : 0;
};

 

6-2. VSC에 작성한 내용

const solution = (before, after) => {
  // console.log(before.split("").sort());
  // console.log(after.split("").sort());
  return before.split("").sort().join("") === after.split("").sort().join("")
    ? 1
    : 0;
};

// 테스트
console.log(solution("olleh", "hello"));
console.log(solution("allpe", "apple"));


7. 특이사항

문제를 착각해서 'before를 뒤집어서 after를 만들 수 있는가'라고 생각했음

오답 코드

const solution = (before, after) => {
  return before.split("").reverse().join("") === after ? 1 : 0;
};

 

8. 다른 사람이 작성한 답

8-1. 가장 많이 쓰인 풀이 방법

function solution(before, after) {
    return before.split('').sort().join('') === after.split('').sort().join('') ? 1 : 0;
}

나와 같음