• [Swift-2019力扣杯春季决赛]2. 按字典序排列最小的等效字符串


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

    给出长度相同的两个字符串:A 和 B,其中 A[i] 和 B[i] 是一组等价字符。举个例子,如果 A = "abc" 且 B = "cde",那么就有 'a' == 'c', 'b' == 'd', 'c' == 'e'

    等价字符遵循任何等价关系的一般规则:

    • 自反性:'a' == 'a'
    • 对称性:'a' == 'b' 则必定有 'b' == 'a'
    • 传递性:'a' == 'b' 且 'b' == 'c' 就表明 'a' == 'c'

    例如,A 和 B 的等价信息和之前的例子一样,那么 S = "eed""acd" 或 "aab",这三个字符串都是等价的,而 "aab" 是 S 的按字典序最小的等价字符串

    利用 A 和 B 的等价信息,找出并返回 S 的按字典序排列最小的等价字符串。

    示例 1:

    输入:A = "parker", B = "morris", S = "parser"
    输出:"makkek"
    解释:根据 AB 中的等价信息,我们可以将这些字符分为 [m,p], [a,o], [k,r,s], [e,i] 共 4 组。每组中的字符都是等价的,并按字典序排列。所以答案是 "makkek"

    示例 2:

    输入:A = "hello", B = "world", S = "hold"
    输出:"hdld"
    解释:根据 AB 中的等价信息,我们可以将这些字符分为 [h,w], [d,e,o], [l,r] 共 3 组。所以只有 S 中的第二个字符 'o' 变成 'd',最后答案为 "hdld"

    示例 3:

    输入:A = "leetcode", B = "programs", S = "sourcecode"
    输出:"aauaaaaada"
    解释:我们可以把 A 和 B 中的等价字符分为 [a,o,e,r,s,c], [l,p], [g,t][d,m] 共 4 组,因此 S 中除了 'u''d' 之外的所有字母都转化成了 'a',最后答案为 "aauaaaaada"

    提示:

    1. 字符串 AB 和 S 仅有从 'a' 到 'z' 的小写英文字母组成。
    2. 字符串 AB 和 S 的长度在 1 到 1000 之间。
    3. 字符串 A 和 B 长度相同。

    64 ms

     1 class Solution {
     2     var f:[Int] = [Int](repeating:0,count:26)
     3     func smallestEquivalentString(_ A: String, _ B: String, _ S: String) -> String {
     4         var n:Int = A.count
     5         var m:Int = S.count
     6         var ans:String = String()
     7         for i in 0..<26
     8         {
     9             f[i] = i
    10         }
    11         let arrA:[Int] = Array(A).map{$0.ascii - 97}
    12         let arrB:[Int] = Array(B).map{$0.ascii - 97}
    13         for i in 0..<n
    14         {
    15             merge(arrA[i],arrB[i])
    16         }
    17         let arrS:[Int] = Array(S).map{$0.ascii - 97}
    18         for i in 0..<m
    19         {
    20             ans.append((get(arrS[i]) + 97).ASCII)
    21         }
    22         return ans
    23     }
    24     
    25     func get(_ x:Int) -> Int
    26     {
    27         if f[x] == x
    28         {
    29             return x
    30         }
    31         f[x] = get(f[x])
    32         return f[x]
    33     }
    34     
    35     func merge(_ x:Int,_ y:Int)
    36     {
    37         var x = x
    38         var y = y
    39         x = get(x)
    40         y = get(y)
    41         if x < y
    42         {
    43             (x,y) = (y,x)
    44         }
    45         f[x] = y
    46     }
    47 }
    48 
    49 //Character扩展 
    50 extension Character  
    51 {  
    52   //Character转ASCII整数值(定义小写为整数值)
    53    var ascii: Int {
    54        get {
    55            return Int(self.unicodeScalars.first?.value ?? 0)
    56        }       
    57     }
    58 }
    59 
    60 //Int扩展
    61 extension Int
    62 {
    63     //Int转Character,ASCII值(定义大写为字符值)
    64     var ASCII:Character 
    65     {
    66         get {return Character(UnicodeScalar(self)!)}
    67     }
    68 }
  • 相关阅读:
    poj 3436 ACM Computer Factory 夜
    poj 1182 食物链 夜
    poj 2299 UltraQuickSort 夜
    E. Printer 夜
    poj 3083 Children of the Candy Corn 夜
    sdut 2500 0\'s 夜
    1776. Anniversary Firework sdut 2507 焰火表演 夜
    删除上传文件中可能包含的空行
    ALV的fieldcat属性
    ALV显示红绿灯(FM&nbsp;ALV&nbsp;和&nbsp;OO&nbsp;ALV两…
  • 原文地址:https://www.cnblogs.com/strengthen/p/10745785.html
Copyright © 2020-2023  润新知