[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 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://leetcode.com/problems/custom-sort-string/discuss/116621/Naive-but-easy-to-understand-solution-explanation-as-the-comments

原文地址:https://www.cnblogs.com/hellowooorld/p/8475415.html

时间: 2024-10-07 12:00:15

[leetcode-791-Custom Sort String]的相关文章

[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

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 x occ

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

[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

leetcode第一刷_Scramble String

字符串的好题.题干解释的非常复杂,一下让人不知所措了. 这道题到底是什么意思呢?最终的结果是把一个字符串中字母的顺序打乱了,让你判断一个字符串能不能由另一个字符串打乱得到.那打乱这个过程是怎么做的呢,很简单,给你一个字符串,你必须先找一个点把它砍成两半,你可以通过交换这两半的顺序来打乱源字符串的顺序,也就是在两半中的字符与另一半中所有字符的相对顺序是统一的.对于每一半,都可以重复上面的过程. 那想一下,怎么知道打断的那个点在哪呢?穷举.怎么知道打断之后有没有做交换操作呢?两种情况递归,有一条走的

【LeetCode】Insertion Sort List

题目 Sort a linked list using insertion sort. 解答 链表无法像数组那样从后往前依次比较插入,只能从前往后:在链表首部添加一个哨兵可以稍微简化下代码,代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }

[LeetCode 题解]: Insertion Sort List

Sort a linked list using insertion sort. 题目要求:链表的插入排序,由于没有时间复杂度的要求,可以直接循环操作. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

LeetCode OJ - Insertion Sort List

题目: Sort a linked list using insertion sort. 解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class S

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