Last active
July 13, 2020 08:40
-
-
Save Ch-sriram/90838b4eacc9a1d9ea659fdac612f115 to your computer and use it in GitHub Desktop.
Check whether two strings are anagrams of each other
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem Link