😎풀이

  1. s를 공백을 기준으로 분리
  2. 분리된 단어 만큼의 새 배열 준비
  3. 분리 단어 순회
    3-1. 단어와 해당 단어의 인덱스 분리
    3-2. 새 정렬될 배열에 인덱스 별 단어 할당
  4. 새 정렬 배열 공백을 기준으로 병합 해 문자열 형태로 반환환
function sortSentence(s: string): string {
    const splitted = s.split(" ")
    const sorted = new Array(splitted.length)
    for(const word of splitted) {
        const n = word.length
        const idx = word.slice(n - 1)
        const onlyStr = word.slice(0, n - 1)
        sorted[Number(idx) - 1] = onlyStr
    }
    return sorted.join(' ')
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글