관리 메뉴

솜씨좋은장씨

[BaekJoon] 25801번 : Odd/Even Strings (Python) 본문

Programming/코딩 1일 1문제

[BaekJoon] 25801번 : Odd/Even Strings (Python)

솜씨좋은장씨 2022. 12. 13. 12:13
728x90
반응형

코딩 1일 1문제! 오늘의 문제는 백준의 Odd/Even Strings 입니다.

 

25801번: Odd/Even Strings

Consider a string containing lowercase letters only. We call the string odd if every letter in the string appears an odd number of times. Similarly, we call the string even if every letter in the string appears an even number of times. Given a string, dete

www.acmicpc.net

👨🏻‍💻 문제 풀이

collections 의 Counter를 활용하여 문자열 속 각 문자들의 빈도수를 구한 다음

from collections import Counter

even_check, odd_check = 0, 0
    
cnt_items = Counter(string).items()
    
for item in cnt_items:
    if item[1] % 2 == 0:
        even_check += 1
    elif item[1] % 2 == 1:
        odd_check += 1

모든 빈도수가 짝수인지 홀수인지 확인할때마다 개수를 카운팅 했습니다.

answer = "0/1"

if even_check > 0 and odd_check == 0:
    answer = "0"
elif even_check == 0 and odd_check > 0:
    answer = "1"

짝수 빈도수 홀수 빈도수 모두 존재할 경우에는 답을 0/1

짝수 빈도수만 존재할 경우에는 답을 0

홀수 빈도수만 존재할 경우에는 답을 1 로 출력하면 됩니다.

전체 코드는 아래를 참고해주세요.

👨🏻‍💻 코드 ( Solution )

from collections import Counter

def odd_even_strings(string):
    even_check, odd_check = 0, 0
    
    answer = "0/1"
    
    cnt_items = Counter(string).items()
    
    for item in cnt_items:
        if item[1] % 2 == 0:
            even_check += 1
        elif item[1] % 2 == 1:
            odd_check += 1
    
    if even_check > 0 and odd_check == 0:
        answer = "0"
    elif even_check == 0 and odd_check > 0:
        answer = "1"
        
    return answer
        

if __name__ == "__main__":
    string = input()
    print(odd_even_strings(string=string))
 

GitHub - SOMJANG/CODINGTEST_PRACTICE: 1일 1문제 since 2020.02.07

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments