forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordPattern.swift
34 lines (29 loc) · 1.07 KB
/
WordPattern.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Question Link: https://leetcode.com/problems/word-pattern/
* Primary idea: Use two dictionarys to determine if a character is unique to a word
* Time Complexity: O(n), Space Complexity: O(n)
*/
class WordPattern {
func wordPattern(pattern: String, _ str: String) -> Bool {
var wordDict = [String: Character]()
var charDict = [Character: String]()
let strs = str.characters.split{$0 == " "}.map(String.init)
let patterns = [Character](pattern.characters)
guard patterns.count == strs.count else {
return false
}
for i in 0 ..< strs.count {
let currentWord = strs[i]
let currentChar = patterns[i]
if wordDict[currentWord] == nil && charDict[currentChar] == nil{
wordDict[currentWord] = currentChar
charDict[currentChar] = currentWord
} else {
if wordDict[currentWord] != currentChar {
return false
}
}
}
return true
}
}