Skip to content

Commit 0f6735a

Browse files
authored
Added tasks 30-58
1 parent b98c9e4 commit 0f6735a

File tree

16 files changed

+560
-130
lines changed

16 files changed

+560
-130
lines changed

README.md

+145-130
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
30\. Substring with Concatenation of All Words
2+
3+
Hard
4+
5+
You are given a string `s` and an array of strings `words` of **the same length**. Return all starting indices of substring(s) in `s` that is a concatenation of each word in `words` **exactly once**, **in any order**, and **without any intervening characters**.
6+
7+
You can return the answer in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "barfoothefoobarman", words = ["foo","bar"]
12+
13+
**Output:** [0,9]
14+
15+
**Explanation:** Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
20+
21+
**Output:** []
22+
23+
**Example 3:**
24+
25+
**Input:** s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
26+
27+
**Output:** [6,9,12]
28+
29+
**Constraints:**
30+
31+
* <code>1 <= s.length <= 10<sup>4</sup></code>
32+
* `s` consists of lower-case English letters.
33+
* `1 <= words.length <= 5000`
34+
* `1 <= words[i].length <= 30`
35+
* `words[i]` consists of lower-case English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// #Hard #String #Hash_Table #Sliding_Window #Top_Interview_150_Sliding_Window
2+
// #2025_04_01_Time_13_ms_(97.44%)_Space_63.70_MB_(46.03%)
3+
4+
function findSubstring(s: string, words: string[]): number[] {
5+
let ans: number[] = [];
6+
let n1 = words[0].length;
7+
let n2 = s.length;
8+
let map1 = new Map<string, number>();
9+
10+
for (let ch of words) {
11+
map1.set(ch, (map1.get(ch) ?? 0) + 1);
12+
}
13+
14+
for (let i = 0; i < n1; i++) {
15+
let left = i;
16+
let j = i;
17+
let c = 0;
18+
let map2 = new Map<string, number>();
19+
20+
while (j + n1 <= n2) {
21+
let word1 = s.substring(j, j + n1);
22+
j += n1;
23+
24+
if (map1.has(word1)) {
25+
map2.set(word1, (map2.get(word1) ?? 0) + 1);
26+
c++;
27+
28+
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
29+
let word2 = s.substring(left, left + n1);
30+
map2.set(word2, (map2.get(word2) ?? 0) - 1);
31+
left += n1;
32+
c--;
33+
}
34+
35+
if (c === words.length) {
36+
ans.push(left);
37+
}
38+
} else {
39+
map2.clear();
40+
c = 0;
41+
left = j;
42+
}
43+
}
44+
}
45+
return ans;
46+
}
47+
48+
export { findSubstring }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
36\. Valid Sudoku
2+
3+
Medium
4+
5+
Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
6+
7+
1. Each row must contain the digits `1-9` without repetition.
8+
2. Each column must contain the digits `1-9` without repetition.
9+
3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition.
10+
11+
**Note:**
12+
13+
* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
14+
* Only the filled cells need to be validated according to the mentioned rules.
15+
16+
**Example 1:**
17+
18+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
19+
20+
**Input:**
21+
22+
board =
23+
[["5","3",".",".","7",".",".",".","."]
24+
,["6",".",".","1","9","5",".",".","."]
25+
,[".","9","8",".",".",".",".","6","."]
26+
,["8",".",".",".","6",".",".",".","3"]
27+
,["4",".",".","8",".","3",".",".","1"]
28+
,["7",".",".",".","2",".",".",".","6"]
29+
,[".","6",".",".",".",".","2","8","."]
30+
,[".",".",".","4","1","9",".",".","5"]
31+
,[".",".",".",".","8",".",".","7","9"]]
32+
33+
**Output:** true
34+
35+
**Example 2:**
36+
37+
**Input:**
38+
39+
board =
40+
[["8","3",".",".","7",".",".",".","."]
41+
,["6",".",".","1","9","5",".",".","."]
42+
,[".","9","8",".",".",".",".","6","."]
43+
,["8",".",".",".","6",".",".",".","3"]
44+
,["4",".",".","8",".","3",".",".","1"]
45+
,["7",".",".",".","2",".",".",".","6"]
46+
,[".","6",".",".",".",".","2","8","."]
47+
,[".",".",".","4","1","9",".",".","5"]
48+
,[".",".",".",".","8",".",".","7","9"]]
49+
50+
**Output:** false
51+
52+
**Explanation:** Same as Example 1, except with the **5** in the top left corner being modified to **8**. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
53+
54+
**Constraints:**
55+
56+
* `board.length == 9`
57+
* `board[i].length == 9`
58+
* `board[i][j]` is a digit `1-9` or `'.'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Medium #Top_Interview_Questions #Array #Hash_Table #Matrix #Data_Structure_I_Day_5_Array
2+
// #Top_Interview_150_Matrix #2025_04_01_Time_1_ms_(99.60%)_Space_58.94_MB_(52.85%)
3+
4+
function isValidSudoku(board: string[][]): boolean {
5+
let rowSet: number[] = new Array(9).fill(0);
6+
let colSet: number[] = new Array(9).fill(0);
7+
let boxSet: number[] = new Array(9).fill(0);
8+
9+
for (let i = 0; i < 9; i++) {
10+
for (let j = 0; j < 9; j++) {
11+
if (board[i][j] === '.') {
12+
continue;
13+
}
14+
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0);
15+
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
16+
17+
if ((rowSet[i] & (1 << val)) || (colSet[j] & (1 << val)) || (boxSet[boxIndex] & (1 << val))) {
18+
return false;
19+
}
20+
21+
rowSet[i] |= 1 << val;
22+
colSet[j] |= 1 << val;
23+
boxSet[boxIndex] |= 1 << val;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
export { isValidSudoku }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
50\. Pow(x, n)
2+
3+
Medium
4+
5+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
6+
7+
**Example 1:**
8+
9+
**Input:** x = 2.00000, n = 10
10+
11+
**Output:** 1024.00000
12+
13+
**Example 2:**
14+
15+
**Input:** x = 2.10000, n = 3
16+
17+
**Output:** 9.26100
18+
19+
**Example 3:**
20+
21+
**Input:** x = 2.00000, n = -2
22+
23+
**Output:** 0.25000
24+
25+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
26+
27+
**Constraints:**
28+
29+
* `-100.0 < x < 100.0`
30+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
31+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math
2+
// #2025_04_01_Time_0_ms_(100.00%)_Space_57.31_MB_(7.22%)
3+
4+
function myPow(x: number, n: number): number {
5+
let nn = BigInt(n);
6+
let res = 1.0;
7+
if (n < 0) {
8+
nn = -nn;
9+
}
10+
while (nn > 0) {
11+
if (nn % 2n === 1n) {
12+
nn--;
13+
res *= x;
14+
} else {
15+
x *= x;
16+
nn /= 2n;
17+
}
18+
}
19+
return n < 0 ? 1.0 / res : res;
20+
}
21+
22+
export { myPow }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
52\. N-Queens II
2+
3+
Hard
4+
5+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
6+
7+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
12+
13+
**Input:** n = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 1
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 9`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Hard #Backtracking #Top_Interview_150_Backtracking
2+
// #2025_04_01_Time_1_ms_(96.89%)_Space_56.94_MB_(52.17%)
3+
4+
function totalNQueens(n: number): number {
5+
function solve(r: number, cols: boolean[], diag: boolean[], antiDiag: boolean[]): number {
6+
if (r === n) {
7+
return 1;
8+
}
9+
let count = 0;
10+
for (let c = 0; c < n; c++) {
11+
if (!cols[c] && !diag[r + c] && !antiDiag[r - c + n - 1]) {
12+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true;
13+
count += solve(r + 1, cols, diag, antiDiag);
14+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false;
15+
}
16+
}
17+
return count;
18+
}
19+
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false));
20+
}
21+
22+
export { totalNQueens }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
54\. Spiral Matrix
2+
3+
Medium
4+
5+
Given an `m x n` `matrix`, return _all elements of the_ `matrix` _in spiral order_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg)
10+
11+
**Input:** matrix = [[1,2,3],[4,5,6],[7,8,9]]
12+
13+
**Output:** [1,2,3,6,9,8,7,4,5]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg)
18+
19+
**Input:** matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
20+
21+
**Output:** [1,2,3,4,8,12,11,10,9,5,6,7]
22+
23+
**Constraints:**
24+
25+
* `m == matrix.length`
26+
* `n == matrix[i].length`
27+
* `1 <= m, n <= 10`
28+
* `-100 <= matrix[i][j] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Simulation
2+
// #Programming_Skills_II_Day_8 #Level_2_Day_1_Implementation/Simulation #Udemy_2D_Arrays/Matrix
3+
// #Top_Interview_150_Matrix #2025_04_01_Time_0_ms_(100.00%)_Space_55.71_MB_(30.13%)
4+
5+
function spiralOrder(matrix: number[][]): number[] {
6+
const result: number[] = [];
7+
let r = 0, c = 0;
8+
let bigR = matrix.length - 1;
9+
let bigC = matrix[0].length - 1;
10+
11+
while (r <= bigR && c <= bigC) {
12+
for (let i = c; i <= bigC; i++) {
13+
result.push(matrix[r][i]);
14+
}
15+
r++;
16+
17+
for (let i = r; i <= bigR; i++) {
18+
result.push(matrix[i][bigC]);
19+
}
20+
bigC--;
21+
22+
for (let i = bigC; i >= c && r <= bigR; i--) {
23+
result.push(matrix[bigR][i]);
24+
}
25+
bigR--;
26+
27+
for (let i = bigR; i >= r && c <= bigC; i--) {
28+
result.push(matrix[i][c]);
29+
}
30+
c++;
31+
}
32+
33+
return result;
34+
}
35+
36+
export { spiralOrder }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// tslint:disable:no-magic-numbers
2+
import { findSubstring } from 'src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('findSubstring', () => {
6+
expect(findSubstring('barfoothefoobarman', ['foo','bar'])).toEqual([0,9])
7+
})
8+
9+
test('findSubstring2', () => {
10+
expect(findSubstring('wordgoodgoodgoodbestword', ['word','good','best','word'])).toEqual([])
11+
})
12+
13+
test('findSubstring3', () => {
14+
expect(findSubstring('barfoofoobarthefoobarman', ['bar','foo','the'])).toEqual([6,9,12])
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// tslint:disable:no-magic-numbers
2+
import { isValidSudoku } from 'src/main/ts/g0001_0100/s0036_valid_sudoku/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('isValidSudoku', () => {
6+
expect(isValidSudoku([['5','3','.','.','7','.','.','.','.']
7+
,['6','.','.','1','9','5','.','.','.']
8+
,['.','9','8','.','.','.','.','6','.']
9+
,['8','.','.','.','6','.','.','.','3']
10+
,['4','.','.','8','.','3','.','.','1']
11+
,['7','.','.','.','2','.','.','.','6']
12+
,['.','6','.','.','.','.','2','8','.']
13+
,['.','.','.','4','1','9','.','.','5']
14+
,['.','.','.','.','8','.','.','7','9']])).toEqual(true)
15+
})
16+
17+
test('isValidSudoku2', () => {
18+
expect(isValidSudoku([['8','3','.','.','7','.','.','.','.']
19+
,['6','.','.','1','9','5','.','.','.']
20+
,['.','9','8','.','.','.','.','6','.']
21+
,['8','.','.','.','6','.','.','.','3']
22+
,['4','.','.','8','.','3','.','.','1']
23+
,['7','.','.','.','2','.','.','.','6']
24+
,['.','6','.','.','.','.','2','8','.']
25+
,['.','.','.','4','1','9','.','.','5']
26+
,['.','.','.','.','8','.','.','7','9']])).toEqual(false)
27+
})

0 commit comments

Comments
 (0)