[Swift]LeetCode890. 查找和替換模式 | Find and Replace Pattern

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-qsnqotrv-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You have a list of words and a pattern, and you want to know which words in words matches the pattern.git

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.github

(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. app

You may return the answer in any order.spa

Example 1:code

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:htm

  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

你有一個單詞列表 words 和一個模式  pattern,你想知道 words 中的哪些單詞與模式匹配。blog

若是存在字母的排列 p ,使得將模式中的每一個字母 x 替換爲 p(x) 以後,咱們就獲得了所需的單詞,那麼單詞與模式是匹配的。ci

(回想一下,字母的排列是從字母到字母的雙射:每一個字母映射到另外一個字母,沒有兩個字母映射到同一個字母。)

返回 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 }
相關文章
相關標籤/搜索