Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 919 Bytes

290. Word Pattern.md

File metadata and controls

42 lines (33 loc) · 919 Bytes

Leetcode

290. Word Pattern

문제링크

코드

class Solution {
    func wordPattern(_ pattern: String, _ s: String) -> Bool {
        var arr: [Int: String] = [:]
        let arrPattern = pattern.map { $0 }
        let arrS = s.split(separator: " ")
        if pattern.count != arrS.count {
            return false
        }
        for (i, c) in arrPattern.enumerated() {
            let count: Int = Int(c.asciiValue! - Character("a").asciiValue!)
            if !arr.contains(where: { $0.0 == count }) &&
            !arr.contains(where: { $0.1 == arrS[i] })
            {
                arr[count] = String(arrS[i])
            } else {
                if arr[count] != String(arrS[i]) {
                    return false
                }
            }
        }
        return true
    }
}

풀이 이유

참고한 내용