phatboislym

Untitled

Oct 18th, 2023
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | Source Code | 0 0
  1. class Solution:
  2.     def equalFrequency(self, word: str) -> bool:
  3.         count: list = []
  4.  
  5.         for i in set(word):
  6.             count.append(word.count(i))
  7.  
  8.         if len(count) == 1 and count[0] != 1:
  9.             return True
  10.  
  11.         x = max(count)
  12.         y = min(count)
  13.         if y == x == 1:
  14.             return True
  15.         count_of_x = 0
  16.         count_of_y = 0
  17.  
  18.         for i in count:
  19.             if i == x:
  20.                 count_of_x += 1
  21.             if i == y:
  22.                 count_of_y += 1
  23.         if count_of_x > 1 and count_of_y != 1:
  24.             return False
  25.  
  26.         counts: set = set(count)
  27.         if len(counts) != 2:
  28.             return False
  29.         if min(counts) == max(counts) - 1 and count_of_x == 1:
  30.             return True
  31.         if min(counts) == 1 and count_of_y == 1:
  32.             return True
  33.         return False
  34.  
Advertisement
Add Comment
Please, Sign In to add comment