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

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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

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 }

 

posted @ 2019-03-17 10:03  为敢技术  阅读(242)  评论(0编辑  收藏  举报