The Custom Sort String Algorithm with Count and Write

  • 时间:2020-09-26 22:11:41
  • 分类:网络文摘
  • 阅读:130 次

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.

Count and Write Algorithm

The S has the orders of the characters that appear in the string T thus if we go through the S character by character, and only print those that appear in string T, the order can be preserved. In order to do this, we need to count the letters in T.

When the first step is done, we need to print those letters that do not appear in string S. This is done by going through string T and check if has appeared in S. Since the input S and T are both lowercase letters, we only require 2 static size array to do the counting.

The space complexity is O(1) and the time complexity is O(N). If the input string can be more than lowercase letters, we might use a hash table to do the counting, in which case the space complexity is O(N).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
    string customSortString(string S, string T) {
        int s[26] = { 0 };
        int t[26] = { 0 };
        for (const auto n: S) {
            s[n - 'a'] ++;
        }
        for (const auto n: T) {
            t[n - 'a'] ++;
        }
        string r = "";
        for (int i = 0; i < S.size(); ++ i) {
            for (int k = 0; k < t[S[i] - 'a']; ++ k) {
                r += S[i];
            }
        }
        for (int i = 0; i < T.size(); ++ i) {
            if (s[T[i] - 'a'] == 0) {
                r += T[i];
            }
        }
        return r;
    }
};
class Solution {
public:
    string customSortString(string S, string T) {
        int s[26] = { 0 };
        int t[26] = { 0 };
        for (const auto n: S) {
            s[n - 'a'] ++;
        }
        for (const auto n: T) {
            t[n - 'a'] ++;
        }
        string r = "";
        for (int i = 0; i < S.size(); ++ i) {
            for (int k = 0; k < t[S[i] - 'a']; ++ k) {
                r += S[i];
            }
        }
        for (int i = 0; i < T.size(); ++ i) {
            if (s[T[i] - 'a'] == 0) {
                r += T[i];
            }
        }
        return r;
    }
};

We can reduce the above to using only 1 array of 26, by iterating from ‘a’ to ‘z’ and print those who do not appear in S (that are printed already should not be printed again).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    string customSortString(string S, string T) {
        int t[26] = { 0 };
        for (const auto n: T) {
            t[n - 'a'] ++;
        }
        string r = "";
        for (int i = 0; i < S.size(); ++ i) {
            for (int k = 0; k < t[S[i] - 'a']; ++ k) {
                r += S[i];
            }          
            t[S[i] - 'a'] = 0; // mark those used
        }
        for (char c = 'a'; c <= 'z'; ++ c) {
            for (int k = 0; k < t[c - 'a']; ++ k) {
                r += c;
            }
        }
        return r;
    }
};
class Solution {
public:
    string customSortString(string S, string T) {
        int t[26] = { 0 };
        for (const auto n: T) {
            t[n - 'a'] ++;
        }
        string r = "";
        for (int i = 0; i < S.size(); ++ i) {
            for (int k = 0; k < t[S[i] - 'a']; ++ k) {
                r += S[i];
            }          
            t[S[i] - 'a'] = 0; // mark those used
        }
        for (char c = 'a'; c <= 'z'; ++ c) {
            for (int k = 0; k < t[c - 'a']; ++ k) {
                r += c;
            }
        }
        return r;
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
冬季咳嗽吃什么自制美味止咳小零食  冬季进补鸡汤要注意 几类人不宜喝鸡汤  芦荟的保健价值、食用禁忌及食用方法  冬季食疗养生:吃南瓜可有效治疗哮喘  食疗:有效增强男人性能力的八款药膳  枸杞保健食疗功效多补肾益精养肝明目  如何从感官上辨别枸杞的真假和质量优劣  红酒对人体有保健作用但并非人人适宜  食品安全:盘点有毒的11种常见食物  鸡蛋的蛋黄和蛋清到底哪个更有营养? 
评论列表
添加评论