Created
March 5, 2020 06:28
-
-
Save inspirit941/d01f0ed7f398bbae4f590b368d16a6c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
from collections import Counter | |
# Complete the isValid function below. | |
def isValid(s): | |
# 문자 개수를 세는 함수 | |
string_count = Counter(s) | |
# 동일한 문자열 개수가 얼마인지 확인하는 함수. | |
numbers = Counter(string_count.values()) | |
# 1. 동일한 문자열 개수가 1개인 경우. 즉 모든 문자열 개수가 동일한 경우 | |
if len(numbers.keys()) == 1: | |
return 'YES' | |
# 문자열 한 개만 지웠을 때 모든 문자열 개수가 같아야 하기 때문에, 동일한 문자열 개수는 2개를 넘을 수 없다. | |
if len(numbers.keys()) == 2: | |
# 2. 가장 많은 거 한 개만 지우면 되는 경우 | |
if (max(numbers.keys()) == min(numbers.keys()) + 1): | |
# ex) {2:7, 3:1} 인 경우, 3개인 문자열에서 하나만 없애면 모든 문자열이 2개가 된다. | |
if (numbers[max(numbers.keys())] - 1 == 0): | |
return "YES" | |
# 3. 가장 적은 거 하나만 지우면 되는 경우 | |
# ex) aabbc => {2:2, 1:1}. c만 지우면 YES 성립 | |
# ex) aabbcd => {2:2, 1:2}. 지울 수 없음. | |
if numbers[min(numbers.keys())] == 1: | |
return 'YES' | |
return "NO" | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
s = input() | |
result = isValid(s) | |
fptr.write(result + '\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment