Skip to content

Commit 91e204b

Browse files
authored
Added tasks 28-58
1 parent b41d0fb commit 91e204b

File tree

18 files changed

+584
-0
lines changed

18 files changed

+584
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
28\. Implement strStr()
2+
3+
Easy
4+
5+
Implement [strStr()](http://www.cplusplus.com/reference/cstring/strstr/).
6+
7+
Return the index of the first occurrence of needle in haystack, or `-1` if `needle` is not part of `haystack`.
8+
9+
**Clarification:**
10+
11+
What should we return when `needle` is an empty string? This is a great question to ask during an interview.
12+
13+
For the purpose of this problem, we will return 0 when `needle` is an empty string. This is consistent to C's [strstr()](http://www.cplusplus.com/reference/cstring/strstr/) and Java's [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)).
14+
15+
**Example 1:**
16+
17+
**Input:** haystack = "hello", needle = "ll"
18+
19+
**Output:** 2
20+
21+
**Example 2:**
22+
23+
**Input:** haystack = "aaaaa", needle = "bba"
24+
25+
**Output:** -1
26+
27+
**Example 3:**
28+
29+
**Input:** haystack = "", needle = ""
30+
31+
**Output:** 0
32+
33+
**Constraints:**
34+
35+
* <code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code>
36+
* `haystack` and `needle` consist of only lower-case English characters.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package s0028_find_the_index_of_the_first_occurrence_in_a_string
2+
3+
// #Easy #Top_Interview_Questions #String #Two_Pointers #String_Matching
4+
// #Programming_Skills_II_Day_1 #Top_Interview_150_Array/String
5+
// #2025_05_13_Time_0_ms_(100.00%)_Space_3.83_MB_(94.91%)
6+
7+
func strStr(haystack string, needle string) int {
8+
if needle == "" {
9+
return 0
10+
}
11+
m := len(haystack)
12+
n := len(needle)
13+
for start := 0; start < m-n+1; start++ {
14+
if haystack[start:start+n] == needle {
15+
return start
16+
}
17+
}
18+
return -1
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s0028_find_the_index_of_the_first_occurrence_in_a_string
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestStrStr(t *testing.T) {
9+
assert.Equal(t, 0, strStr("sadbutsad", "sad"))
10+
}
11+
12+
func TestStrStr2(t *testing.T) {
13+
assert.Equal(t, -1, strStr("leetcode", "leeto"))
14+
}
Lines changed: 35 additions & 0 deletions
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.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package s0030_substring_with_concatenation_of_all_words
2+
3+
// #Hard #String #Hash_Table #Sliding_Window #Top_Interview_150_Sliding_Window
4+
// #2025_05_13_Time_5_ms_(91.71%)_Space_8.76_MB_(38.24%)
5+
6+
func findSubstring(s string, words []string) []int {
7+
ans := make([]int, 0)
8+
n1 := len(words[0])
9+
n2 := len(s)
10+
map1 := make(map[string]int)
11+
for _, ch := range words {
12+
map1[ch]++
13+
}
14+
for i := 0; i < n1; i++ {
15+
left := i
16+
j := i
17+
c := 0
18+
map2 := make(map[string]int)
19+
for j+n1 <= n2 {
20+
word1 := s[j : j+n1]
21+
j += n1
22+
if _, ok := map1[word1]; ok {
23+
map2[word1]++
24+
c++
25+
for map2[word1] > map1[word1] {
26+
word2 := s[left : left+n1]
27+
map2[word2]--
28+
left += n1
29+
c--
30+
}
31+
if c == len(words) {
32+
ans = append(ans, left)
33+
}
34+
} else {
35+
map2 = make(map[string]int)
36+
c = 0
37+
left = j
38+
}
39+
}
40+
}
41+
return ans
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0030_substring_with_concatenation_of_all_words
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestFindSubstring(t *testing.T) {
9+
assert.Equal(t, []int{0, 9}, findSubstring("barfoothefoobarman", []string{"foo", "bar"}))
10+
}
11+
12+
func TestFindSubstring2(t *testing.T) {
13+
assert.Equal(t, []int{}, findSubstring("wordgoodgoodgoodbestword", []string{"word", "good", "best", "word"}))
14+
}
15+
16+
func TestFindSubstring3(t *testing.T) {
17+
assert.Equal(t, []int{6, 9, 12}, findSubstring("barfoofoobarthefoobarman", []string{"bar", "foo", "the"}))
18+
}
Lines changed: 58 additions & 0 deletions
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 `'.'`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package s0036_valid_sudoku
2+
3+
// #Medium #Top_Interview_Questions #Array #Hash_Table #Matrix #Data_Structure_I_Day_5_Array
4+
// #Top_Interview_150_Matrix #2025_05_13_Time_0_ms_(100.00%)_Space_4.66_MB_(82.08%)
5+
6+
type Solution struct {
7+
j1 int
8+
i1 [9]int
9+
b1 [9]int
10+
}
11+
12+
func isValidSudoku(board [][]byte) bool {
13+
s := &Solution{}
14+
for i := 0; i < 9; i++ {
15+
for j := 0; j < 9; j++ {
16+
if !s.checkValid(board, i, j) {
17+
return false
18+
}
19+
}
20+
}
21+
return true
22+
}
23+
24+
func (s *Solution) checkValid(board [][]byte, i, j int) bool {
25+
if j == 0 {
26+
s.j1 = 0
27+
}
28+
if board[i][j] == '.' {
29+
return true
30+
}
31+
val := int(board[i][j] - '0')
32+
if s.j1 == (s.j1 | (1 << (val - 1))) {
33+
return false
34+
}
35+
s.j1 |= 1 << (val - 1)
36+
if s.i1[j] == (s.i1[j] | (1 << (val - 1))) {
37+
return false
38+
}
39+
s.i1[j] |= 1 << (val - 1)
40+
b := (i/3)*3 + j/3
41+
if s.b1[b] == (s.b1[b] | (1 << (val - 1))) {
42+
return false
43+
}
44+
s.b1[b] |= 1 << (val - 1)
45+
return true
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package s0036_valid_sudoku
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestIsValidSudoku(t *testing.T) {
9+
board := [][]byte{
10+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
11+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
12+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
13+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
14+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
15+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
16+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
17+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
18+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
19+
}
20+
assert.True(t, isValidSudoku(board))
21+
}
22+
23+
func TestIsValidSudoku2(t *testing.T) {
24+
board := [][]byte{
25+
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
26+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
27+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
28+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
29+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
30+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
31+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
32+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
33+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
34+
}
35+
assert.False(t, isValidSudoku(board))
36+
}
Lines changed: 28 additions & 0 deletions
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`

0 commit comments

Comments
 (0)