We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Sherlock and the Valid String
  5. Discussions

Sherlock and the Valid String

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • embrown801
    2 months ago+ 0 comments

    Counter is nice.

    def isValid(s):
        counts = dict(Counter(s))
        count_freqs = dict(Counter(counts.values()))
        if len(count_freqs) == 1:
            return 'YES'
        elif len(count_freqs) > 2:
            return 'NO'
        elif count_freqs.get(1, 0) == 1:
            return 'YES'
        elif abs(list(count_freqs.keys())[-1] - list(count_freqs.keys())[0]) == 1 and count_freqs[max(list(count_freqs.keys()))] == 1:
            return 'YES'
        
        return 'NO'
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy