[LeetCode] 942. DI String Match

Chobby·2025년 5월 23일
1

LeetCode

목록 보기
435/470

😎풀이

  1. s 순회
    1-1. I문자가 탐색될 시, 오름차 카운트 배열 추가 및 증가
    1-2. D문자가 탐색될 시, 내림차 카운트 배열 추가 및 감소
  2. 잔여 수량 반환
  3. 순열 반환
function diStringMatch(s: string): number[] {
    let inc = 0
    let dec = s.length
    const permutation = []
    for(const char of s) {
        if(char === 'I') {
            permutation.push(inc++)
        } else {
            permutation.push(dec--)
        }
    }
    permutation.push(inc)
    return permutation
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글