Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 601 Bytes

PatternMatchSolution.md

File metadata and controls

22 lines (20 loc) · 601 Bytes

Pattern Match Solution

Swift

func match(pattern: String, str: String) -> Bool {
    let words = str.components(separatedBy: " ")
    let patternChars = pattern.map{$0}
    var dict = [Character: String]()
    guard patternChars.count == words.count else { return false }
    for index in 0..<words.count {
        let patternChar = patternChars[index]
        let word = words[index]
        if let patternWord = dict[patternChar] {
            if word != patternWord { return false }
        } else {
            dict[patternChar] = word
        }        
    }
    return true
}