★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-auaimbda-kw.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a pattern
and a string str
, find if str
follows the same pattern.git
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in str
.github
Example 1:微信
Input: pattern = , str = Output: true"abba""dog cat cat dog"
Example 2:app
Input:pattern = , str = Output: false"abba""dog cat cat fish"
Example 3:spa
Input: pattern = , str = Output: false"aaaa""dog cat cat dog"
Example 4:code
Input: pattern = , str = Output: false"abba""dog dog dog dog"
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.component
給定一種 pattern(模式)
和一個字符串 str
,判斷 str
是否遵循相同的模式。htm
這裏的遵循指徹底匹配,例如, pattern
裏的每一個字母和字符串 str
中的每一個非空單詞之間存在着雙向鏈接的對應模式。blog
示例1:
輸入: pattern = , str = 輸出: true"abba""dog cat cat dog"
示例 2:
輸入:pattern = , str = 輸出: false"abba""dog cat cat fish"
示例 3:
輸入: pattern = , str = 輸出: false"aaaa""dog cat cat dog"
示例 4:
輸入: pattern = , str = 輸出: false"abba""dog dog dog dog"
說明:
你能夠假設 pattern
只包含小寫字母, str
包含了由單個空格分隔的小寫字母。
12ms
1 class Solution { 2 func wordPattern(_ pattern: String, _ str: String) -> Bool { 3 var strArr = str.components(separatedBy: " "); 4 if pattern.count != strArr.count { 5 return false 6 } 7 var keyValueDic = Dictionary<Character, String>() 8 var keyValueDic2 = Dictionary<String, Character>() 9 for (index,key) in pattern.enumerated() { 10 let tempStr = keyValueDic[key] 11 if tempStr == nil { 12 if keyValueDic2[strArr[index]] != nil { 13 return false; 14 } 15 keyValueDic[key] = strArr[index] 16 keyValueDic2[strArr[index]] = key 17 } 18 else { 19 if tempStr == strArr[index] && keyValueDic2[tempStr!] == key { 20 continue 21 } 22 else { 23 return false 24 } 25 } 26 } 27 return true 28 } 29 }
12ms
1 class Solution { 2 func wordPattern(_ pattern: String, _ str: String) -> Bool { 3 4 let patternArr = Array(pattern) 5 let strArr = str.split(separator: " ").map{String($0)} 6 7 var patternDict = [Character:Int]() 8 var strDict = [String:Int]() 9 10 var tempP = 0 11 var tempS = 0 12 13 for i in patternArr { 14 if patternDict[i] == nil { 15 patternDict[i] = tempP 16 tempP += 1 17 } 18 } 19 20 for i in strArr { 21 if strDict[i] == nil { 22 strDict[i] = tempS 23 tempS += 1 24 } 25 } 26 27 var resultPattern = [Int]() 28 var resultStr = [Int]() 29 30 for i in patternArr { 31 resultPattern.append(patternDict[i]!) 32 } 33 34 for i in strArr { 35 resultStr.append(strDict[i]!) 36 } 37 38 print(resultPattern) 39 print(resultStr) 40 print(resultPattern == resultStr) 41 return resultPattern == resultStr 42 } 43 }
16ms
1 class Solution { 2 func wordPattern(_ pattern: String, _ str: String) -> Bool { 3 let wordArr = str.components(separatedBy: " ") 4 if pattern.count != wordArr.count { 5 return false 6 } 7 8 var patternMap: [String: String] = [String: String]() 9 10 for (n,x) in pattern.enumerated() { 11 let strX = String(x) 12 if patternMap[strX] == nil {//沒有strx這個key 13 let word = wordArr[n] 14 let flag = patternMap.values.contains(word) 15 if !flag { 16 patternMap[strX] = word 17 }else { 18 return false 19 } 20 }else { 21 if patternMap[strX] != wordArr[n] { 22 return false 23 } 24 } 25 } 26 return true 27 } 28 }
20ms
1 class Solution { 2 func wordPattern(_ pattern: String, _ str: String) -> Bool { 3 let strArr = str.split(separator: " ") 4 var dic = [Character: String]() 5 if strArr.count != pattern.characters.count || Set(strArr).count != Set(pattern.characters).count{ 6 return false 7 } 8 9 for (i, v) in pattern.characters.enumerated(){ 10 if dic[v] == nil{ 11 dic[v] = String(strArr[i]) 12 }else{ 13 if dic[v] != String(strArr[i]){ 14 return false 15 } 16 } 17 } 18 return true 19 } 20 }