★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-xwjxswxf-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
S
and T
are strings composed of lowercase letters. In S
, no letter occurs more than once.git
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.github
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:app
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
中,全部字符只會出現一次。this
S
已經根據某種規則進行了排序。咱們要根據S
中的字符順序對T
進行排序。更具體地說,若是S
中x
在y
以前出現,那麼返回的字符串中x
也應出如今y
以前。spa
返回任意一種符合條件的字符串T
。code
示例: 輸入: S = "cba" T = "abcd" 輸出: "cbad" 解釋: S中出現了字符 "a", "b", "c", 因此 "a", "b", "c" 的順序應該是 "c", "b", "a". 因爲 "d" 沒有在S中出現, 它能夠放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的輸出。
注意:htm
S
的最大長度爲26
,其中沒有重複的字符。T
的最大長度爲200
。S
和T
只包含小寫字符。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 }
8msblog
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 }