0

242. Valid Anagram leetcode.

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. My code gives false when it should give true. class Solution(object): def isAnagram(self, s, t): sed = set() sed2 = set() for el in s: sed.add(el) for e in t: sed2.add(e) for el in sed2: if el not in sed: print("false") print("true")

19th May 2023, 9:08 AM
Paul-Henry Paltmann
Paul-Henry Paltmann - avatar
11 Answers
+ 2
You should print the result of the function. print(isAnagram("abc", "cba"))
22nd May 2023, 7:02 AM
Tibor Santa
Tibor Santa - avatar
+ 7
Paul-Henry Paltmann , calling the function with: > "abcde", "beadx" the output is: false true >> a different (and simple) approach can be to sort both of the strings and then compare them for equalty.
19th May 2023, 9:39 AM
Lothar
Lothar - avatar
+ 3
Converting to set, keeps only unique letters. Duplicates are removed. For example, ESSAY is not anagram with EASY, even though they have the same letters, but each letter is not used exactly once in the other word. In your function you should return the result, rather than printing it. If there are multiple mismatches, "false" can be printed multiple times, then at the end, "true" is printed.
19th May 2023, 9:37 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Paul-Henry Paltmann Two side hints: 1. You don't need a class for this. There won't be objects, or any data persistence during code execution. A simple function is enough. 2. Use meaningful variable names. They help in understanding the logic.
19th May 2023, 11:01 AM
Emerson Prado
Emerson Prado - avatar
+ 3
Emerson Prado I agree with you on both points, but actually these parts are given by the Leetcode solution template. https://leetcode.com/problems/valid-anagram/ One good element of this scaffolding that was not followed though: the isAnagram function is not supposed to print anything, it is supposed to return a bool value (True or False).
19th May 2023, 11:06 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The sort() method does an in-place sorting of the list, and does not return anything. There is also a sorted() function which leaves the list in its original state and returns a new, sorted list. So there are two ways you can fix it (apply to both lists): list1.sort() # and then compare the original lists or srtdlst1 = sorted(list1) Read how to use sort and sorted: https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-sort/
22nd May 2023, 6:49 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Yes, thanks for your help
22nd May 2023, 7:12 AM
Paul-Henry Paltmann
Paul-Henry Paltmann - avatar
+ 1
By the way you are now on the right track about finding the correct solution. There is a lot that can be simplified. The sorted function can actually take any iterable, that includes a string, and converts it automatically to a sorted list of characters. So ultimately you can do the conversion and the comparison in a single step, without using intermediary variables. The function should return the boolean result of the comparison, so even your if condition is not necessary. Simplified version of your function looks like this: def isAnagram(s, t): return sorted(s) == sorted(t)
22nd May 2023, 7:08 AM
Tibor Santa
Tibor Santa - avatar
0
So this code still returns None.... I did it exactly like it should be def isAnagram(s, t): list1= [] list2 = [] list1.extend(s) list2.extend(t) srtdlst1= list1.sort() srtdlst2= list2.sort() if srtdlst1 == srtdlst2: return True else: return False isAnagram("anagram", "nagaram")
22nd May 2023, 6:37 AM
Paul-Henry Paltmann
Paul-Henry Paltmann - avatar
0
Tibor Santa the code still outputs nothing https://www.sololearn.com/compiler-playground/cAiiQG5SSIPj
22nd May 2023, 7:00 AM
Paul-Henry Paltmann
Paul-Henry Paltmann - avatar
0
wtf
22nd May 2023, 7:04 AM
Paul-Henry Paltmann
Paul-Henry Paltmann - avatar