Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 옹알이(2) #68

Closed
hwangJi-dev opened this issue Jan 12, 2023 · 0 comments
Closed

[Algorithm] 옹알이(2) #68

hwangJi-dev opened this issue Jan 12, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 12, 2023

💬 문제

[코딩테스트 연습 - 옹알이 (2)](https://school.programmers.co.kr/learn/courses/30/lessons/133499)


💬 Idea

  • 옹알이를 각각 1, 2, 3, 4로 변환해준다.
    • 이후 형변환을 했을 때 nil이 나온다면 옹알이 이외의 말이 포함되어 있는 것이기 때문이다.
  • 그리고 연속해서 같은 발음을 하는 것을 불편해하므로 연속되는 경우를 제외했을 때 모든 규칙이 성립한다면 result에 1을 더해주었다.

💬 풀이

func solution(babbling:[String]) -> Int {
    let babble: [String: String] = ["aya": "1", "ye": "2", "woo": "3", "ma": "4"]
    var result = 0
    
    for b in babbling {
        var rep = b
        babble.forEach {
            rep = rep.replacingOccurrences(of: $0.key, with: $0.value)
        }
        
        if Int(rep) != nil && !rep.contains("11") && !rep.contains("22") && !rep.contains("33") && !rep.contains("44") {
            result += 1
        }
    }
    
    return result
}

💬 알게된 문법

✅ replacingOccurrences

  • 문자열 치환
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant