def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s)==len(t): for char in set(s): if s.count(char)!=t.count(char): return False return True else: return False
By converting 's' into a Set, we only compare against the unique values in s, and hence reduce the number of comparisons.
What if two strings are "btt" and "tbb" ? Your solution will still return True but the answer is false.
It returns false. You should try dry running it once. It converts the first one into a set. Hence set(s) contains 'b' and 't'. It then checks the count of 'b' in both s and t. Since the count is not equal, it returns false.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.