Skip to content

Commit 309aa52

Browse files
authored
Merge pull request #1486 from ivan1016017/december15
adding algo
2 parents 2e4932a + 57a5aa7 commit 309aa52

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import Counter
4+
5+
class Solution:
6+
def minWindow(self, s: str, t: str) -> str:
7+
if not s or not t or len(s) < len(t):
8+
return ""
9+
10+
# Frequency map of characters needed from t
11+
t_freq = Counter(t)
12+
required = len(t_freq) # Number of unique characters in t
13+
14+
# Window character frequencies
15+
window_freq = {}
16+
17+
# Track how many unique characters in window have desired frequency
18+
formed = 0
19+
20+
# Left and right pointers
21+
left = 0
22+
min_len = float('inf')
23+
min_left = 0
24+
25+
for right in range(len(s)):
26+
# Add character from right to window
27+
char = s[right]
28+
window_freq[char] = window_freq.get(char, 0) + 1
29+
30+
# Check if frequency of current character matches desired frequency
31+
if char in t_freq and window_freq[char] == t_freq[char]:
32+
formed += 1
33+
34+
# Try to contract window until it's no longer valid
35+
while left <= right and formed == required:
36+
# Update result if current window is smaller
37+
if right - left + 1 < min_len:
38+
min_len = right - left + 1
39+
min_left = left
40+
41+
# Remove leftmost character from window
42+
left_char = s[left]
43+
window_freq[left_char] -= 1
44+
45+
# Check if window is no longer valid
46+
if left_char in t_freq and window_freq[left_char] < t_freq[left_char]:
47+
formed -= 1
48+
49+
left += 1
50+
51+
return "" if min_len == float('inf') else s[min_left:min_left + min_len]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def isValidSudoku(self, board: List[List[str]]):
6+
return (self.is_row_valid(board) and
7+
self.is_col_valid(board) and
8+
self.is_square_valid(board))
9+
10+
def is_row_valid(self, board: List[List[str]]):
11+
for row in board:
12+
if not self.is_unit_valid(row):
13+
return False
14+
return True
15+
16+
def is_col_valid(self, board: List[List[str]]):
17+
for col in zip(*board):
18+
if not self.is_unit_valid(col):
19+
return False
20+
return True
21+
22+
def is_square_valid(self, board: List[List[str]]):
23+
for i in (0, 3, 6):
24+
for j in (0, 3, 6):
25+
square = [board[x][y] for x in range(i, i + 3) for y in range(j, j + 3)]
26+
if not self.is_unit_valid(square):
27+
return False
28+
return True
29+
30+
def is_unit_valid(self, unit):
31+
unit = [i for i in unit if i != '.']
32+
return len(set(unit)) == len(unit)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_33_minimum_window_substring import Solution
4+
5+
class MinimumWindowSubstringTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.minWindow(s = "ADOBECODEBANC", t = "ABC")
10+
target = "BANC"
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.minWindow(s = "a", t = "a")
16+
target = "a"
17+
self.assertEqual(output, target)
18+
19+
def test_third_pattern(self):
20+
solution = Solution()
21+
output = solution.minWindow(s = "a", t = "aa")
22+
target = ""
23+
self.assertEqual(output, target)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_34_valid_sudoku import Solution
4+
5+
class ValidSudokuTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.isValidSudoku(board =
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+
self.assertTrue(output)
20+
21+
def test_second_pattern(self):
22+
solution = Solution()
23+
output = solution.isValidSudoku(board =
24+
[["8","3",".",".","7",".",".",".","."]
25+
,["6",".",".","1","9","5",".",".","."]
26+
,[".","9","8",".",".",".",".","6","."]
27+
,["8",".",".",".","6",".",".",".","3"]
28+
,["4",".",".","8",".","3",".",".","1"]
29+
,["7",".",".",".","2",".",".",".","6"]
30+
,[".","6",".",".",".",".","2","8","."]
31+
,[".",".",".","4","1","9",".",".","5"]
32+
,[".",".",".",".","8",".",".","7","9"]])
33+
self.assertFalse(output)

0 commit comments

Comments
 (0)