dlehdanakf / Codingtest-Study

알고리즘 코딩테스트 토막지식 정리
1 stars 0 forks source link

Sherlock and the Valid String #12

Open dlehdanakf opened 3 years ago

dlehdanakf commented 3 years ago

Sherlock and the Valid String

풀이과정

function isValid(s) {
    const obj = new Map;
    for(const e of s) {
        obj.set(e, (obj.get(e) || 0) + 1);
    }

    const _v = [...obj.values()], _s = new Set(_v);
    if(_s.size === 1) {
        return 'YES';
    } else if(_s.size === 2) {
        const min = Math.min(..._s), max = Math.max(..._s);
        if(max - min === 1 && _v.filter(e => e === max).length === 1) return 'YES';
        return 'NO';
    }

    return 'NO';
}

오답내용

function isValid(s) {
    const obj = new Map;
    for(const e of s) {
        obj.set(e, (obj.get(e) || 0) + 1);
    }

    const _v = [...obj.values()], _s = new Set(_v);
    if(_s.size === 1) {
        return 'YES';
    } else if(_s.size === 2) {
        const min = Math.min(..._s), max = Math.max(..._s);
        if(max - min === 1 && _v.filter(e => e === max).length === 1) return 'YES';
        if(min === 1 && _v.filter(e => e === min).length === 1) return 'YES';
        return 'NO';
    }

    return 'NO';
}