Skip to content

Commit ae96004

Browse files
committed
2 parents c24f083 + 4d44e61 commit ae96004

16 files changed

+1490
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 187. Repeated DNA Sequences
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Hash Table, String, Bit Manipulation, Sliding Window, Rolling Hash, Hash Function.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
The **DNA sequence** is composed of a series of nucleotides abbreviated as `'A'`, `'C'`, `'G'`, and `'T'`.
10+
11+
12+
13+
- For example, `"ACGAATTCCG"` is a **DNA sequence**.
14+
15+
16+
When studying **DNA**, it is useful to identify repeated sequences within the DNA.
17+
18+
Given a string `s` that represents a **DNA sequence**, return all the **`10`-letter-long** sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in **any order**.
19+
20+
 
21+
Example 1:
22+
```
23+
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
24+
Output: ["AAAAACCCCC","CCCCCAAAAA"]
25+
```Example 2:
26+
```
27+
Input: s = "AAAAAAAAAAAAA"
28+
Output: ["AAAAAAAAAA"]
29+
```
30+
 
31+
**Constraints:**
32+
33+
34+
35+
- `1 <= s.length <= 105`
36+
37+
- `s[i]` is either `'A'`, `'C'`, `'G'`, or `'T'`.
38+
39+
40+
41+
## Solution
42+
43+
```javascript
44+
/**
45+
* @param {string} s
46+
* @return {string[]}
47+
*/
48+
var findRepeatedDnaSequences = function(s) {
49+
var res = [];
50+
var map = {};
51+
for (var i = 0; i <= s.length - 10; i++) {
52+
var str = s.slice(i, i + 10);
53+
var num = map[str];
54+
if (num === -1) continue;
55+
if (num === 1) {
56+
map[str] = -1;
57+
res.push(str);
58+
continue;
59+
}
60+
map[str] = 1;
61+
}
62+
return res;
63+
};
64+
```
65+
66+
**Explain:**
67+
68+
nope.
69+
70+
**Complexity:**
71+
72+
* Time complexity : O(n).
73+
* Space complexity : O(1).

101-200/191. Number of 1 Bits.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 191. Number of 1 Bits
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Divide and Conquer, Bit Manipulation.
5+
- Similar Questions: Reverse Bits, Power of Two, Counting Bits, Binary Watch, Hamming Distance, Binary Number with Alternating Bits, Prime Number of Set Bits in Binary Representation.
6+
7+
## Problem
8+
9+
Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
10+
11+
**Note:**
12+
13+
14+
15+
- Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
16+
17+
- In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. `-3`.
18+
19+
20+
 
21+
Example 1:
22+
23+
```
24+
Input: n = 00000000000000000000000000001011
25+
Output: 3
26+
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
27+
```
28+
29+
Example 2:
30+
31+
```
32+
Input: n = 00000000000000000000000010000000
33+
Output: 1
34+
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
35+
```
36+
37+
Example 3:
38+
39+
```
40+
Input: n = 11111111111111111111111111111101
41+
Output: 31
42+
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
43+
```
44+
45+
 
46+
**Constraints:**
47+
48+
49+
50+
- The input must be a **binary string** of length `32`.
51+
52+
53+
 
54+
**Follow up:** If this function is called many times, how would you optimize it?
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number} n - a positive integer
61+
* @return {number}
62+
*/
63+
var hammingWeight = function(n) {
64+
var count = 0;
65+
while (n) {
66+
if (n & 1) count++;
67+
n >>>= 1;
68+
}
69+
return count;
70+
};
71+
```
72+
73+
**Explain:**
74+
75+
use `>>>` instead of `>>`.
76+
77+
**Complexity:**
78+
79+
* Time complexity : O(1).
80+
* Space complexity : O(1).
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 1160. Find Words That Can Be Formed by Characters
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, String.
5+
- Similar Questions: Rearrange Characters to Make Target String.
6+
7+
## Problem
8+
9+
You are given an array of strings `words` and a string `chars`.
10+
11+
A string is **good** if it can be formed by characters from chars (each character can only be used once).
12+
13+
Return **the sum of lengths of all good strings in words**.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: words = ["cat","bt","hat","tree"], chars = "atach"
20+
Output: 6
21+
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
28+
Output: 10
29+
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
30+
```
31+
32+
 
33+
**Constraints:**
34+
35+
36+
37+
- `1 <= words.length <= 1000`
38+
39+
- `1 <= words[i].length, chars.length <= 100`
40+
41+
- `words[i]` and `chars` consist of lowercase English letters.
42+
43+
44+
45+
## Solution
46+
47+
```javascript
48+
/**
49+
* @param {string[]} words
50+
* @param {string} chars
51+
* @return {number}
52+
*/
53+
var countCharacters = function(words, chars) {
54+
var map = Array(26).fill(0);
55+
var a = 'a'.charCodeAt(0);
56+
for (var i = 0; i < chars.length; i++) {
57+
map[chars.charCodeAt(i) - a]++;
58+
}
59+
var res = 0;
60+
outerLoop: for (var m = 0; m < words.length; m++) {
61+
var arr = Array.from(map);
62+
for (var n = 0; n < words[m].length; n++) {
63+
if (--arr[words[m].charCodeAt(n) - a] < 0) continue outerLoop;
64+
}
65+
res += words[m].length;
66+
}
67+
return res;
68+
};
69+
```
70+
71+
**Explain:**
72+
73+
nope.
74+
75+
**Complexity:**
76+
77+
* Time complexity : O(m * n).
78+
* Space complexity : O(1).
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Union Find, Graph, Sorting, Minimum Spanning Tree, Strongly Connected Component.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a weighted undirected connected graph with `n` vertices numbered from `0` to `n - 1`, and an array `edges` where `edges[i] = [ai, bi, weighti]` represents a bidirectional and weighted edge between nodes `ai` and `bi`. A minimum spanning tree (MST) is a subset of the graph's edges that connects all vertices without cycles and with the minimum possible total edge weight.
10+
11+
Find **all the critical and pseudo-critical edges in the given graph's minimum spanning tree (MST)**. An MST edge whose deletion from the graph would cause the MST weight to increase is called a **critical edge**. On the other hand, a pseudo-critical edge is that which can appear in some MSTs but not all.
12+
13+
Note that you can return the indices of the edges in any order.
14+
15+
 
16+
Example 1:
17+
18+
19+
![](https://assets.leetcode.com/uploads/2020/06/04/ex1.png)
20+
21+
22+
```
23+
Input: n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]
24+
Output: [[0,1],[2,3,4,5]]
25+
Explanation: The figure above describes the graph.
26+
The following figure shows all the possible MSTs:
27+
28+
Notice that the two edges 0 and 1 appear in all MSTs, therefore they are critical edges, so we return them in the first list of the output.
29+
The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges. We add them to the second list of the output.
30+
```
31+
32+
Example 2:
33+
34+
35+
![](https://assets.leetcode.com/uploads/2020/06/04/ex2.png)
36+
37+
38+
```
39+
Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]
40+
Output: [[],[0,1,2,3]]
41+
Explanation: We can observe that since all 4 edges have equal weight, choosing any 3 edges from the given 4 will yield an MST. Therefore all 4 edges are pseudo-critical.
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- `2 <= n <= 100`
50+
51+
- `1 <= edges.length <= min(200, n * (n - 1) / 2)`
52+
53+
- `edges[i].length == 3`
54+
55+
- `0 <= ai < bi < n`
56+
57+
- `1 <= weighti <= 1000`
58+
59+
- All pairs `(ai, bi)` are **distinct**.
60+
61+
62+
63+
## Solution
64+
65+
```javascript
66+
/**
67+
* @param {number} n
68+
* @param {number[][]} edges
69+
* @return {number[][]}
70+
*/
71+
var findCriticalAndPseudoCriticalEdges = function(n, edges) {
72+
var [min] = findMinSpanningTreeWeight(n, edges);
73+
var res = [[], []];
74+
for (var i = 0; i < edges.length; i++) {
75+
var [num, parents] = findMinSpanningTreeWeight(n, edges, undefined, edges[i]);
76+
var root = find(parents, 0);
77+
var isCritical = num > min || !Array(n).fill(0).every((_, k) => find(parents, k) === root);
78+
if (isCritical) {
79+
res[0].push(i);
80+
} else {
81+
var [num2] = findMinSpanningTreeWeight(n, edges, edges[i], undefined);
82+
if (num2 === min) res[1].push(i);
83+
}
84+
}
85+
return res;
86+
};
87+
88+
var findMinSpanningTreeWeight = function(n, edges, mustHaveItem, mustNotHaveItem) {
89+
edges = [...edges];
90+
edges.sort((a, b) => a[2] - b[2]);
91+
92+
if (mustHaveItem !== undefined) {
93+
edges = edges.filter((item) => item !== mustHaveItem);
94+
edges.unshift(mustHaveItem);
95+
}
96+
97+
var res = 0;
98+
var parents = Array(n).fill(0).map((_, i) => i);
99+
var count = Array(n).fill(0);
100+
for (var i = 0; i < edges.length; i++) {
101+
if (edges[i] === mustNotHaveItem) continue;
102+
var [m, k, distance] = edges[i];
103+
var j = find(parents, m);
104+
var p = find(parents, k);
105+
if (j === p) continue;
106+
if (count[j] <= count[p]) {
107+
union(parents, j, p);
108+
count[p]++;
109+
} else {
110+
union(parents, p, j);
111+
count[j]++;
112+
}
113+
res += distance;
114+
}
115+
116+
return [res, parents];
117+
};
118+
119+
var find = function(parents, i) {
120+
if (parents[i] === i) return i
121+
parents[i] = find(parents, parents[i]);
122+
return parents[i];
123+
};
124+
125+
var union = function(parents, i, j) {
126+
parents[i] = j;
127+
};
128+
```
129+
130+
**Explain:**
131+
132+
nope.
133+
134+
**Complexity:**
135+
136+
* Time complexity : O(m * n ^ 2).
137+
* Space complexity : O(n ^ 2).

0 commit comments

Comments
 (0)