생성일: 2022년 1월 29일 오후 5:52
# 아나그램(구글)
import sys
sys.stdin = open("input.txt", "rt")
first = input()
second = input()
firstDic = {}
secondDic = {}
for x in first:
if x in firstDic:
firstDic[x] += 1
else:
firstDic[x] = 1
for x in second:
if x in secondDic:
secondDic[x] += 1
else:
secondDic[x] = 1
if firstDic == secondDic:
print("YES")
else:
print("NO")
import sys
sys.stdin=open("input.txt", "r")
a=input()
b=input()
str1=dict()
str2=dict()
for x in a:
str1[x]=str1.get(x, 0)+1
for x in b:
str2[x]=str2.get(x, 0)+1
for i in str1.keys():
if i in str2.keys():
if str1[i]!=str2[i]:
print("NO")
break
else:
print("NO")
break
else:
print("YES")
<개선된 코드>
import sys
#sys.stdin=open("in1.txt", "r")
a=input()
b=input()
sH=dict()
for x in a:
sH[x]=sH.get(x, 0)+1
for x in b:
sH[x]=sH.get(x, 0)-1
for x in a:
if(sH.get(x)>0):
print("NO")
break;
else:
print("YES")
if x in firstDic:
firstDic[x] += 1
else:
firstDic[x] = 1
str1[x]=str1.get(x, 0)+1
위의 두 코드는 같은 역할을 한다. (get()을 사용하여 1줄로 코드를 줄일 수 있다.)