[LeetCode] 1941. Check if All Characters Have Equal Number of Occurrences

Chobby·2일 전
1

LeetCode

목록 보기
631/650

😎풀이

  1. s 순회
    1-1. 모든 요소의 빈도 조회
  2. 각 빈도가 모두 일치하는지 판별
  3. 일치한다면 true, 그렇지 않다면 false 반환환
function areOccurrencesEqual(s: string): boolean {
    const frequent = new Map()
    const firstChar = s[0]
    let count = 0
    for(let i = 0; i < s.length; i++) {
        const char = s[i]
        if(char === firstChar) count++
        frequent.set(char, (frequent.get(char) ?? 0) + 1)
    }
    for(const [_, value] of frequent) {
        if(value !== count) return false
    }
    return true
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글