Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the code to reduce time complexity #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions algorithms/strings/anagram_check.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# function to check if two strings are
# anagram or not
def check(s1, s2):

# the sorted strings are checked
if(sorted(s1)== sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")

# driver code
s1 ="listen" #String 1
s2 ="silent" #String 2
check(s1, s2)
from collections import Counter

# function to check if two strings are
# anagram or not
def check(s1, s2):

# implementing counter function
if(Counter(s1) == Counter(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")


# driver code
s1 = "listen"
s2 = "silent"
check(s1, s2)