Algorithm/백준문제

[Baekjoon] 2941번 크로아티아 알파벳

bestwish 2022. 4. 12. 19:03

문제

 

 

Python3 답

words = input()
croatia = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z=']


for i in croatia:
    words = words.replace(i, '*')

print(len(words))

 

 

코드 풀이

2글자가 합쳐진 크로아티아 코드는 1글자로 취급한다고 하여서, replace를 이용하기로 하였다.
replace를 이용하여 글자를 변환한 뒤, 마지막에 문자열의 길이를 출력해주면 값이 나온다.

 

 

replace

  • replace는 문자열을 특정 문자로 변경하는 함수이다.
  • 변수명.replace(old, new, [count])형식으로 사용한다.
  • count의 기본값은 전체를 의미하는 -1로 지정되어 있다.
a = 'test'
a = a.replace('t', 'b')

print(a)

# 결과
besb

count를 이용할시

a = 'test'
a = a.replace('t', 'b', 1)

print(a)

# 결과
best