791. Custom Sort String

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.

把字符串T按照S的序列重排。

解决:统计T的字符,按照S的顺序依次输出。

class Solution {
public:
    string customSortString(string S, string T) {
        string res;
        int cnt[26] = {0};
        for (auto t: T)
            ++cnt[t-‘a‘];
        for (auto s: S) {
            res.append(cnt[s-‘a‘], s);
            cnt[s-‘a‘] = 0;
        }
        for (int i=0; i<26; ++i)
            res.append(cnt[i], ‘a‘+i);
        return res;
    }
};

原文地址:https://www.cnblogs.com/Zzz-y/p/8475937.html

时间: 2024-10-12 04:43:12

791. Custom Sort String的相关文章

791. Custom Sort String - LeetCode

Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T = "abcd" 先构造一个map,sMap key存储S中出现的字符,value存储字符在S中的位置 c -> 0 b -> 1 a -> 2 再构造一个int数组,sIdx sIdx,存储S中的字符在T字符串中出现的次数 遍历T字符串 如果字符在sMap中,sIdx

791. Custom Sort String字符串保持字母一样,位置可以变

[抄题]: 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

[leetcode]791. Custom Sort String自定义排序字符串

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 occ

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

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 occ

G面经prepare: Sort String Based On Another

Given a sorting order string, sort the input string based on the given sorting order string. Ex sorting order string -> dfbcae Input string -> abcdeeabc output -> dbbccaaee 法一:Comparable sample Input: String order = "dfbcae"; String str

[leetcode-791-Custom Sort String]

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 occ

(线段树区间赋值)CSU 1942 - Sort String

题意: 一个串(串中只有26个小写字母),选一个区间进行排序,进行100000次,输出最后的串. 分析: 比赛的时候很懵逼,感觉这题跟之前的额大崩龙有点像,但是没多想,也怪自己太菜了. 确实是真的像,甚至是一模一样啊. 对于每次排序只需要进行一次类似计数排序的的操作即可,26个字符,进行26次区间赋值即可.理论上时间能过得去. 代码: 1 #include <queue> 2 #include <string> 3 #include <cstdio> 4 #includ

arts-week6

Algorithm 830. Positions of Large Groups - LeetCode 75. Sort Colors - LeetCode 859. Buddy Strings - LeetCode 791. Custom Sort String - LeetCode 804. Unique Morse Code Words - LeetCode 877. Stone Game - LeetCode 890. Find and Replace Pattern - LeetCod

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman