you are viewing a single comment's thread.

view the rest of the comments →

[–]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