Strings – permutation of one another

Q. Given two strings, write a method to decide if one is a permutation of the other.

def string_permutation_memory_efficient(str1, str2):
    if len(str1) != len(str2):
        return False
    else:
        if sorted(str1) == sorted(str2):
            return True
        else:
            return False


def string_permutation_time_efficient(str1, str2):
    if len(str1) != len(str2):
        return False
    
    letters = {}
    
    for entries in str1:
        if entries in letters:
            letters[entries] += 1
        else:
            letters[entries] = 1
        
    for entries in str2:
        if entries in letters:
            letters[entries] -= 1
            if letters[entries] < 0:
                return False
        else:
            return False
        
    return True
            
 
    
if __name__ == "__main__":
    print string_permutation_memory_efficient("god","dog")
    print string_permutation_memory_efficient("debanjan","vinay")
    
    print string_permutation_time_efficient("god", "dog")
    print string_permutation_time_efficient("debanjan", "vinay")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s