C_01 String matching in array
Gives you a string array words, each string in the array can be regarded as a word. Please return all words that are substrings of other words in words in any order.
If you can delete the leftmost and / or rightmost characters of words [j] to get word [i], then the string words [i] is a substring of words [j].
输入:words = ["mass","as","hero","superhero"]
输出:["as","hero"]
解释:"as" 是 "mass" 的子字符串,"hero" 是 "superhero" 的子字符串。
["hero","as"] 也是有效的答案。
Method 1: Violence
public List<String> stringMatching(String[] words) {
LinkedList<String> list = new LinkedList<>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (i == j)
continue;
if (words[i].contains(words[j]) && !list.contains(words[j]))
list.add(words[j]);
}
}
return list;
}
Complexity analysis
- time complexity: ,
- Space complexity: ,
B_02 Query the arrangement with key
Give you an array of queries to check, the elements in the array are positive integers between 1 and m. Please process all the items to be queried according to the following rules [i] (from i = 0 to i = queries.length-1):
- At the beginning, arrange P = [1,2,3, ..., m].
- For the current i, please find the position of the query item queues [i] in the arrangement P (the subscript starts from 0), and then move it from the original position to the starting position of the arrangement P (that is, the index is 0 Office). Note that the position of queries [i] in P is the query results of queries [i].
Please return the query results of the array query to be checked in the form of an array.
- 1 <= m <= 10^3
1 <= queries.length <= m
1 <= queries[i] <= m
输入:queries = [3,1,2,1], m = 5
输出:[2,1,2,1]
解释:待查数组 queries 处理如下:
对于 i=0: queries[i]=3, P=[1,2,3,4,5], 3 在 P 中的位置是 2,接着我们把 3 移动到 P 的起始位置,得到 P=[3,1,2,4,5] 。
对于 i=1: queries[i]=1, P=[3,1,2,4,5], 1 在 P 中的位置是 1,接着我们把 1 移动到 P 的起始位置,得到 P=[1,3,2,4,5] 。
对于 i=2: queries[i]=2, P=[1,3,2,4,5], 2 在 P 中的位置是 2,接着我们把 2 移动到 P 的起始位置,得到 P=[2,1,3,4,5] 。
对于 i=3: queries[i]=1, P=[2,1,3,4,5], 1 在 P 中的位置是 1,接着我们把 1 移动到 P 的起始位置,得到 P=[1,2,3,4,5] 。
因此,返回的结果数组为 [2,1,2,1] 。
Method 1: Simulation
public int[] processQueries(int[] queries, int m) {
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 1; i <= m; i++) {
nums.add(i);
}
LinkedList<Integer> res = new LinkedList<Integer>();
for (int q : queries) {
int idx = nums.indexOf(q);
res.add(idx);
nums.remove(idx);
nums.add(0, q);
}
int p = 0;
int[] arr = new int[queries.length];
for (int i : res) {
arr[p++] = i;
}
return arr;
}
Complexity analysis
- time complexity: ,
- Space complexity: ,
B_03 HTML entity parser
"HTML entity parser" is a special parser that takes HTML code as input and replaces all these special character entities with characters themselves.
These special characters and their corresponding character entities in HTML include:
- Double quotes: the character entity is "", and the corresponding character is "".
Single quotes: The character entity is 'and the corresponding character is'.
And symbol: The character entity is &, and the corresponding character is &.
Greater than sign: The character entity is>, and the corresponding character is>.
Less than sign: The character entity is <, and the corresponding character is <.
Slash: The character entity is ⁄ and the corresponding character is /.
Enter the string text for you, please implement an HTML entity parser, and return the parsing result of the parser.
Method 1: Replace string
public String entityParser(String text) {
String res = text;
res = res.replaceAll(""", "\"");
res = res.replaceAll("'", "'");
res = res.replaceAll("&", "&");
res = res.replaceAll(">", ">");
res = res.replaceAll("<", "<");
res = res.replaceAll("⁄", "/");
return res;
}
Complexity analysis
- time complexity: ,
- Space complexity: ,
A_04 Number of schemes to color N x 3 grid graph (no AC)
输入:n = 2
输出:54
输入:n = 3
输出:246
示例 4:
输入:n = 7
输出:106494
示例 5:
输入:n = 5000
输出:30228214
prompt:
- n == grid.length
grid[i].length == 3
1 <= n <= 5000
method one:
Complexity analysis
- time complexity: ,
- Space complexity: ,