Swift 코딩테스트/Swift 백준 문제 풀이

[Swift 코테] 백준 2941 크로아티아 알파벳

ㄱ ㅅ ㄱ 2022. 3. 17. 14:37
728x90

하루 지나서 푸니까 생각보다 너무 간단했던 문제였다. 복잡하게 문자열로 바꿔서 풀지 말고 String 함수를 많이 알아야 겠다고 느낀 문제였다.

 

아래 contains와 replaceOccurrences함수가 메인이다.

line.contains("지금 이거를")
line = line.replaceOccurrences(of: "지금 이거를", with: "이걸로 바꾸자")

1. 이전 운영체제 버전의 크로아티아 알파벳의 유무를 String 함수인 contains로 확인하고

2. 있다면 다른 Character 하나로 변경한다 ex) "0" - 숫자는 입력되지 않기 때문

3. 모든 String을 살펴봐야 하기 때문에 else if 문이 아닌 if문만 사용

4. 바뀐 String을 Array로 변경하고, Array의 count를 출력하면 답!

ljes=njak  -> lj/e/š/nj/a/k = 0e00ak

변경된 크로아티아 알파벳 3개 + 알파벳 3개 = 6개

 

코드

import Foundation

var count = 0
var line = readLine()!

if line.contains("c="){
    line = line.replacingOccurrences(of: "c=", with: "0")
    count += 1
}
if line.contains("c-"){
    line = line.replacingOccurrences(of: "c-", with: "0")
}
if line.contains("dz="){
    line = line.replacingOccurrences(of: "dz=", with: "0")
}
if line.contains("d-"){
    line = line.replacingOccurrences(of: "d-", with: "0")
}
if line.contains("lj"){
    line = line.replacingOccurrences(of: "lj", with: "0")
}
if line.contains("nj"){
    line = line.replacingOccurrences(of: "nj", with: "0")
}
if line.contains("s="){
    line = line.replacingOccurrences(of: "s=", with: "0")
}
if line.contains("z="){
    line = line.replacingOccurrences(of: "z=", with: "0")
}

print(Array(line).count)

 

 

728x90