在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You have a list of A word matches the pattern if there exists a permutation of letters (Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.) Return a list of the words in You may return the answer in any order. Example 1: Input: words = Note:
你有一个单词列表 如果存在字母的排列 (回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。) 返回 你可以按任何顺序返回答案。 示例: 输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" 输出:["mee","aqq"] 解释: "mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。 "ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。 因为 a 和 b 映射到同一个字母。 提示:
16ms
1 class Solution { 2 func findAndReplacePattern(_ words: [String], _ pattern: String) -> [String] { 3 var output = [String]() 4 for word in words { 5 if checkPattern(word,pattern) { 6 output.append(word) 7 } 8 } 9 return output 10 } 11 func checkPattern(_ word: String, _ pattern: String) -> Bool { 12 if word.count != pattern.count { 13 return false 14 } 15 var myDict : Dictionary<Character,Character> = Dictionary.init() 16 var secondDict : Dictionary<Character,Character> = Dictionary.init() 17 var wordArray = Array(word) 18 let patternArray = Array(pattern) 19 for i in 0..<word.count { 20 if myDict[wordArray[i]] == nil { 21 if secondDict[patternArray[i]] != nil { 22 continue 23 } 24 myDict[wordArray[i]] = patternArray[i] 25 secondDict[patternArray[i]] = wordArray[i] 26 wordArray[i] = patternArray[i] 27 } else { 28 wordArray[i] = myDict[wordArray[i]]! 29 } 30 } 31 if wordArray != patternArray { 32 return false 33 } 34 return true 35 } 36 } 16ms 1 class Solution { 2 func rootArray(of string:String) -> [Int] { 3 var rootDic : [UInt32:Int] = [:] 4 var rootArray : [Int] = [] 5 6 var idx : Int = 0 7 for p in string.unicodeScalars { 8 if let pRoot = rootDic[p.value] { 9 rootArray.append(pRoot) 10 }else{ 11 rootArray.append(idx) 12 rootDic[p.value] = idx 13 } 14 idx += 1 15 } 16 return rootArray 17 } 18 19 func findAndReplacePattern(_ words: [String], _ pattern: String) -> [String] { 20 let patternRootArray = rootArray(of: pattern) 21 22 var result : [String] = [] 23 for word in words { 24 if rootArray(of: word) == patternRootArray { 25 result.append(word) 26 } 27 } 28 29 return result 30 } 31 } 20ms 1 class Solution { 2 func findAndReplacePattern(_ words: [String], _ pattern: String) -> [String] { 3 return words.filter({ word($0, matches: pattern)}) 4 } 5 6 func word(_ word: String, matches pattern: String) -> Bool { 7 if word.count != pattern.count { 8 return false 9 } 10 11 var letterMapping = [Character: Character]() 12 var wordIndex = word.startIndex 13 var patternIndex = pattern.startIndex 14 15 while wordIndex != word.endIndex && patternIndex != pattern.endIndex { 16 let currentChar = word[wordIndex] 17 let patternChar = pattern[patternIndex] 18 19 if !letterMapping.keys.contains(currentChar) { 20 if letterMapping.values.contains(patternChar) { 21 return false 22 } 23 24 letterMapping[currentChar] = patternChar 25 } else if letterMapping[currentChar] != patternChar { 26 return false 27 } 28 29 wordIndex = word.index(after: wordIndex) 30 patternIndex = pattern.index(after: patternIndex) 31 } 32 33 return true 34 } 35 } Runtime: 24 ms
Memory Usage: 19.7 MB
1 class Solution { 2 func findAndReplacePattern(_ words: [String], _ pattern: String) -> [String] { 3 var res:[String] = [String]() 4 for w in words 5 { 6 if F(w) == F(pattern) 7 { 8 res.append(w) 9 } 10 } 11 return res 12 } 13 14 func F(_ w:String) -> String 15 { 16 var arrW:[Character] = Array(w) 17 var m:[Character:Int] = [Character:Int]() 18 for c in w 19 { 20 if m[c] == nil 21 { 22 m[c] = m.count 23 } 24 } 25 for i in 0..<w.count 26 { 27 arrW[i] = (97 + m[arrW[i],default:0]).ASCII 28 } 29 return String(arrW) 30 } 31 } 32 33 //Int扩展 34 extension Int 35 { 36 //Int转Character,ASCII值(定义大写为字符值) 37 var ASCII:Character 38 { 39 get {return Character(UnicodeScalar(self)!)} 40 } 41 }
|
请发表评论