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

[Swift]LeetCode242. 有效的字母异位词 | Valid Anagram

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

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

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

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

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

Example 1:

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?

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。

示例 1:

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

示例 2:

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

说明:
你可以假设字符串只包含小写字母。

进阶:
如果输入字符串包含 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 }

 


 

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