为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode290. 单词模式 | Word Pattern

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

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

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

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

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Example 1:

Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:

Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:

Input: pattern = "abba", str = "dog dog dog dog"
Output: false

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.


 给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。

这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。

示例1:

输入: pattern = "abba", str = "dog cat cat dog"
输出: true

示例 2:

输入:pattern = "abba", str = "dog cat cat fish"
输出: false

示例 3:

输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false

示例 4:

输入: pattern = "abba", str = "dog dog dog dog"
输出: false

说明:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。    


 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 }

 

posted @ 2018-10-08 20:30  为敢技术  阅读(285)  评论(0编辑  收藏  举报