MUKER_DEV with iOS

[swift] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - μ˜Ήμ•Œμ΄ (2) λ³Έλ¬Έ

πŸ€– μ•Œκ³ λ¦¬μ¦˜/programmers

[swift] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - μ˜Ήμ•Œμ΄ (2)

MUKER 2023. 2. 13. 13:15
 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

programmers.co.kr

문제 ν‘ΈλŠ” 데 μžˆμ–΄ 도움이 λ˜λ„λ‘ λ‚˜μ˜ 풀이와 κ°œμ„ λœ 풀이λ₯Ό μ˜¬λ¦½λ‹ˆλ‹€.
λ˜ν•œ 풀이 ν›„ λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이λ₯Ό 보고 μ°Έκ³ ν• λ§Œν•œ 풀이도 μ˜¬λ¦½λ‹ˆλ‹€.

- λ¬Έμ œμ— 따라 λ‚˜μ˜ ν’€μ΄λ§Œ μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.
- ν•΄λ‹Ή 풀이듀은 풀이 쀑 ν•˜λ‚˜μΌ 뿐 μ΅œμ„ μ˜ ν’€μ΄λŠ” 아닐 수 μžˆμŠ΅λ‹ˆλ‹€.

 


 

문제 μ„€λͺ…

 

- μ˜Ήμ•Œμ΄μ˜ μ’…λ₯˜λŠ” "aya", "ye", "woo", "ma" μž…λ‹ˆλ‹€.

 

- μ •ν™•ν•œ μ˜Ήμ•Œμ΄λ‘œλ§Œ λ°œμŒν•œ λ¬Έμžμ—΄μ˜ 개수λ₯Ό κ΅¬ν•΄μ•Όν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

 

- μ—°μ†ν•΄μ„œ 같은 λ°œμŒμ„ ν•˜λŠ”κ±΄ μ •ν™•ν•œ μ˜Ήμ•Œμ΄μΌμ§€λΌλ„ 개수둜 μΉ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

 


 

λ‚˜μ˜ 풀이

import Foundation

func solution(_ babbling:[String]) -> Int {
    var correct = Array(repeating: "", count: babbling.count) // λΉˆλ°°μ—΄ μ΄ˆκΈ°ν™”
    
    for (index,i) in babbling.enumerated() {
        var temp = ""
        var before = ""

        for j in i {
            temp.append(j) // 문자 ν•˜λ‚˜μ”© temp에 λ„£μ–΄μ£ΌκΈ°
            
            if temp ==  "aya" || temp == "ye" || temp == "woo" || temp == "ma" { // μ˜Ήμ•Œμ΄κ°€ μ™„μ„±λ˜λ©΄ tempκ°’ correct에 λ„£μ–΄μ£ΌκΈ°
                if before == temp { correct[index] = ""; continue } // 전에 λ¬Έμžμ™€ κ°™λ‹€λ©΄ ""둜 λ§Œλ“€μ–΄λ²„λ¦¬κΈ°
                correct[index] += temp
                before = temp
                temp = ""
            }
        }
    }
    return correct.enumerated().filter { $0.element == babbling[$0.offset] }.count // correctκ°’κ³Ό babbling값이 같은 κ²ƒλ§Œ return
}

 


 

μ°Έκ³ ν• λ§Œν•œ 풀이

import Foundation

func solution(_ babbling:[String]) -> Int {
    var count: Int = 0
    for element in babbling {
        var str = String(element)
        str = str.replacingOccurrences(of: "aya", with: "1")
        str = str.replacingOccurrences(of: "ye", with: "2")
        str = str.replacingOccurrences(of: "woo", with: "3")
        str = str.replacingOccurrences(of: "ma", with: "4")
        if Int(str) != nil && !str.contains("11") && !str.contains("22") && !str.contains("33") && !str.contains("44"){
            count += 1
        }
    }    
    return count
}

 

func solution(_ babbling: [String]) -> Int {
    return babbling.filter { $0.range(of: "^(aya(?!aya)|ye(?!ye)|woo(?!woo)|ma(?!ma))+$", options: .regularExpression) != nil }.count
}