Skip to content

Instantly share code, notes, and snippets.

@stshryu
Created June 23, 2016 10:26
Show Gist options
  • Save stshryu/7f9f7dd9bccc00c159b106aa7932e88b to your computer and use it in GitHub Desktop.
Save stshryu/7f9f7dd9bccc00c159b106aa7932e88b to your computer and use it in GitHub Desktop.
https://repl.it/C5Q7/1 created by shstyoo
def sherlockString(s):
# Split string into list of characters
_s = list(s)
# Initialize our dictionary with key: letter, value: number of occurence
dict = {}
# Iterate through string
for item in _s:
# If a letter has already been viewed, increment counter by 1
if item in dict:
dict[item] = dict[item] + 1
# If a letter hasn't been viewed, create a dictionary entry for letter with occurence 1
else:
dict[item] = 1
# DEBUG print(dict)
# Initialize our second dictionary with key: number of occurences, value: number of, number of occurences
tempDict = {}
# Iterate through each value in our original dictionary with the letters
for key, value in dict.items():
# If a value has already been viewed, increment counter by 1
if value in tempDict:
tempDict[value] = tempDict[value] + 1
# If a value hasn't already been viewed, create dictionary entry for value with occurence 1
else:
tempDict[value] = 1
# DEBUG print(tempDict)
# Create list containing values of occurence from tempDict
eval = []
for key, value in tempDict.items():
eval.append(value)
# If there is one entry in tempDict (e.g. string is all one letter) return "YES"
if(len(tempDict) == 1):
print("YES")
return("YES")
# If there are two entries in tempDict, do more tests
elif(len(tempDict) == 2):
# If the list containing the values of occurence from tempDict is of size 2, and contains the value 1 return "YES"
if 1 in eval:
print("YES")
return("YES")
# Otherwise Fail all other cases. Return "NO"
else:
print("NO")
return("NO")
else:
print("NO")
return("NO")
'''
::::::INSTRUCTIONS::::::
A "valid" string is a string such that for all distinct characters in each such character occurs the same number of times in .
For example, aabb is a valid string because the frequency of both characters a and b is , whereas aabbc is not a valid string because the frequency of characters a, b, and c is not the same.
Watson gives a string to Sherlock and asks him to remove some characters from the string such that the new string is a "valid" string.
Sherlock wants to know from you if it's possible to be done with less than or equal to one removal.
Input Format
----
The first and only line contains , the string Watson gives to Sherlock.
Constraints
----
String contains lowercase letters only ().
Output Format
----
Output YES if string can be converted to a "valid" string by removing less than or equal to one character.
Else, output NO.
'''
# TEST STRINGS
t1 = 'jtqgugmcsxvdwidtcyqpogkdifapuloqykjfxruvfrshcehekoiwbpbrprahwvhliglyxynjotbaswnnnmxbkmcftvsdqajemeul'
# OUTPUT = YES
t2 = 'aabbcd'
# OUTPUT = NO
t3 = 'hfchdkkbfifgbgebfaahijchgeeeiagkadjfcbekbdaifchkjfejckbiiihegacfbchdihkgbkbddgaefhkdgccjejjaajgijdkd'
# OUTPUT = YES
t4 = 'ibfdgaeadiaefgbhbdghhhbgdfgeiccbiehhfcggchgghadhdhagfbahhddgghbdehidbibaeaagaeeigffcebfbaieggabcfbiiedcabfihchdfabifahcbhagccbdfifhghcadfiadeeaheeddddiecaicbgigccageicehfdhdgafaddhffadigfhhcaedcedecafeacbdacgfgfeeibgaiffdehigebhhehiaahfidibccdcdagifgaihacihadecgifihbebffebdfbchbgigeccahgihbcbcaggebaaafgfedbfgagfediddghdgbgehhhifhgcedechahidcbchebheihaadbbbiaiccededchdagfhccfdefigfibifabeiaccghcegfbcghaefifbachebaacbhbfgfddeceababbacgffbagidebeadfihaefefegbghgddbbgddeehgfbhafbccidebgehifafgbghafacgfdccgifdcbbbidfifhdaibgigebigaedeaaiadegfefbhacgddhchgcbgcaeaieiegiffchbgbebgbehbbfcebciiagacaiechdigbgbghefcahgbhfibhedaeeiffebdiabcifgccdefabccdghehfibfiifdaicfedagahhdcbhbicdgibgcedieihcichadgchgbdcdagaihebbabhibcihicadgadfcihdheefbhffiageddhgahaidfdhhdbgciiaciegchiiebfbcbhaeagccfhbfhaddagnfieihghfbaggiffbbfbecgaiiidccdceadbbdfgigibgcgchafccdchgifdeieicbaididhfcfdedbhaadedfageigfdehgcdaecaebebebfcieaecfagfdieaefdiedbcadchabhebgehiidfcgahcdhcdhgchhiiheffiifeegcfdgbdeffhgeghdfhbfbifgidcafbfcd'
# OUTPUT = YES
t5 = 'tfgyrknpgngtqgjccbyctwdcymwrcjtpoaflkeoxfxijxkngpjoofggsozftkpgxakptmzjxugavazwllxdtrjrrbjmrqwfxaby'
# OUTPUT = NO
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>>> {'p': 4, 'd': 4, 'u': 4, 'o': 4, 'a': 4, 'v': 4, 'x': 4, 'm': 4, 'n': 4, 'f': 4, 'e': 4, 'g': 4, 'q': 4, 'l': 4, 'k': 4, 't': 4, 'r': 4, 'c': 4, 'w': 4, 'h': 4, 's': 4, 'y': 4, 'i': 4, 'j': 4, 'b': 4}
{4: 25}
YES
{'d': 1, 'b': 2, 'a': 2, 'c': 1}
{1: 2, 2: 2}
NO
{'h': 9, 'd': 9, 'j': 9, 'f': 9, 'c': 9, 'e': 9, 'b': 9, 'a': 9, 'g': 9, 'i': 9, 'k': 10}
{9: 10, 10: 1}
YES
{'d': 111, 'h': 111, 'c': 111, 'f': 111, 'e': 111, 'i': 111, 'a': 111, 'g': 111, 'b': 111, 'n': 1}
{1: 1, 111: 9}
YES
{'d': 2, 'u': 1, 'k': 5, 'a': 5, 'v': 1, 'f': 6, 'x': 7, 'm': 3, 'n': 3, 'p': 5, 'e': 1, 'g': 9, 'b': 3, 'l': 3, 'o': 5, 't': 7, 'r': 6, 'c': 5, 'w': 4, 'z': 3, 'q': 2, 's': 1, 'y': 4, 'i': 1, 'j': 7}
{1: 5, 2: 2, 3: 5, 4: 2, 5: 5, 6: 2, 7: 3, 9: 1}
NO
=> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment