Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Last active July 13, 2020 08:40
Show Gist options
  • Save Ch-sriram/90838b4eacc9a1d9ea659fdac612f115 to your computer and use it in GitHub Desktop.
Save Ch-sriram/90838b4eacc9a1d9ea659fdac612f115 to your computer and use it in GitHub Desktop.
Check whether two strings are anagrams of each other
# Problem Link: https://practice.geeksforgeeks.org/problems/anagram/0
def is_anagram(A, B): # A: str; B: str;
if len(A) != len(B): return False
cnt = [0 for _ in range(26)]
for i in range(len(A)):
cnt[ord('a') - ord(A[i])] += 1
cnt[ord('a') - ord(B[i])] -= 1
for i in range(len(cnt)):
if cnt[i] != 0:
return False
return True
if __name__ == "__main__":
t = int(input().strip())
while t > 0:
t -= 1
a_str, b_str = list(input().strip().split(' '))
print("YES" if is_anagram(a_str, b_str) is True else "NO")
@Ch-sriram
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment