SWEA

[python][SWEA][D3] 11856. 반반

haju 2023. 5. 15. 16:05

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&problemLevel=3&contestProbId=AXjS1GXqZ8gDFATi&categoryId=AXjS1GXqZ8gDFATi&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=PYTHON&select-1=3&pageSize=10&pageIndex=6 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

✅ 문제 풀이

  • 반복문을 돌면서 각 글자의 갯수를 세어서, 2가 아닐경우는 1개나 3개, 4개일 경우이므로 break
  • 개수가 2개일 경우 Yes! 이 때, 2개의 각 글자가 정확히 두 번 등장하는 지 판별할 필요는 없다.
T=int(input())

for test_case in range(1,T+1):
    flag=0
    s=list(input())
    for i in s:
        if s.count(i)!=2:
            flag=1
            break
    if flag==0:
        print("#%d Yes" % (test_case))
    else:
        print("#%d No" %(test_case))

 

✅ short code

  • 생각해보니 이렇게 풀면 더 빨리 가능하다!
  • sort()를 이용해 정렬하면 첫 번째 문자 == 두 번째 문자, 세 번째 문자 == 네 번째 문자 일 경우 Yes!
  • ㄴ그런데 4글자 모두 같은 경우를 생각해줘야 하므로, 두 번째 문자 != 세 번째 문자 조건 필요함
T=int(input())

for test_case in range(1,T+1):
    flag=0
    s=list(input())
    s.sort()
    if s[0]==s[1] and s[2]==s[3] and s[1]!=s[2]:
        print("#%d Yes" % (test_case))
    else:
        print("#%d No" %(test_case))