zoukankan      html  css  js  c++  java
  • [Swift]LeetCode205. 同构字符串 | Isomorphic Strings

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

    Given two strings s and t, determine if they are isomorphic.

    Two strings are isomorphic if the characters in s can be replaced to get t.

    All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

    Example 1:

    Input: s = "egg", t = "add"
    Output: true
    

    Example 2:

    Input: s = "foo", t = "bar"
    Output: false

    Example 3:

    Input: s = "paper", t = "title"
    Output: true

    Note:
    You may assume both and have the same length.


    给定两个字符串 和 t,判断它们是否是同构的。

    如果 中的字符可以被替换得到 ,那么这两个字符串是同构的。

    所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。

    示例 1:

    输入: s = "egg", t = "add"
    输出: true
    

    示例 2:

    输入: s = "foo", t = "bar"
    输出: false

    示例 3:

    输入: s = "paper", t = "title"
    输出: true

    说明:
    你可以假设 和 具有相同的长度。


     28ms

     1 class Solution {
     2   func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3     let s = Array(s)
     4     let t = Array(t)
     5     
     6     var dict: [Character: Character] = [:]
     7     
     8     for i in 0..<s.count {
     9       if let cache = dict[s[i]] {
    10         if cache != t[i] {
    11           return false
    12         }
    13       } else if dict.values.contains(t[i]) {
    14         return false
    15       } else {
    16         dict[s[i]] = t[i]
    17       }
    18     }
    19     
    20     return true
    21   }
    22 }

    28ms

     1 class Solution {
     2     func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3         guard s.count == t.count else {
     4             return false
     5         }
     6         
     7         var map: [Character: Character] = [:]
     8         var set: Set<Character> = []
     9         var charsS = Array(s)
    10         var charsT = Array(t)
    11         for index in 0..<s.count {
    12             let charS = charsS[index]
    13             let charT = charsT[index]
    14             if let char = map[charS] {
    15                 if char != charT {
    16                     return false
    17                 }
    18             } else {
    19                 if set.contains(charT) {
    20                     return false
    21                 }
    22                 
    23                 map[charS] = charT
    24                 set.insert(charT)
    25             }
    26         }
    27         
    28         return true
    29     }
    30 }

    32ms

     1 class Solution {
     2     func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3         let s = Array(s)
     4         let t = Array(t)
     5         if s.count != t.count { return false }
     6         
     7         var isoMap1 = [Character:Character]()
     8         var isoMap2 = [Character:Character]()
     9         
    10         var index = 0
    11         
    12         while index < s.count {
    13             
    14             let c1 = s[index]
    15             let c2 = t[index]
    16             
    17             if let saved = isoMap1[c1] {
    18                 if saved != c2 {
    19                     return false
    20                 }
    21             } else {
    22                 isoMap1[c1] = c2
    23             }
    24             
    25             if let saved = isoMap2[c2] {
    26                 if saved != c1 {
    27                     return false
    28                 }
    29             } else {
    30                 isoMap2[c2] = c1
    31             }
    32             index += 1
    33         }
    34         return true
    35     }
    36 }

    36ms

     1 class Solution {
     2     func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3         var m1 = [Int](repeating: 0, count: 256)
     4         var m2 = [Int](repeating: 0, count: 256)
     5         var s = Array(s)
     6         var t = Array(t)
     7     
     8         for i in 0..<s.count {
     9             let sAscii = Int(s[i].unicodeScalars.first?.value ?? 0)
    10             let tAscii = Int(t[i].unicodeScalars.first?.value ?? 0)
    11             if m1[sAscii] != m2[tAscii] {
    12                 return false
    13             }
    14             m1[sAscii] = i + 1
    15             m2[tAscii] = i + 1
    16         }
    17     
    18         return true
    19     }
    20 }

    44ms

     1 class Solution {
     2     func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3         var s0 = Array(s)
     4         var t0 = Array(t)
     5         var dic = [Character : Character]()
     6         
     7         
     8         for i in 0..<s0.count {
     9             let s1 = s0[i]
    10             let t1 = t0[i]
    11             if let tmp = dic[s1]  {
    12                 if tmp == t1 {
    13                     continue
    14                 }else {
    15                     return false
    16                 }
    17             }else {
    18                 if dic.values.contains(t1) {
    19                     return false
    20                 }
    21                 dic[s1] = t1
    22             }
    23         }
    24         
    25         return true
    26     }
    27 }

    52ms

     1 class Solution {
     2     func isIsomorphic(_ s: String, _ t: String) -> Bool {
     3         var mapS = [Character:Character]()
     4         var mapT = [Character:Character]()
     5         for idx in 0 ..< s.count {
     6             let cs = s[String.Index.init(encodedOffset: idx)]
     7             let ct = t[String.Index.init(encodedOffset: idx)]
     8             
     9             if let c = mapT[ct] {
    10                 if c != cs {
    11                     return false
    12                 }
    13             } else {
    14                 mapT[ct] = cs
    15             }
    16             
    17             if let c = mapS[cs] {
    18                 if c != ct {
    19                     return false
    20                 }
    21             } else {
    22                 mapS[cs] = ct
    23             }
    24         }
    25         return true
    26     }
    27 }
  • 相关阅读:
    Spring使用@Value注解各种类型的值
    Jdom生成xml文件时的特殊字符问题
    将博客搬至CSDN
    ubuntu/mint添加字体
    linux保持ssh连接
    servlet 重定向与转发区别
    u盘写保护
    修改默认终端
    sudo apt-get update 无法获得锁
    logback多线程日志MDC
  • 原文地址:https://www.cnblogs.com/strengthen/p/9745441.html
Copyright © 2011-2022 走看看