Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
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
Refactored the code to reduce time complexity
The time complexity now is O(n), which was O(nlogn) with the earlier method.
  • Loading branch information
SwayamSahu authored Oct 31, 2022
commit 20577bcdd86d79e45f4a9d594de9f7fe8467c489
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)