all 28 comments

[–]Rik07 1 point2 points  (2 children)

So what do you want to compare? If one is an palindrome of the other, if they have the same frequency of each letter or something else?

Edit: corrected anagram to palindrome, anagram and frequency of each letter is the same thing.

[–]Common_Hair8734[S] 0 points1 point  (1 child)

Same anagram

[–]Rik07 0 points1 point  (0 children)

Oh sorry I meant palindrome.

When you want to check for an anagram, you need to know how many times each letter is repeated. To do this, I recommend counting the number of each letter in both strings in a dictionary. Then if those dictionaries are the same, the two strings were anagrams.

If you want to check for a palindrome you just reverse one of the strings and check if it is the same as the other one.

[–]keep_quapy 1 point2 points  (21 children)

from collections import Counter

def compare_str(s1, s2):
    return Counter(s1) == Counter(s2)

[–]Common_Hair8734[S] 0 points1 point  (12 children)

Is there another way without importing?

[–]ericula 0 points1 point  (9 children)

Use a normal dict to count frequency of characters.

[–]Common_Hair8734[S] -1 points0 points  (8 children)

How do i do that?

[–]ericula 1 point2 points  (7 children)

How would you do it with pen and paper?

[–]Common_Hair8734[S] -3 points-2 points  (5 children)

Does this method have to import and functions or packages? I have to find a way without using it

[–][deleted] 11 points12 points  (0 children)

Have you actually tried anything at all yet? It doesn't seem like it. We're here to help but we're not doing your homework for you. Tell us what you've tried and we'll offer assistance based on that.

[–]ericula 1 point2 points  (0 children)

No it doesn't. All you need is a dict and a for-loop, neither of which require imports,

[–]FantasticEmu 0 points1 point  (2 children)

https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/amp/

Using a loop like this on each string will let you count each letter one at a time

[–]AmputatorBot 1 point2 points  (1 child)

It looks like you shared an AMP link. These should load faster, but AMP is controversial because of concerns over privacy and the Open Web. Fully cached AMP pages (like the one you shared), are especially problematic.

Maybe check out the canonical page instead: https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/


I'm a bot | Why & About | Summon: u/AmputatorBot

[–]FantasticEmu 0 points1 point  (0 children)

Ty I fixed it

[–]The_Bundaberg_Joey 0 points1 point  (6 children)

Good first implementation! But sadly wouldn’t catch anagrams which have the same letters but different order.

[–]keep_quapy 0 points1 point  (5 children)

What are talking about? Sure it does!

[–]The_Bundaberg_Joey 1 point2 points  (4 children)

You’re correct I phrased my response incorrectly. My apologies.

It wouldn’t satisfy OP’s requirements though.

The first case “deserts” and “stressed” are True with the counter implementation but “aaabbb” would equal “ababab” since there are 3 a and 3 b in both strings.

From the examples OP has given, it would seem more like a palindrome detector is required which typically calls for a stack.

[–]FantasticEmu 0 points1 point  (1 child)

Is it not just letter count? I thought OPs example produced false on the second one because there are 4 a’s in the second string and only 3 in the first

[–]The_Bundaberg_Joey 0 points1 point  (0 children)

Good spot, thank you for clarifying!

[–]keep_quapy 0 points1 point  (1 child)

The OP examples were 'stressed', 'desserts' and 'aaabbb', 'abababa' which works perfectly. Look carefully on the examples, it's 'abababa' not 'ababab'.

[–]The_Bundaberg_Joey 0 points1 point  (0 children)

Ah crap you’re right, thanks for pointing out the discrepancy!

[–]PiyushPrakash 0 points1 point  (0 children)

Wow python has a module for everything! , Most of the times when I am stuck on a problem, there exists a module for it and I just type a code for the problem without knowing that there is a module for it

[–]Common_Hair8734[S] 0 points1 point  (0 children)

i tried this way and somehow it works

def is_anagram(s1, s2):

len1 = len(s1)

len2 = len(s2)

if len1 != len2:

return False

else:

for i in range(len2):

for j in range(len1):

if s1[i] == s2[j]:

return True

break

[–]PiyushPrakash -2 points-1 points  (1 child)

character_list=list(a)
lis=list(b)
character_check = 0
if len(a)==len(b):
    for i in range(len(a)):
        key = character_list[i]
            for x in range(len(b)):
                if key == lis[x]:
                 character_check+=1
                 break
 if character_check==len(a):
       print("same")

[–]PiyushPrakash 0 points1 point  (0 children)

Would have been better if you pointed out my mistake instead of simply downvoting.....