• 73th LeetCode Weekly Contest 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.
    

    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和S都出现的字母,我们按照S的字母出现位置放进新的字符串里,然后呢,没出现的放最后,再然后就是把空的位置去掉就好了。

     1 class Solution {
     2 public:
     3     map<char,int>Mp,mp;
     4     string customSortString(string S, string T) {
     5        string Str="";
     6        string Ctr="";
     7        int lens=S.length();
     8        int lent=T.length();
     9        for(int i=0;i<max(lent,lens);i++){
    10             Str+='#';
    11        }
    12        for(int i=0;i<lens;i++){
    13             Mp[S[i]]=i+1;
    14        }
    15        for(int i=0;i<lent;i++){
    16            mp[T[i]]++;
    17         if(Mp[T[i]]==0){
    18            continue;
    19         }else{
    20             Str[Mp[T[i]]-1]=T[i];
    21         }
    22        }
    23         //cout<<Str<<endl;
    24        for(int i=0;i<max(lent,lens);i++){
    25             for(int j=0;j<mp[Str[i]];j++){
    26                     Ctr+=Str[i];
    27                 }
    28        }
    29        for(int i=0;i<lent;i++){
    30             if(Mp[T[i]]==0){
    31                 Ctr+=T[i];
    32             }
    33        }
    34        return Ctr;
    35     }
    36 };
  • 相关阅读:
    线段树优化dp(elect选择)
    gdb调试
    无参装饰器
    3.23作业
    3.22周末作业
    函数对象与闭包函数
    3.20作业
    3.19作业
    名称空间与作用域
    函数参数的使用
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/8503292.html
Copyright © 2020-2023  润新知