博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode-791-Custom Sort String]
阅读量:6945 次
发布时间:2019-06-27

本文共 1248 字,大约阅读时间需要 4 分钟。

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.

思路:

依次从T中找到S中的元素即可满足条件。

string customSortString(string S, string T){    string ret = "";    for(int i = 0;i < S.size();i++)    {        for(int j = 0;j < T.size();j++)        {            if(S[i] == T[j])            {                ret = ret + S[i];                T[j] = '#';            }        }    }    for(int i = 0;i < T.size();i++)    {        if(T[i] != '#')ret += T[i];    }    return ret;}

参考:

转载于:https://www.cnblogs.com/hellowooorld/p/8475415.html

你可能感兴趣的文章
Ubuntu shutdown 关机、重启、注销 命令 常用实例
查看>>
图片切换[置顶] 送大家几款可以运用到实际项目的flash+xml控件
查看>>
XSS第二节,XSS左邻右舍
查看>>
蔬菜销售策划
查看>>
15个带给您优秀用户体验的移动应用 UI 设计
查看>>
Visual Studio 宏的高级用法
查看>>
Android -- 解析xml
查看>>
IC芯片
查看>>
解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译)
查看>>
批处理创建文件夹
查看>>
手机网站调试神器之chrome控制台
查看>>
UVa 825 - Walking on the Safe Side
查看>>
Could not load file or assembly or one of its dependencies. 试图加载格式不正确的程序。
查看>>
PHP超大文件下载,断点续传下载
查看>>
C++ overloading contructor
查看>>
怎样配置PHP环境和安装Zendstdio编辑器
查看>>
基于Maven构建开发第一个Storm项目
查看>>
SQL Server on Linux 理由浅析
查看>>
Oracle database
查看>>
JAVA - JAVA编译运行过程
查看>>