• 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 };
  • 相关阅读:
    从传统BI报表系统上重构指标库
    autoload魔术方法的妙用
    kerberos委派详解
    一篇文章弄懂session的两种存储方式
    一款专门针对高质量女性的易语言钓鱼样本简单分析
    长城杯线上赛wp
    羊城杯WP
    ICMP隧道通信原理与通信特征
    浅析栈溢出遇到的坑及绕过技巧
    从本地到WordPress代码注入
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/8503292.html
Copyright © 2020-2023  润新知