Page tree
Skip to end of metadata
Go to start of metadata
live
def isBeautifulString(inputString):
    cnts = [0] * 26
    
    for c in inputString: 
        idx = ord(c)-97        
        cnts[idx] += 1
        
    return True if cnts[::-1] == sorted(cnts) else False
test case
testcases = {"bbbaacdafe": True,
             "aabbb": False, 
             "bbc": False, 
             "bbbaa" : False, 
             "abcdefghijklmnopqrstuvwxyzz": False, 
             "abcdefghijklmnopqrstuvwxyz": True, 
             "abcdefghijklmnopqrstuvwxyzqwertuiopasdfghjklxcvbnm": True, 
             "fyudhrygiuhdfeis": False, 
             "zaa": False, 
             "zyy": False,}

def test_solution(testcases) : 
    for t in testcases.keys():
        result = isBeautifulString(t)
        
        if testcases[t] != result:
            print(f"Wrong Case: {t} / expected: {testcases[t]} / your : {result}")
        
    print("Test Finished")


##  Refactoring

Refactoring
import string 

def isBeautifulString(inputString):
    cnts = [inputString.count(c) for c in string.ascii_lowercase]

    # return cnts[::-1] == sorted(cnts)
    return cnts == sorted(cnts, reverse=True)
  • python battery included 만세


ref


  • No labels