码迷,mamicode.com
首页 > 其他好文 > 详细

242. Valid Anagram

时间:2017-06-10 18:28:01      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:hub   elf   http   min   div   inpu   interview   esc   pytho   

https://leetcode.com/problems/valid-anagram/#/description

 

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

 

Sol: 
 

There are two ways of thinking about this problem, if two strings have the same frequency of letters/element (meaning each letter shows up the same number of times in both strings) then they are anagrams of eachother. On a similar vien of logic, if two strings are equal to each other once they are sorted, then they are also anagrams of each other.

You would be able to implement this second solution pretty easily in Python:

 

def anagram(s1,s2):
    
    # Remove spaces and lowercase letters
    s1 = s1.replace( ,‘‘).lower()
    s2 = s2.replace( ,‘‘).lower()
    
    # Return boolean for sorted match.
    return sorted(s1) == sorted(s2)

 

 

Now the above sorting approach is simple, but is actually not optimal and in an interview setting you would probably be asked to implement a more manual solution involving just counting the number of letters in each string to test your ability to understand hash tables. Let‘s build out a fuller solution using counting and Python dictionaries:

 

class Solution(object):
    def isAnagram(self, s1, s2):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """

    # Remove spaces and lowercase letters
        s1 = s1.replace( ,‘‘).lower()
        s2 = s2.replace( ,‘‘).lower()
    
    # Edge Case to check if same number of letters
        if len(s1) != len(s2):
            return False
    
    # Create counting dictionary (Note could use DefaultDict from Collections module)
        count = {}
    
    
        
    # Fill dictionary for first string (add counts)
        for letter in s1:
            if letter in count:
                count[letter] += 1
            else:
                count[letter] = 1
            
    # Fill dictionary for second string (subtract counts)
        for letter in s2:
            if letter in count:
                count[letter] -= 1
            else:
                count[letter] = 1
    
    # Check that all counts are 0
        for k in count:
            if count[k] != 0:
                return False

    # Otherwise they‘re anagrams
        return True

 

 

 

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Array%20Sequences/Array%20Sequences%20Interview%20Questions/Array%20Sequence%20Interview%20Questions%20-%20SOLUTIONS/Anagram%20Check%20-%20SOLUTION.ipynb

242. Valid Anagram

标签:hub   elf   http   min   div   inpu   interview   esc   pytho   

原文地址:http://www.cnblogs.com/prmlab/p/6979350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!