[Swift]LeetCode242. 有效的字母異位詞 | Valid Anagram

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

Given two strings s and , write a function to determine if t is an anagram of s.git

Example 1:github

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:微信

Input: s = "rat", t = "car"
Output: false

Note:
You may assume the string contains only lowercase alphabets.函數

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?spa

給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是不是 s 的一個字母異位詞。code

示例 1:htm

輸入: s = "anagram", t = "nagaram"
輸出: true

示例 2:blog

輸入: s = "rat", t = "car"
輸出: false

說明:
你能夠假設字符串只包含小寫字母。ci

進階:
若是輸入字符串包含 unicode 字符怎麼辦?你可否調整你的解法來應對這種狀況?


 

32ms

 1 class Solution {
 2     func isAnagram(_ s: String, _ t: String) -> Bool {
 3         
 4       let chars_S = s.unicodeScalars
 5       var counter_S = Array(repeating: 0, count: 26)
 6       let chars_T = t.unicodeScalars
 7       var counter_T = Array(repeating: 0, count: 26)
 8 
 9       for char in chars_S {
10         let index = Int(char.value - 97)
11         counter_S[index] += 1
12       }
13 
14       for char in chars_T {
15         let index = Int(char.value - 97)
16         counter_T[index] += 1
17       }
18       return counter_T == counter_S
19     }
20 }

32ms

 1 class Solution {
 2     func isAnagram(_ s: String, _ t: String) -> Bool {
 3         guard s.count == t.count else {
 4             return false
 5         }
 6         var occurances = [Int](repeating: 0, count: 26)
 7         let aValue: UInt8 = 97
 8         for char in s.utf8 {
 9             occurances[Int(char - aValue)] += 1
10         }
11         for char in t.utf8 {
12             occurances[Int(char - aValue)] -= 1
13         }
14         for value in occurances {
15             if value != 0 {
16                 return false
17             }
18         }
19         return true
20     }
21 }

48ms

1 class Solution {
2     func isAnagram(_ s: String, _ t: String) -> Bool {
3         return t.unicodeScalars.reduce(into: [:]) { $0[$1, default: 0] += 1 } == s.unicodeScalars.reduce(into: [:]) { $0[$1, default: 0] += 1 }
4     }
5 }

64ms

 1 extension Character {
 2     
 3     var ascii: Int {
 4         return Int(unicodeScalars.first!.value)
 5     }
 6     
 7 }
 8 
 9 class Solution {
10     func isAnagram(_ s: String, _ t: String) -> Bool {
11         var table = [Int](repeating: 0, count: 128)
12 
13         for char in s {
14             table[char.ascii] += 1
15         }
16 
17         for char in t {
18             table[char.ascii] -= 1
19         }
20 
21         for ascii in 97...122 {
22             if table[ascii] != 0 {
23                 return false
24             }
25         }
26 
27         return true
28     }
29 }
相關文章
相關標籤/搜索