You have a list of words
and a pattern
, and you want to know which words in words
matches the pattern.
A word matches the pattern if there exists a permutation of letters p
so that after replacing every letter x
in the pattern with p(x)
, we get the desired word.
(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 words
that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" Output: ["mee","aqq"] Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
你有一个单词列表
words
和一个模式 pattern
,你想知道 words
中的哪些单词与模式匹配。
如果存在字母的排列 p
,使得将模式中的每个字母 x
替换为 p(x)
之后,我们就得到了所需的单词,那么单词与模式是匹配的。
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
返回 words
中与给定模式匹配的单词列表。
你可以按任何顺序返回答案。
示例:
输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" 输出:["mee","aqq"] 解释: "mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。 "ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。 因为 a 和 b 映射到同一个字母。
提示:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
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 }
原文地址:https://www.cnblogs.com/strengthen/p/10605199.html