Skip to content

Commit 1d40db5

Browse files
committed
Merge branch 'master' of github.com:BaffinLee/leetcode-javascript
2 parents 85f6708 + 3555a02 commit 1d40db5

File tree

3 files changed

+235
-7
lines changed

3 files changed

+235
-7
lines changed

Diff for: 1501-1600/1544. Make The String Great.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 1544. Make The String Great
2+
3+
- Difficulty: Easy.
4+
- Related Topics: String, Stack.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a string `s` of lower and upper case English letters.
10+
11+
A good string is a string which doesn't have **two adjacent characters** `s[i]` and `s[i + 1]` where:
12+
13+
14+
15+
- `0 <= i <= s.length - 2`
16+
17+
- `s[i]` is a lower-case letter and `s[i + 1]` is the same letter but in upper-case or **vice-versa**.
18+
19+
20+
To make the string good, you can choose **two adjacent** characters that make the string bad and remove them. You can keep doing this until the string becomes good.
21+
22+
Return **the string** after making it good. The answer is guaranteed to be unique under the given constraints.
23+
24+
**Notice** that an empty string is also good.
25+
26+
 
27+
Example 1:
28+
29+
```
30+
Input: s = "leEeetcode"
31+
Output: "leetcode"
32+
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
33+
```
34+
35+
Example 2:
36+
37+
```
38+
Input: s = "abBAcC"
39+
Output: ""
40+
Explanation: We have many possible scenarios, and all lead to the same answer. For example:
41+
"abBAcC" --> "aAcC" --> "cC" --> ""
42+
"abBAcC" --> "abBA" --> "aA" --> ""
43+
```
44+
45+
Example 3:
46+
47+
```
48+
Input: s = "s"
49+
Output: "s"
50+
```
51+
52+
 
53+
**Constraints:**
54+
55+
56+
57+
- `1 <= s.length <= 100`
58+
59+
- `s` contains only lower and upper case English letters.
60+
61+
62+
63+
## Solution
64+
65+
```javascript
66+
/**
67+
* @param {string} s
68+
* @return {string}
69+
*/
70+
var makeGood = function(s) {
71+
var i = 0;
72+
var res = [];
73+
while (i < s.length) {
74+
if (res.length
75+
&& s[i] !== res[res.length - 1]
76+
&& s[i].toLowerCase() === res[res.length - 1].toLowerCase()
77+
) {
78+
res.pop();
79+
} else {
80+
res.push(s[i]);
81+
}
82+
i += 1;
83+
}
84+
return res.join('');
85+
};
86+
```
87+
88+
**Explain:**
89+
90+
nope.
91+
92+
**Complexity:**
93+
94+
* Time complexity : O(n).
95+
* Space complexity : O(n).

Diff for: 201-300/205. Isomorphic Strings.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ Output: true
5050
var isIsomorphic = function(s, t) {
5151
if (s.length !== t.length) return false;
5252
var map = {};
53-
var usedMap = {};
53+
var used = {};
5454
for (var i = 0; i < s.length; i++) {
55-
if (!map[s[i]]) {
56-
if (usedMap[t[i]]) return false;
57-
usedMap[t[i]] = true;
55+
if (map[s[i]]) {
56+
if (map[s[i]] !== t[i]) return false;
57+
} else {
58+
if (used[t[i]]) return false;
59+
used[t[i]] = true;
5860
map[s[i]] = t[i];
59-
} else if (map[s[i]] !== t[i]) {
60-
return false;
6161
}
6262
}
6363
return true;
@@ -71,4 +71,4 @@ nope.
7171
**Complexity:**
7272

7373
* Time complexity : O(n).
74-
* Space complexity : O(1).
74+
* Space complexity : O(n).

Diff for: 201-300/289. Game of Life.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# 289. Game of Life
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Matrix, Simulation.
5+
- Similar Questions: Set Matrix Zeroes.
6+
7+
## Problem
8+
9+
According to Wikipedia's article: "The **Game of Life**, also known simply as **Life**, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
10+
11+
The board is made up of an `m x n` grid of cells, where each cell has an initial state: **live** (represented by a `1`) or **dead** (represented by a `0`). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
12+
13+
14+
15+
- Any live cell with fewer than two live neighbors dies as if caused by under-population.
16+
17+
- Any live cell with two or three live neighbors lives on to the next generation.
18+
19+
- Any live cell with more than three live neighbors dies, as if by over-population.
20+
21+
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
22+
23+
24+
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the `m x n` grid `board`, return **the next state**.
25+
26+
 
27+
Example 1:
28+
29+
![](https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg)
30+
31+
```
32+
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
33+
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
34+
```
35+
36+
Example 2:
37+
38+
![](https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg)
39+
40+
```
41+
Input: board = [[1,1],[1,0]]
42+
Output: [[1,1],[1,1]]
43+
```
44+
45+
 
46+
**Constraints:**
47+
48+
49+
50+
- `m == board.length`
51+
52+
- `n == board[i].length`
53+
54+
- `1 <= m, n <= 25`
55+
56+
- `board[i][j]` is `0` or `1`.
57+
58+
59+
 
60+
**Follow up:**
61+
62+
63+
64+
- Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
65+
66+
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
67+
68+
69+
70+
## Solution
71+
72+
```javascript
73+
/**
74+
* @param {number[][]} board
75+
* @return {void} Do not return anything, modify board in-place instead.
76+
*/
77+
var gameOfLife = function(board) {
78+
for (var i = 0; i < board.length; i++) {
79+
for (var j = 0; j < board[i].length; j++) {
80+
var count = countLiveneighbors(board, i, j);
81+
if (board[i][j] === 1) {
82+
if (count < 2 || count > 3) {
83+
board[i][j] = 2;
84+
}
85+
} else {
86+
if (count === 3) {
87+
board[i][j] = 3;
88+
}
89+
}
90+
}
91+
}
92+
for (var i = 0; i < board.length; i++) {
93+
for (var j = 0; j < board[i].length; j++) {
94+
if (board[i][j] === 2) {
95+
board[i][j] = 0;
96+
} else if (board[i][j] === 3) {
97+
board[i][j] = 1;
98+
}
99+
}
100+
}
101+
};
102+
103+
var countLiveneighbors = function(board, i, j) {
104+
var directions = [
105+
[0, 1],
106+
[0, -1],
107+
[1, 0],
108+
[-1, 0],
109+
[1, 1],
110+
[1, -1],
111+
[-1, 1],
112+
[-1, -1],
113+
];
114+
var count = 0;
115+
for (var m = 0; m < directions.length; m++) {
116+
var [y, x] = directions[m];
117+
if (!board[i + y]) continue;
118+
if (board[i + y][j + x] === 1 || board[i + y][j + x] === 2) {
119+
count += 1;
120+
}
121+
}
122+
return count;
123+
};
124+
```
125+
126+
**Explain:**
127+
128+
nope.
129+
130+
**Complexity:**
131+
132+
* Time complexity : O(n).
133+
* Space complexity : O(n).

0 commit comments

Comments
 (0)