Twitter Stickers problem
Anonymous User
2044

"""Problem Statement

We have a bunch of stickers which say "twitter" and we decide to cut these up into their separate letters to make new words. So, for example, one sticker would give us the letters "T", "W", "I", "T", "T", "E", "R", which we could rearrange into other word[s] (like "write", "wit", "twit", etc)

Challenge:
Write a function that takes as its input an arbitrary string and as output, returns the number of intact “twitter” stickers we would need to cut up to recreate that string.

Example: twitter_stickers(“write wit twit”) would return "3", since we would need to cut up 3 stickers to provide enough letters to write “write wit twit”
"""

def twitter_stickers(s):
    """s = 'write wit twit
    """
    sticker = 'twitter'
    
    res = 1
    
    char_count = {} #count for 1 sticker
    
    for char in sticker:
        if char in char_count:  #O(1)
            char_count[char] += 1
        else:
            char_count[char] = 1
            
    s_count = {}
    
    for c in s:
        if c.isalpha():
            if c not in char_count:
                return 0
            
            if c in s_count:
                s_count[c] += 1
            else:
                s_count[c] = 1
                
            if s_count[c] > res * char_count[c]:
                    res += 1
    
    return res


assert twitter_stickers("write wit twit") == 3
assert twitter_stickers("twitter") == 1
assert twitter_stickers('rty') == 0
assert twitter_stickers('ttttww')  == 2 

I realized that instead of building charcount I could have just initialized it. The interviewer kept on asking for optimization and I just focused on time-complexity. Basically he wanted a static sticker_char_count. Was it a big blunder on my part?

Comments (6)