algorithm/python

hackerrank/String/Sherlock and the Valid String

아르르르를를르 2020. 3. 8. 21:05

https://www.hackerrank.com/interview/interview-preparation-kit/strings/challenges

아.. 또 노가다로 푼거 같다.

 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the isValid function below.
def isValid(s):
    diff = {}
    for i in s:
        if i not in diff:
            diff[i] = 1
        else:
            diff[i] += 1
    print(diff)
    a = sorted([i for i in diff.values()])
    l = len(a)
    if a[0] == a[l-1]:
        return "YES"
    elif a[0] != a[1] and a[l-1] == a[l-2]:
        if a[0] == 1:
            return "YES"
        else:
            return "NO"
    elif a[0] == a[1] and a[l-1] != a[l-2]:
        if a[l-1]-1 == a[l-2]:
            return "YES"
        else:
            return "NO"
    return "NO"


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = isValid(s)

    fptr.write(result + '\n')

    fptr.close()