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.

  1. Example :
  2. Input:
  3. S = "cba"
  4. T = "abcd"
  5. Output: "cbad"
  6. Explanation:
  7. "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
  8. 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=;i<max(lent,lens);i++){
  10. Str+='#';
  11. }
  12. for(int i=;i<lens;i++){
  13. Mp[S[i]]=i+;
  14. }
  15. for(int i=;i<lent;i++){
  16. mp[T[i]]++;
  17. if(Mp[T[i]]==){
  18. continue;
  19. }else{
  20. Str[Mp[T[i]]-]=T[i];
  21. }
  22. }
  23. //cout<<Str<<endl;
  24. for(int i=;i<max(lent,lens);i++){
  25. for(int j=;j<mp[Str[i]];j++){
  26. Ctr+=Str[i];
  27. }
  28. }
  29. for(int i=;i<lent;i++){
  30. if(Mp[T[i]]==){
  31. Ctr+=T[i];
  32. }
  33. }
  34. return Ctr;
  35. }
  36. };

73th LeetCode Weekly Contest Custom Sort String的更多相关文章

  1. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  2. 73th LeetCode Weekly Contest Rotated Digits

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  3. 73th LeetCode Weekly Contest Domino and Tromino Tiling

    We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...

  4. 73th LeetCode Weekly Contest Escape The Ghosts

    You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...

  5. 791. Custom Sort String - LeetCode

    Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

随机推荐

  1. HihoCoder1664 01间隔方阵([Offer收割]编程练习赛40)(DP)

    给定一个NxM的01矩阵,小Hi希望从中找到一个01间隔的子方阵,并且方阵的边长越大越好. 例如对于 0100100 1000101 0101010 1010101 0101010 在右下角有一个4x ...

  2. [HDU4652]Dice

    vjudge 题意 \(m\)面骰子,求 1.连续出现\(n\)个相同的停止: 2.连续出现\(n\)个不同的停止 的期望投骰子次数. \(n,m ≤ 10^6\) sol 首先考虑一个转移式子吧. ...

  3. BestCoder Round #92 比赛记录

    上午考完试后看到了晚上的BestCoder比赛,全机房都来参加 感觉压力好大啊QAQ,要被虐了. 7:00 比赛开始了,迅速点进了T1 大呼这好水啊!告诉了同桌怎么看中文题面 然后就开始码码码,4分1 ...

  4. 洛谷【P1104】生日(冒泡排序版)

    题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,我主要是来讲冒泡排序的. 所谓冒泡排序,流程如下: 每次确定一个\(rk\)(从\(n\ ...

  5. Python xlrd、xlwt、xlutils修改Excel文件-OK

    一.xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件. 首先,打开workbook:    import xlrdwb = ...

  6. vs2015解决fopen、fscanf 要求替换为fopen_s、fscanf_s的办法

    在工程项目设置一下就行:项目属性 -- C/C++-- 预处理器 -- 预处理器定义,添加:_CRT_SECURE_NO_WARNINGS

  7. java web项目创建

    https://www.cnblogs.com/kangjianwei101/p/5621738.html

  8. JVM体系结构之三:方法区之1

    一.简介 方法区在JVM中也是一个非常重要的区域,它与堆一样,是被线程共享的区域.在方法区中,存储了每个类的信息(包括类的名称.方法信息.字段信息).静态变量.常量以及编译器编译后的代码等. 方法区( ...

  9. PopupWindow 从底部弹出窗体

    第一步  : 初始化PopupWindow private void initPop() { if (view == null) { // 照片 view = View.inflate(Registe ...

  10. ViewPage+Fragment(仿微信切换带通知)

    第一步 : 布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...