일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- PYTHON
- 백준
- Docker
- 더현대서울 맛집
- hackerrank
- programmers
- leetcode
- AI 경진대회
- 캐치카페
- 자연어처리
- 코로나19
- 프로그래머스 파이썬
- github
- 맥북
- 편스토랑 우승상품
- ubuntu
- 프로그래머스
- 금융문자분석경진대회
- dacon
- SW Expert Academy
- 편스토랑
- 파이썬
- 우분투
- Baekjoon
- Real or Not? NLP with Disaster Tweets
- Kaggle
- Git
- ChatGPT
- gs25
- 데이콘
- Today
- Total
솜씨좋은장씨
[BaekJoon] 25801번 : Odd/Even Strings (Python) 본문

코딩 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
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaekJoon] 26307번 : Correct (Python) (0) | 2022.12.15 |
---|---|
[BaekJoon] 2033번 : 반올림 (Python) (0) | 2022.12.14 |
[BaekJoon] 24937번 : SciComLove (2022) (Python) (0) | 2022.12.12 |
[Programmers] 가장 가까운 같은 글자 (Python) (0) | 2022.12.11 |
[BaekJoon] 26004번 : HI-ARC (Python) (0) | 2022.12.10 |