[Swift]LeetCode791. 自定义字符串排序 | Custom Sort String

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

S and T are strings composed of lowercase letters. In S, no letter occurs more than once.

S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.

Return any permutation of T (as a string) that satisfies this property.

Example :
Input: 
S = "cba"
T = "abcd"
Output: "cbad"
Explanation: 
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a". 
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.

Note:

  • S has length at most 26, and no character is repeated in S.
  • T has length at most 200.
  • S and T consist of lowercase letters only.

字符串S和 T 只包含小写字符。在S中,所有字符只会出现一次。

S 已经根据某种规则进行了排序。我们要根据S中的字符顺序对T进行排序。更具体地说,如果Sxy之前出现,那么返回的字符串中x也应出现在y之前。

返回任意一种符合条件的字符串T

示例:
输入:
S = "cba"
T = "abcd"
输出: "cbad"
解释: 
S中出现了字符 "a", "b", "c", 所以 "a", "b", "c" 的顺序应该是 "c", "b", "a". 
由于 "d" 没有在S中出现, 它可以放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的输出。

注意:

  • S的最大长度为26,其中没有重复的字符。
  • T的最大长度为200
  • ST只包含小写字符。

Runtime: 8 ms
Memory Usage: 19.8 MB
 1 class Solution {
 2     func customSortString(_ S: String, _ T: String) -> String {
 3         var res:String = String()
 4         var m:[Character:Int] = [Character:Int]()
 5         for c in T
 6         {
 7             m[c,default:0] += 1
 8         }
 9         for c in S
10         {
11             let num:Int = m[c,default:0]
12             for _ in 0..<num
13             {
14                 res.append(c)
15             }
16             m[c,default:0] = 0
17         }
18         for (key,val) in m
19         {
20             for _ in 0..<val
21             {
22                 res.append(key)
23             }
24         }
25         return res
26     }
27 }

8ms

 1 class Solution {
 2     func customSortString(_ S: String, _ T: String) -> String {
 3         var tMap: Dictionary<Character, Int> = [:]
 4         for t in T {
 5             if let v = tMap[t] {
 6                 tMap[t] = v + 1
 7             } else {
 8                 tMap[t] = 1
 9             }
10         }
11         
12         var result = ""
13         for s in S {
14             if let v = tMap[s] {
15                 result.append(String(repeating: s, count: v))
16                 tMap.removeValue(forKey: s)
17             }
18         }
19         
20         for k in tMap.keys {
21             result.append(String(repeating: k, count: tMap[k]!))
22         }
23         
24         return result
25     }
26 }

24ms

1 class Solution {
2     func customSortString(_ S: String, _ T: String) -> String {
3         let map = S.enumerated().reduce(into: [:]) { $0[$1.1, default: 0] = $1.0 }
4         let character = T.sorted(by: { map[$0, default: Int.max] < map[$1, default: Int.max] })
5         return String(character)
6     }
7 }

32ms

 1 class Solution {
 2     static func relativeCode(_ c: Character) -> Int {
 3         return Int(c.unicodeScalars.first!.value - Character("a").unicodeScalars.first!.value)
 4     }
 5     func customSortString(_ S: String, _ T: String) -> String {
 6         var order = [Int](repeating: -1, count: 26)
 7         for (i, s) in S.enumerated() {
 8             order[Solution.relativeCode(s)] = i
 9         }
10         
11         return String(T.sorted {
12             return order[Solution.relativeCode($0)] < order[Solution.relativeCode($1)]
13         })
14     }
15 }

 

转载于:https://www.cnblogs.com/strengthen/p/10545744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值