Skip to content

Commit ccf123f

Browse files
authored
Added tasks 28-57
1 parent 0c61225 commit ccf123f

File tree

16 files changed

+463
-0
lines changed

16 files changed

+463
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
245245

246246
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
247247
|-|-|-|-|-|-
248+
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00
248249

249250
#### Day 2 String
250251

@@ -344,6 +345,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
344345

345346
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
346347
|-|-|-|-|-|-
348+
| 0057 |[Insert Interval](src/main/python/g0001_0100/s0057_insert_interval/Solution.java)| Medium | Array | 0 | 100.00
347349
| 0056 |[Merge Intervals](src/main/python/g0001_0100/s0056_merge_intervals/Solution0056.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 72.98
348350

349351
#### Day 18 Stack
@@ -430,6 +432,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
430432
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
431433
|-|-|-|-|-|-
432434
| 0074 |[Search a 2D Matrix](src/main/python/g0001_0100/s0074_search_a_2d_matrix/Solution0074.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
435+
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00
433436
| 0048 |[Rotate Image](src/main/python/g0001_0100/s0048_rotate_image/Solution0048.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
434437
| 0073 |[Set Matrix Zeroes](src/main/python/g0001_0100/s0073_set_matrix_zeroes/Solution0073.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 3 | 71.07
435438
| 0056 |[Merge Intervals](src/main/python/g0001_0100/s0056_merge_intervals/Solution0056.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 72.98
@@ -530,6 +533,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
530533
| 0012 |[Integer to Roman](src/main/python/g0001_0100/s0012_integer_to_roman/Solution0012.py)| Medium | String, Hash_Table, Math, Big_O_Time_O(1)_Space_O(1) | 0 | 100.00
531534
| 0014 |[Longest Common Prefix](src/main/python/g0001_0100/s0014_longest_common_prefix/Solution0014.py)| Easy | Top_Interview_Questions, String, Big_O_Time_O(n\*m)_Space_O(m) | 0 | 100.00
532535
| 0006 |[Zigzag Conversion](src/main/python/g0001_0100/s0006_zigzag_conversion/Solution0006.py)| Medium | String, Big_O_Time_O(n)_Space_O(n) | 5 | 93.09
536+
| 0028 |[Implement strStr()](src/main/python/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string/Solution.java)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 0 | 100.00
533537

534538
#### Top Interview 150 Two Pointers
535539

@@ -543,12 +547,15 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
543547
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
544548
|-|-|-|-|-|-
545549
| 0003 |[Longest Substring Without Repeating Characters](src/main/python/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution0003.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 9 | 97.01
550+
| 0030 |[Substring with Concatenation of All Words](src/main/python/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.java)| Hard | String, Hash_Table, Sliding_Window | 11 | 97.43
546551
| 0076 |[Minimum Window Substring](src/main/python/g0001_0100/s0076_minimum_window_substring/Solution0076.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 59 | 88.78
547552

548553
#### Top Interview 150 Matrix
549554

550555
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
551556
|-|-|-|-|-|-
557+
| 0036 |[Valid Sudoku](src/main/python/g0001_0100/s0036_valid_sudoku/Solution.java)| Medium | Top_Interview_Questions, Array, Hash_Table, Matrix | 1 | 100.00
558+
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00
552559
| 0048 |[Rotate Image](src/main/python/g0001_0100/s0048_rotate_image/Solution0048.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
553560
| 0073 |[Set Matrix Zeroes](src/main/python/g0001_0100/s0073_set_matrix_zeroes/Solution0073.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 3 | 71.07
554561

@@ -565,6 +572,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
565572
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
566573
|-|-|-|-|-|-
567574
| 0056 |[Merge Intervals](src/main/python/g0001_0100/s0056_merge_intervals/Solution0056.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 72.98
575+
| 0057 |[Insert Interval](src/main/python/g0001_0100/s0057_insert_interval/Solution.java)| Medium | Array | 0 | 100.00
568576

569577
#### Top Interview 150 Stack
570578

@@ -729,6 +737,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
729737

730738
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
731739
|-|-|-|-|-|-
740+
| 0036 |[Valid Sudoku](src/main/python/g0001_0100/s0036_valid_sudoku/Solution.java)| Medium | Top_Interview_Questions, Array, Hash_Table, Matrix | 1 | 100.00
732741
| 0074 |[Search a 2D Matrix](src/main/python/g0001_0100/s0074_search_a_2d_matrix/Solution0074.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
733742

734743
#### Day 6 String
@@ -1495,6 +1504,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
14951504

14961505
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14971506
|-|-|-|-|-|-
1507+
| 0028 |[Implement strStr()](src/main/python/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string/Solution.java)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 0 | 100.00
14981508

14991509
#### Day 2
15001510

@@ -1532,6 +1542,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
15321542

15331543
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15341544
|-|-|-|-|-|-
1545+
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00
15351546

15361547
#### Day 9
15371548

@@ -1670,8 +1681,10 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
16701681
| 0070 |[Climbing Stairs](src/main/python/g0001_0100/s0070_climbing_stairs/Solution0070.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
16711682
| 0064 |[Minimum Path Sum](src/main/python/g0001_0100/s0064_minimum_path_sum/Solution0064.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(m\*n)_Space_O(m\*n) | 15 | 60.38
16721683
| 0062 |[Unique Paths](src/main/python/g0001_0100/s0062_unique_paths/Solution0062.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, LeetCode_75_DP/Multidimensional, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
1684+
| 0057 |[Insert Interval](src/main/python/g0001_0100/s0057_insert_interval/Solution.java)| Medium | Array, Level_2_Day_17_Interval, Top_Interview_150_Intervals | 0 | 100.00
16731685
| 0056 |[Merge Intervals](src/main/python/g0001_0100/s0056_merge_intervals/Solution0056.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix, Top_Interview_150_Intervals, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 72.98
16741686
| 0055 |[Jump Game](src/main/python/g0001_0100/s0055_jump_game/Solution0055.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 12 | 85.38
1687+
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix | 0 | 100.00
16751688
| 0053 |[Maximum Subarray](src/main/python/g0001_0100/s0053_maximum_subarray/Solution0053.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm, Top_Interview_150_Kadane's_Algorithm, Big_O_Time_O(n)_Space_O(1) | 50 | 77.23
16761689
| 0051 |[N-Queens](src/main/python/g0001_0100/s0051_n_queens/Solution0051.py)| Hard | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(N!)_Space_O(N) | 6 | 98.12
16771690
| 0049 |[Group Anagrams](src/main/python/g0001_0100/s0049_group_anagrams/Solution0049.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings, Top_Interview_150_Hashmap, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 11 | 82.75
@@ -1681,11 +1694,14 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
16811694
| 0042 |[Trapping Rain Water](src/main/python/g0001_0100/s0042_trapping_rain_water/Solution0042.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Dynamic_Programming_I_Day_9, Udemy_Two_Pointers, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 11 | 71.16
16821695
| 0041 |[First Missing Positive](src/main/python/g0001_0100/s0041_first_missing_positive/Solution0041.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 19 | 93.81
16831696
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution0039.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Top_Interview_150_Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 4 | 90.51
1697+
| 0036 |[Valid Sudoku](src/main/python/g0001_0100/s0036_valid_sudoku/Solution.java)| Medium | Top_Interview_Questions, Array, Hash_Table, Matrix, Data_Structure_I_Day_5_Array, Top_Interview_150_Matrix | 1 | 100.00
16841698
| 0035 |[Search Insert Position](src/main/python/g0001_0100/s0035_search_insert_position/Solution0035.py)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Top_Interview_150_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
16851699
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution0034.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Top_Interview_150_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
16861700
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution0033.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Top_Interview_150_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
16871701
| 0032 |[Longest Valid Parentheses](src/main/python/g0001_0100/s0032_longest_valid_parentheses/Solution0032.py)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 6 | 66.50
16881702
| 0031 |[Next Permutation](src/main/python/g0001_0100/s0031_next_permutation/Solution0031.py)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1703+
| 0030 |[Substring with Concatenation of All Words](src/main/python/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.java)| Hard | String, Hash_Table, Sliding_Window, Top_Interview_150_Sliding_Window | 11 | 97.43
1704+
| 0028 |[Implement strStr()](src/main/python/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string/Solution.java)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching, Programming_Skills_II_Day_1, Top_Interview_150_Array/String | 0 | 100.00
16891705
| 0027 |[Remove Element](src/main/python/g0001_0100/s0027_remove_element/Solution0027.py)| Easy | Array, Two_Pointers, Top_Interview_150_Array/String | 0 | 100.00
16901706
| 0026 |[Remove Duplicates from Sorted Array](src/main/python/g0001_0100/s0026_remove_duplicates_from_sorted_array/Solution0026.py)| Easy | Top_Interview_Questions, Array, Two_Pointers, Udemy_Two_Pointers, Top_Interview_150_Array/String | 0 | 100.00
16911707
| 0025 |[Reverse Nodes in k-Group](src/main/python/g0001_0100/s0025_reverse_nodes_in_k_group/Solution0025.py)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# #Easy #Top_Interview_Questions #String #Two_Pointers #String_Matching
2+
# #Programming_Skills_II_Day_1 #Top_Interview_150_Array/String
3+
# #2025_09_13_Time_0_ms_(100.00%)_Space_12.36_MB_(81.86%)
4+
5+
class Solution:
6+
def strStr(self, haystack: str, needle: str) -> int:
7+
if not needle:
8+
return 0
9+
m = len(haystack)
10+
n = len(needle)
11+
for start in range(m - n + 1):
12+
if haystack[start:start + n] == needle:
13+
return start
14+
return -1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0028 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_strStr(self):
6+
self.assertEqual(Solution().strStr("hello", "ll"), 2)
7+
8+
def test_strStr2(self):
9+
self.assertEqual(Solution().strStr("hello", ""), 0)
10+
11+
def test_strStr3(self):
12+
self.assertEqual(Solution().strStr("hello", "oo"), -1)
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# #Hard #String #Hash_Table #Sliding_Window #Top_Interview_150_Sliding_Window
2+
# #2025_09_13_Time_20_ms_(96.85%)_Space_13.11_MB_(74.97%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
8+
ans = []
9+
n1 = len(words[0])
10+
n2 = len(s)
11+
map1 = {}
12+
for word in words:
13+
map1[word] = map1.get(word, 0) + 1
14+
15+
for i in range(n1):
16+
left = i
17+
j = i
18+
c = 0
19+
map2 = {}
20+
while j + n1 <= n2:
21+
word1 = s[j:j + n1]
22+
j += n1
23+
if word1 in map1:
24+
map2[word1] = map2.get(word1, 0) + 1
25+
c += 1
26+
while map2[word1] > map1[word1]:
27+
word2 = s[left:left + n1]
28+
map2[word2] = map2.get(word2, 0) - 1
29+
left += n1
30+
c -= 1
31+
if c == len(words):
32+
ans.append(left)
33+
else:
34+
map2.clear()
35+
c = 0
36+
left = j
37+
38+
return ans
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from Solution0030 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_findSubstring(self):
6+
self.assertEqual(
7+
Solution().findSubstring("barfoothefoobarman", ["foo", "bar"]),
8+
[0, 9]
9+
)
10+
11+
def test_findSubstring2(self):
12+
self.assertEqual(
13+
Solution().findSubstring(
14+
"wordgoodgoodgoodbestword", ["word", "good", "best", "word"]
15+
),
16+
[]
17+
)
18+
19+
def test_findSubstring3(self):
20+
self.assertEqual(
21+
Solution().findSubstring(
22+
"barfoofoobarthefoobarman", ["bar", "foo", "the"]
23+
),
24+
[6, 9, 12]
25+
)
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# #Medium #Top_Interview_Questions #Array #Hash_Table #Matrix #Data_Structure_I_Day_5_Array
2+
# #Top_Interview_150_Matrix #2025_09_13_Time_5_ms_(72.85%)_Space_12.40_MB_(54.10%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def __init__(self):
8+
self.j1 = 0
9+
self.i1 = [0] * 9
10+
self.b1 = [0] * 9
11+
12+
def isValidSudoku(self, board: List[List[str]]) -> bool:
13+
for i in range(9):
14+
for j in range(9):
15+
res = self._checkValid(board, i, j)
16+
if not res:
17+
return False
18+
return True
19+
20+
def _checkValid(self, board: List[List[str]], i: int, j: int) -> bool:
21+
if j == 0:
22+
self.j1 = 0
23+
if board[i][j] == '.':
24+
return True
25+
val = int(board[i][j])
26+
if self.j1 == (self.j1 | (1 << (val - 1))):
27+
return False
28+
self.j1 |= 1 << (val - 1)
29+
if self.i1[j] == (self.i1[j] | (1 << (val - 1))):
30+
return False
31+
self.i1[j] |= 1 << (val - 1)
32+
b = (i // 3) * 3 + j // 3
33+
if self.b1[b] == (self.b1[b] | (1 << (val - 1))):
34+
return False
35+
self.b1[b] |= 1 << (val - 1)
36+
return True

0 commit comments

Comments
 (0)