practivceAlgorithm/다시 봐야할 문제들

[백준][Python] 1622 공통순열

findTheValue 2021. 10. 6. 17:23

아직 EOF 다루는게 익숙하지 않다. 예외처리에 대해 공부해야 할 듯.

 

from collections import defaultdict

while True:
    try:
        a=input()
        b=input()
        alpha1=defaultdict(int)
        alpha2=defaultdict(int)
        ans=''
        for s in a:
            alpha1[s]+=1
        for s in b:
            alpha2[s]+=1
        s = []
        for char in alpha1:
            if char in alpha2:
                s.append(char)
        s.sort()
        for char in s:
            ans += char*min(alpha1[char],alpha2[char])
        print(ans)
    except:
        break