在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a Here follow means a full match, such that there is a bijection between a letter in Example 1: Input: pattern = Example 2: Input:pattern = Example 3: Input: pattern = Example 4: Input: pattern = Notes: 给定一种 这里的遵循指完全匹配,例如, 示例1: 输入: pattern = 示例 2: 输入:pattern = 示例 3: 输入: pattern = 示例 4: 输入: pattern = 说明: 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 }
|
请发表评论