Skip to content

Commit 6199107

Browse files
committed
[Function add]
1. Add leetcode solutions with tag permutation.
1 parent 73ebf4f commit 6199107

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@
531531
* [216. Combination Sum III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/216.%20Combination%20Sum%20III.md)
532532
* Conclusion: We can speed up the recursion using an index and if we want all elements used once, we need to sort first and in the for loop, we need to remove duplicate values.
533533

534+
#### Permutation
535+
* [46. Permutations](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/46.%20Permutations.md)
536+
* [47. Permutations II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/47.%20Permutations%20II.md)
537+
* [784. Letter Case Permutation](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/784.%20Letter%20Case%20Permutation.md)
538+
* [943. Find the Shortest Superstring](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/943.%20Find%20the%20Shortest%20Superstring.md)
539+
* [996. Number of Squareful Arrays](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/996.%20Number%20of%20Squareful%20Arrays.md)
534540

535541
## Algorithm(4th_Edition)
536542
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
## 943. Find the Shortest Superstring
2+
3+
### Question:
4+
Given an array A of strings, find any smallest string that contains each string in A as a substring.
5+
We may assume that no string in A is substring of another string in A.
6+
7+
```
8+
Example 1:
9+
10+
Input: ["alex","loves","leetcode"]
11+
Output: "alexlovesleetcode"
12+
Explanation: All permutations of "alex","loves","leetcode" would also be accepted.
13+
14+
Example 2:
15+
16+
Input: ["catg","ctaagt","gcta","ttca","atgcatc"]
17+
Output: "gctaagttcatgcatc"
18+
```
19+
20+
Note:
21+
* 1 <= A.length <= 12
22+
* 1 <= A[i].length <= 20
23+
24+
25+
### Solution:
26+
* Method 1: search O(N! + N^2) AC 775ms
27+
* First create a cost[i][j] matrix, means the number of characters need to be added at the end of the result if last added word is A[i] and we want to add A[j]. Cost matrix is calculated at the very beginning so it will be calculated once, and its time efficiency is O(n^2).
28+
* We list all possible permutations and save the minimum. Pruning is required for remove useless iterations.
29+
* We only record the path of words it goes through and concat the string at the end(we only concat once).
30+
```Java
31+
class Solution {
32+
public String shortestSuperstring(String[] A) {
33+
if(A.length == 1) return A[0];
34+
int[][] cost = new int[A.length][A.length];
35+
for(int i = 0; i < A.length; i++){
36+
LABEL:
37+
for(int j = 0; j < A.length; j++){
38+
if(i == j){
39+
cost[i][j] = A[j].length();
40+
}else{
41+
int start = A[i].indexOf(A[j].charAt(0));
42+
if(start == -1) cost[i][j] = A[j].length();
43+
else{
44+
for(int k = start; k < A[i].length(); k++){
45+
if(A[j].startsWith(A[i].substring(k))){
46+
cost[i][j] = A[j].length() - (A[i].length() - k);
47+
continue LABEL;
48+
}
49+
}
50+
cost[i][j] = A[j].length();
51+
}
52+
}
53+
}
54+
}
55+
List<Integer> res = new ArrayList<>();
56+
dfs(A, cost, 0, 0, new ArrayList<Integer>(), res, 0);
57+
StringBuilder sb = new StringBuilder();
58+
for(int i = 0; i < res.size(); i++){
59+
Integer path = res.get(i);
60+
sb.append(i == 0 ? A[path]: A[path].substring(A[path].length() - cost[res.get(i - 1)][path]));
61+
}
62+
return sb.toString();
63+
}
64+
private int sum = Integer.MAX_VALUE;
65+
private void dfs(String[] A, int[][] cost, int index, int temp, List<Integer> path, List<Integer> res, int visited){
66+
if(temp >= sum) return;
67+
if(A.length == index){
68+
sum = temp;
69+
res.clear();
70+
res.addAll(path);
71+
return;
72+
}
73+
for(int i = 0; i < A.length; i++){
74+
if((visited & (1 << i)) > 0) continue;
75+
path.add(i);
76+
dfs(A,
77+
cost,
78+
index + 1,
79+
index == 0 ? A[i].length(): temp + cost[path.get(index - 1)][i],
80+
path,
81+
res,
82+
(visited | (1 << i)));
83+
path.remove(index);
84+
}
85+
}
86+
}
87+
```
88+
89+
* Method 2: DP Need to fill when doing the dp questions.
90+
91+
### Reference
92+
1. [花花酱 LeetCode 943. Find the Shortest Superstring - 刷题找工作 EP231](https://www.youtube.com/watch?v=u_Wc4jwrp3Q)
93+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## 996. Number of Squareful Arrays
2+
3+
### Question:
4+
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
5+
6+
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
7+
8+
```
9+
Example 1:
10+
11+
Input: [1,17,8]
12+
Output: 2
13+
Explanation:
14+
[1,8,17] and [17,8,1] are the valid permutations.
15+
16+
Example 2:
17+
18+
Input: [2,2,2]
19+
Output: 1
20+
```
21+
22+
Note:
23+
* 1 <= A.length <= 12
24+
* 0 <= A[i] <= 1e9
25+
26+
27+
28+
### Solution:
29+
* Method 1: search O(N!) AC 3ms
30+
* Pruning is super important, I cannot AC without pruning.
31+
* need to check if the last two variables are squareful.
32+
```Java
33+
class Solution {
34+
private int res = 0;
35+
public int numSquarefulPerms(int[] A) {
36+
if(A.length == 1) return 0;
37+
Arrays.sort(A);
38+
dfs(A, new ArrayList<Integer>(), 0);
39+
return res;
40+
}
41+
private void dfs(int[] A, List<Integer> temp, int visited){
42+
if(temp.size() == A.length){
43+
boolean flag = true;
44+
for(int i = 1; i < A.length; i++){
45+
flag &= square(temp.get(i - 1) + temp.get(i));
46+
}
47+
if(flag) res++;
48+
}else if(temp.size() <= 1 || square(temp.get(temp.size() - 1) + temp.get(temp.size() - 2))){
49+
for(int i = 0; i < A.length; i++){
50+
if((visited & (1 << i)) > 0) continue;
51+
if(i > 0 && A[i] == A[i - 1] && (visited & (1 << (i - 1))) == 0) continue;
52+
temp.add(A[i]);
53+
dfs(A, temp, visited | (1 << i));
54+
temp.remove(temp.size() - 1);
55+
}
56+
}
57+
}
58+
private boolean square(int sum){
59+
return (int)Math.sqrt(sum) * (int)Math.sqrt(sum) == sum;
60+
}
61+
}
62+
```
63+
64+
* Method 2: DP Need to fill when doing the dp questions.
65+
66+
### Reference
67+
1. [花花酱 LeetCode 996. Number of Squareful Arrays](https://zxi.mytechroad.com/blog/searching/leetcode-996-number-of-squareful-arrays/)
68+

0 commit comments

Comments
 (0)