Chapter 7 String

String is a special char array ended with NULL character \0.

7.1 C++ string functions

isalpha, isalnum, isdigit, tolower, toupper

find(‘\’,start_pos), string::npos, s.erase(0,L), s.substr(1), s.substr(1,2), s.erase(s.begin()), s.append(5,‘x’)

[cling]$ #include <henry.h>
[cling]$ using namespace std;
[cling]$ std::string s("simonwoo");
[cling]$ s.rfind("o")
(unsigned long) 7
[cling]$ s.rfind('o')
(unsigned long) 7
[cling]$ s.rfind('o',0)
(unsigned long) 18446744073709551615
[cling]$ s.rfind('o',0) == string::npos
(bool) true
[cling]$ s.find('o',0)
(unsigned long) 3
[cling]$ s.erase(4,5)   // 5 is length
(std::__cxx11::basic_string &) "simo"
[cling]$ s.append(5,'x')
(std::__cxx11::basic_string &) "simoxxxxx"
[cling]$ s.substr(1)
(std::__cxx11::basic_string) "imoxxxxx"
[cling]$ s.substr(1,3)
(std::__cxx11::basic_string) "imo"
[cling]$ s.erase(1)
(std::__cxx11::basic_string &) "s"
[cling]$ s+="imonwoo"
(std::__cxx11::basic_string &) "simonwoo"
[cling]$ s.erase(s.begin())
(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::iterator) @0x55fb991298a0
[cling]$ s
(std::string &) "imonwoo"
[cling]$ s.erase(s.begin(),s.end())
(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::iterator) @0x55fb9912afc0
[cling]$ s
(std::string &) ""
[cling]$ s="simonwoo"
(std::__cxx11::basic_string &) "simonwoo"
[cling]$ s.find("woo")
(unsigned long) 5
[cling]$ s.find("o",6)
(unsigned long) 6
[cling]$ s.find('o',7)
(unsigned long) 7
[cling]$ s.find("o",8)
(unsigned long) 18446744073709551615
[cling]$ s.erase(remove(s.begin(), s.end(), 'o'),s.end()) // erase all 'o'
(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::iterator) @0x55da465e2630
[cling]$ s
(std::string &) "simnw"

7.2 394. Decode String

Given an encoded string, return it’s decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].
Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

https://leetcode.com/problems/decode-string/

  1. Recursion

Google’s Follow up:

  • Encode String

Description

折叠的定义如下:

1. 一个字符串可以看成它自身的折叠.记作S ~S
2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) ~SSSS...S(X个S).
3. 如果A ~A', B ~B',则AB ~A'B' 例如,因为3(A) = AAA, 2(B) = BB,所以3(A)C2(B) ~AAACBB,而2(3(A)C)2(B) ~AAACAAACBB

给一个字符串,求它的最短折叠.例如AAAAAAAAAABABABCCD的最短折叠为:9(A)3(AB)CCD.

Input

仅一行,即字符串S,长度保证不超过100.

Output

仅一行,即最短的折叠长度.

Sample Input

NEERCYESYESYESNEERCYESYESYES

Sample Output

14

HINT

一个最短的折叠为:2(NEERC3(YES))

题解

dp[l][r]表示l~r的最短折叠长度

即可推出

dp[l][r]=min(r-l+1, dp[l][k]+dp[k+1][r]) where l<=k<r

But an edge case will make this formula fail. If s is a string with 10 repeated abc, then abc9(abc) and 9(abc)abc will be the solution, which is wrong. To fix this, we need to consider当[k+1][r]可以由[l][k]重复得到时还要:

dp[l][r]=min(dp[l][r],dp[l][k]+2+intlen((r-l+1)/(k-l+1)));

intlen用来计算一个Natural Number所占位数, 答案就是dp[0][len-1];

http://hzwer.com/1905.html

T:\(N(O^2)\), ES:\(O(N^2)\)

This is a top-down DP. Very concise!

可以用dp或者opt表示dp数组,矩阵,map等,其实就是一个存储最优值的cache. 用rec()表示dynamic programming函数,因为它一定是recursive function.

7.3 KMP (Knuth Morris Pratt)

KMP算法在面试前应该能手写. 关键是6行的LPPP函数. partial for loop套if else的方法比较好记!

P763 Algorithms 4th Ed.

The KMP matching algorithm uses degenerating property (pattern having same sub-patterns appearing more than once in the pattern) of the pattern and improves the worst case complexity to \(O(n)\).

The key point is to calculate LPPS[]: longest proper prefix which is also proper suffix.

Proper prefix: All the characters in a string, with one or more cut off the end. “S”, “Sn”, “Sna”, and “Snap” are all the proper prefixes of “Snape”.

Proper suffix: All the characters in a string, with one or more cut off the beginning. “agrid”, “grid”, “rid”, “id”, and “d” are all proper suffixes of “Hagrid”.

KMP: http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/

http://www.geeksforgeeks.org/searching-for-patterns-set-2-kmp-algorithm/

7.5 459. Repeated Substring Pattern

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1: Input: “abab” Output: True
Explanation: It's the substring “ab” twice.
Example 2: Input: “aba” Output: False

https://leetcode.com/problems/repeated-substring-pattern/
http://www.geeksforgeeks.org/find-given-string-can-represented-substring-iterating-substring-n-times/

One line solution:

return (s+s).substr(1,2*s.size()-1).find(s)!=s.size()-1;

vector<int> r(0); will generate an empty vector.

This problem is also described at CLRS P1012.

7.6 205. Isomorphic Strings

https://leetcode.com/problems/isomorphic-strings/

Solution: Map + Set

edge case: "ab"->"aa" (false), "ab"->"ca" (true), abc->xab (true)

第二个例子a=>c and b=>a is true can be understand like this. 有个男孩叫a,碰巧有个女孩也叫a,男孩a matches to girl c, and boy b matches to girl a, which is totally fine!

数学上的双射关系. In mathematics, a bijection, bijective function or one-to-one correspondence is a function between the elements of two sets, where each element of one set is paired with exactly one element of the other set, and each element of the other set is paired with exactly one element of the first set. There are no unpaired elements.

建一个map保存映射关系, 同时用一个set保存被映射的char, 保证同一个char不会被映射两次.

本题难度算是比较简单的.需要注意两种情况:
1. a -> e并且a -> f这种很好用unordered_map<char, char>避免
2. a -> e并且b -> e这种用Hashmap就无能为力了,要再原来的基础上添加一个unordered_set<char>记录string t,检测hashmap和hashset的size()是否相等

还有种解法,同一个函数调用两次,交换s和t的位置: http://www.voidcn.com/blog/hechunjiao2011/article/p-2798205.html

更多讨论参见: linkedin-2016-11.html#isomorphic-string

7.7 290. Word Pattern

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:

pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.

Notes: You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

https://leetcode.com/problems/word-pattern

这个题也是bijection关系,和上题如出一辙.

7.8 291. Word Pattern II

Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.

Examples:
pattern = “abab”, str = “redblueredblue” should return true.
pattern = “aaaa”, str = “asdasdasdasd” should return true.
pattern = “aabb”, str = “xyzabcxzyabc” should return false.
Notes: You may assume both pattern and str contains only lowercase letters.

https://leetcode.com/problems/word-pattern-ii

7.9 65. Valid Number

https://leetcode.com/problems/valid-number/

这道题网上的解法说得十分玄乎,实际上是很简单的. 另外Leetcode的test case不完整,没有包含“1E1”这种情况.e可以用大写E.

[cling]$ 1E2
(double) 100.000

数字的模式如下图.*表示可以重复0或者多次:

方法就是index不断前进,如果在绿色的位置退出,都是number,如果在浅红色的位置退出就不是number.位置8和9有点类似画蛇添足的感觉.

唯一容易出错的地方是在位置8的时候,之前snake必须为true,不然可以直接返回false.(" +e1")

C++:

Regular Expression: [\s]*[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?[\s]*

值得注意的是NAN,INFINITY其实也是numeric type,根据IEEE 754.

https://en.wikipedia.org/wiki/NaN

In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities.

std::isnan

7.11 635. Design Log Storage System

You are given several logs that each log contains a unique id and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers.

Design a log storage system to implement the following functions:

void Put(int id, string timestamp): Given a log’s unique id and timestamp, store the log in your storage system.

int Retrieve(String start, String end, String granularity): Return the id of logs whose timestamps are within the range from start to end. Start and end all have the same format as timestamp. However, granularity means the time level for consideration. For example, start = “2017:01:01:23:59:59”, end = “2017:01:02:23:59:59”, granularity = “Day”, it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017.

Example 1:

put(1, "2017:01:01:23:59:59");
put(2, "2017:01:01:22:59:59");
put(3, "2016:01:01:00:00:00");
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year");
// return [1,2,3], because you need to return all logs within 2016 and 2017.
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour");
// return [1,2], because you need to return all logs start from 2016:01:01:01
// to 2017:01:01:23, where log 3 is left outside the range.

Note:
There will be at most 300 operations of Put or Retrieve.
Year ranges from [2000,2017]. Hour ranges from [00,23].
Output for Retrieve has no order required.

注意substr的用法(line 15,16). 还有容易搞混的地方是ID是unique的但是log timestamp并不是,这个也符合工作中的实际情况.所以这里必须用multimap. Again,在RB tree里面range query用lower_boundupper_bound.

7.14 71. Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”

https://leetcode.com/problems/simplify-path/

这道题的难度主要是切分字符串. 数据结构除了用deque之外还可以用stack,第5行的|| !path.empty()部分是为了复用while循环内部的逻辑.

7.15 388. Longest Absolute File Path

Suppose we abstract our file system by a string in the following manner:

The string “dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext” represents:

dir
    subdir1
    subdir2
        file.ext

The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

The string “dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext” represents:

dir
    subdir1
        file1.ext
        subsubdir1
    subdir2
        subsubdir2
            file2.ext

The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is “dir/subdir2/subsubdir2/file2.ext”, and its length is 32 (not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.

https://leetcode.com/problems/longest-absolute-file-path/

http://www.cnblogs.com/grandyang/p/5806493.html

如下图, 问题是从abc.txt到xyz.txt长度该如何调整. 用一个vector记录每层的cumulative的长度,每次遇到“\n”或者最后一行的时候就用max取其最大值.

dir
    subdir1
        file1.ext
        subsubdir1
              abc.txt
    xyz.txt

刚开始的时候写下的代码:

后来发现题目条件有对文件名和folder名做规定.所以我的提交出现如下错误:

Input:"a\n\tb\n\t\tc"
Output:5
Expected:0

加上判断file和folder的条件,代码如下:

7.16 482. License Key Formatting

Now you are given a string S, which represents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes in the given string are possibly misplaced.

We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character). To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case.

So, you are given a non-empty string S, representing a license key to format, and an integer K. And you need to return the license key formatted according to the description above.

Example 1:
Input: S = “2-4A0r7-4k”, K = 4
Output: “24A0-R74K”

Example 2:
Input: S = “2-4A0r7-4k”, K = 3
Output: “24-A0R-74K”

https://leetcode.com/problems/license-key-formatting

这道题让我们对注册码进行格式化,正确的注册码的格式是每四个字符后面跟一个短杠,每一部分的长度为K,第一部分长度可以小于K,另外,字母必须是大写的.那么由于第一部分可以不为K,那么我们可以反过来想,我们从S的尾部往前遍历,把字符加入结果res,每K个后面加一个短杠,那么最后遍历完再把res翻转一下即可,注意翻转之前要把结尾的短杠去掉(如果有的话),参见代码如下:

http://bit.ly/2wokpEv

7.17 616. Add Bold Tag in String

Given a string s and a list of strings dict, you need to add a closed pair of bold tag ‘<b>’ and ‘</b>’ to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.

Examples:
Input: s = “abcxyz123”, dict = [“abc”,“123”]; Output: <b>abc</b>xyz<b>123</b>
Input: s = “aaabbcc”, dict = [“aaa”,“aab”,“bc”]; Output: <b>aaabbc</b>c
Input: s = “zzz”, dict = [“zz”]; Output: <b>zzz</b>

https://leetcode.com/problems/add-bold-tag-in-string

注意find带pos的用法: http://en.cppreference.com/w/cpp/string/basic_string/find

7.18 670. Maximum Swap

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.

Example 1: Input: 2736, Output: 7236
Explanation: Swap the number 2 and the number 7.

Example 2: Input: 9973, Output: 9973
Explanation: No swap.

https://leetcode.com/problems/maximum-swap/

Note: Here we should use find_last_of because we always want to keep bigger number as foremost as possible. One example is 1993. The result should be 9913, instead of 9193.

7.19 165. Compare Version Numbers

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character. The . character does not represent a decimal point and is used to separate number sequences. For instance, 2.5 is not “two and a half” or “half way to version three”, it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

7.20 6. ZigZag Conversion

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: “PAHNAPLSIIGYIR”

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”

https://leetcode.com/problems/zigzag-conversion/

http://fisherlei.blogspot.com/2013/01/leetcode-zigzag-conversion.html

7.21 777. Swap Adjacent in LR String

In a string composed of ‘L’, ‘R’, and ‘X’ characters, like “RXXLRXRXL”, a move consists of either replacing one occurrence of “XL” with “LX”, or replacing one occurrence of “RX” with “XR”. Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other.

Example:

Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True
Explanation:
We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX

Note:

1 <= len(start) = len(end) <= 10000.
Both start and end will only consist of characters in {'L', 'R', 'X'}.

https://leetcode.com/contest/weekly-contest-70/problems/swap-adjacent-in-lr-string/

‘L’ can move to left and ‘R’ can move to right.

Another solution but atucally it is also two pointers:

7.22 415. Add Strings

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

https://leetcode.com/problems/add-strings/


7.23 43. Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.

Note:

The length of both num1 and num2 is < 110.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to 
integer directly.

https://leetcode.com/problems/multiply-strings/

和加法不同的是这题需要预先分配空间.

题目虽然不难.要完全写对也不容易.
1. 首先要注意num1[i] * num2[j]的结果应该加到ret[i+j]的位置上.
2. 其次要注意ln 16不能遗漏最高位的进位,由于此时ret中该位为0,所以只需要将carry转为字符即可.
3. 最容易遗漏的corner case是ln 18.如999*0 = 0000,此时需要去掉开始的0,但又需要保留最后一个0.

http://bangbingsyb.blogspot.com/2014/11/leetcode-multiply-strings.html

7.25 271. Encode and Decode Strings

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.

Machine 1 (sender) has the function:

string encode(vector<string> strs) {
 // ... your code
 return encoded_string;
}

Machine 2 (receiver) has the function:

vector<string> decode(string s) {
 //... your code
 return strs;
}

So Machine 1 does:

string encoded_string = encode(strs);

and Machine 2 does:

vector<string> strs2 = decode(encoded_string);

strs2 in Machine 2 should be the same as strs in Machine 1.

Implement the encode and decode methods.

Note:

The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters. Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless. Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

  • CPP

decode函数也可以这样写,不用删除原始字符串:

vector<string> decode(string s) {
    vector<string> r;
    size_t pos=0, start=0;
    while((pos=s.find('/',start)) != string::npos){
        int len=stoi(s.substr(start,pos-start));
        r.push_back(s.substr(pos+1,len));
        start=pos+1+len;
    }
    return r;
}
  • JAVA

7.26 293. Flip Game

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive “++” into “–”. The game ends when a person can no longer make a move and therefore the other person will be the winner.

Write a function to compute all possible states of the string after one valid move.

For example, given s = “++++”, after one move, it may become one of the following states:

[
 "--++",
 "+--+",
 "++--"
]

If there is no valid move, return an empty list .

7.27 294. Flip Game II

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive “++” into “–”. The game ends when a person can no longer make a move and therefore the other person will be the winner.

Write a function to determine if the starting player can guarantee a win.

For example, given s = “++++”, return true. The starting player can guarantee a win by flipping the middle “++” to become “+–+”.

Follow up: Derive your algorithm’s runtime complexity.

https://leetcode.com/problems/flip-game-ii/
http://www.cnblogs.com/grandyang/p/5226206.html

class Solution {
public:
    bool canWin(string s) {
        size_t i = -1;
        while((i = s.find("++", i + 1)) != s.npos) {
            string t=s.substr(0,i)+"--"+s.substr(i+2);
            if (!canWin(t)) return true;
        }
        return false;
    }
};

7.28 93. Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example: “25525511135”, return [“255.255.11.135”, “255.255.111.35”]. (Order does not matter)

https://leetcode.com/problems/restore-ip-addresses/

几点注意的地方:

  1. 在验证字符串是否是数字的时候,要注意0的情况,001,010,03都是非法的.所以,如果第一位取出来是0,那么我们就判断字符串是否是“0”,不是的情况都是非法的

  2. 取字符串的时候,注意位数不够的问题,不仅<4, 而且<s.length()

  3. 注意substring的范围

  4. 字符串转换成数字 Integer.parseInt();

  5. 别忘了IP 地址里面的 “.”

  6. 到第4个Part的时候我们就可以整体验证剩下的所有字符串(因为第4个Part最后一定要取到结尾才是正确的字符串)

7.29 246. Strobogrammatic Number

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is represented as a string.

For example, the numbers “69”, “88”, and “818” are all strobogrammatic.

https://leetcode.com/problems/strobogrammatic-number

7.30 247. Strobogrammatic Number II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Find all strobogrammatic numbers that are of length = n.

For example, Given n = 2, return [“11”,“69”,“88”,“96”].

https://leetcode.com/problems/strobogrammatic-number-ii

这道题是之前那道Strobogrammatic Number的拓展,那道题让我们判断一个数是否是对称数,而这道题让我们找出长度为n的所有的对称数,我们肯定不能一个数一个数的来判断,那样太不高效了,而且OJ肯定也不会答应.题目中给了提示说可以用递归来做,而且提示了递归调用n-2,而不是n-1.我们先来列举一下n为0,1,2,3,4的情况:

n = 0:   none
n = 1:   0, 1, 8
n = 2:   11, 69, 88, 96
n = 3:   101, 609, 808, 906, 111, 619, 818, 916, 181, 689, 888, 986
n = 4:   1001, 6009, 8008, 9006, 1111, 6119, 8118, 9116, 1691, 6699, 8698, 9696,
         1881, 6889, 8888, 9886, 1961, 6969, 8968, 9966

我们注意观察n=0和n=2,可以发现后者是在前者的基础上,每个数字的左右增加了[1 1], [6 9], [8 8], [9 6],看n=1和n=3更加明显,在0的左右增加[1 1],变成了101, 在0的左右增加[6 9],变成了609, 在0的左右增加[8 8],变成了808, 在0的左右增加[9 6],变成了906, 然后在分别在1和8的左右两边加那四组数,我们实际上是从m=0层开始,一层一层往上加的,需要注意的是当加到了n层的时候,左右两边不能加[0 0],因为0不能出现在两位数及多位数的开头,在中间递归的过程中,需要有在数字左右两边各加上0的那种情况,参见代码如下:

http://www.cnblogs.com/grandyang/p/5200919.html

7.31 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.

https://leetcode.com/problems/custom-sort-string/

My Solution:

Most concise solution:

  • Python