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就加1
如果不存在,就直接加入到返回字符串

最后遍历sIdx数组,将S中的字符加入到返回字符串

Java实现:

public String customSortString(String S, String T) {
    Map<Character, Integer> sMap = new HashMap<>();
    int[] sIdx = new int[S.length()];
    for (int i = 0; i < S.length(); i++) {
        sMap.put(S.charAt(i), i);
        sIdx[i] = 0;
    }

    String retStr = "";
    for (char c : T.toCharArray()) {
        if(sMap.containsKey(c)) {
            sIdx[sMap.get(c)] += 1;
            continue;
        };
        retStr += String.valueOf(c);
    }

    for (int i = 0; i < S.length(); i++) {
        while (sIdx[i]-- > 0) {
            retStr += S.charAt(i);
        }
    }
    return retStr;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9526362.html

时间: 2024-10-12 04:43:08

791. Custom Sort String - LeetCode的相关文章

[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

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

Scramble String leetcode java

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

Reverse Words in a String leetcode java

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters con

Interleaving String leetcode java

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 题解: 这道题还是像

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

Sort Colors leetcode java

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an