zoukankan      html  css  js  c++  java
  • [Swift]LeetCode242. 有效的字母异位词 | Valid Anagram

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

    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 }

  • 相关阅读:
    HTML-DOM实例——实现带样式的表单验证
    HTML-DOM常用对象的用法(select/option/form/table)
    C++程序嵌入Python解释器二次开发
    线程池、协程
    Qt信号(SINGAL)与槽(SLOT)
    随机数
    字符串、内存拷贝
    模板元编程以及关键字template和typename
    std::thread,std::future,std::promise,std::async
    C++智能指针,RAII(资源获取即初始化) 原则
  • 原文地址:https://www.cnblogs.com/strengthen/p/9748285.html
Copyright © 2011-2022 走看看