Skip to content

[Algorithm] 옹알이(2) #68

Closed
Closed
@hwangJi-dev

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 옹알이 (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

  • 문자열 치환

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions