• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode890.查找和替换模式|FindandReplacePattern

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10605199.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 = 

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift 学习- 11 -- 属性发布时间:2022-07-13
下一篇:
Swift-按钮(UIButton)的用法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap