diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_33_minimum_window_substring.py b/src/my_project/interviews/top_150_questions_round_22/ex_33_minimum_window_substring.py new file mode 100644 index 00000000..6010d974 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_33_minimum_window_substring.py @@ -0,0 +1,51 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import Counter + +class Solution: + def minWindow(self, s: str, t: str) -> str: + if not s or not t or len(s) < len(t): + return "" + + # Frequency map of characters needed from t + t_freq = Counter(t) + required = len(t_freq) # Number of unique characters in t + + # Window character frequencies + window_freq = {} + + # Track how many unique characters in window have desired frequency + formed = 0 + + # Left and right pointers + left = 0 + min_len = float('inf') + min_left = 0 + + for right in range(len(s)): + # Add character from right to window + char = s[right] + window_freq[char] = window_freq.get(char, 0) + 1 + + # Check if frequency of current character matches desired frequency + if char in t_freq and window_freq[char] == t_freq[char]: + formed += 1 + + # Try to contract window until it's no longer valid + while left <= right and formed == required: + # Update result if current window is smaller + if right - left + 1 < min_len: + min_len = right - left + 1 + min_left = left + + # Remove leftmost character from window + left_char = s[left] + window_freq[left_char] -= 1 + + # Check if window is no longer valid + if left_char in t_freq and window_freq[left_char] < t_freq[left_char]: + formed -= 1 + + left += 1 + + return "" if min_len == float('inf') else s[min_left:min_left + min_len] \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_34_valid_sudoku.py b/src/my_project/interviews/top_150_questions_round_22/ex_34_valid_sudoku.py new file mode 100644 index 00000000..3a6748d4 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_34_valid_sudoku.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isValidSudoku(self, board: List[List[str]]): + return (self.is_row_valid(board) and + self.is_col_valid(board) and + self.is_square_valid(board)) + + def is_row_valid(self, board: List[List[str]]): + for row in board: + if not self.is_unit_valid(row): + return False + return True + + def is_col_valid(self, board: List[List[str]]): + for col in zip(*board): + if not self.is_unit_valid(col): + return False + return True + + def is_square_valid(self, board: List[List[str]]): + for i in (0, 3, 6): + for j in (0, 3, 6): + square = [board[x][y] for x in range(i, i + 3) for y in range(j, j + 3)] + if not self.is_unit_valid(square): + return False + return True + + def is_unit_valid(self, unit): + unit = [i for i in unit if i != '.'] + return len(set(unit)) == len(unit) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_33_minimum_window_substring_round_22.py b/tests/test_150_questions_round_22/test_33_minimum_window_substring_round_22.py new file mode 100644 index 00000000..0633b80b --- /dev/null +++ b/tests/test_150_questions_round_22/test_33_minimum_window_substring_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_33_minimum_window_substring import Solution + +class MinimumWindowSubstringTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.minWindow(s = "ADOBECODEBANC", t = "ABC") + target = "BANC" + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.minWindow(s = "a", t = "a") + target = "a" + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.minWindow(s = "a", t = "aa") + target = "" + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_34_valid_sudoku_round_22.py b/tests/test_150_questions_round_22/test_34_valid_sudoku_round_22.py new file mode 100644 index 00000000..083e11ad --- /dev/null +++ b/tests/test_150_questions_round_22/test_34_valid_sudoku_round_22.py @@ -0,0 +1,33 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_34_valid_sudoku import Solution + +class ValidSudokuTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.isValidSudoku(board = +[["5","3",".",".","7",".",".",".","."] +,["6",".",".","1","9","5",".",".","."] +,[".","9","8",".",".",".",".","6","."] +,["8",".",".",".","6",".",".",".","3"] +,["4",".",".","8",".","3",".",".","1"] +,["7",".",".",".","2",".",".",".","6"] +,[".","6",".",".",".",".","2","8","."] +,[".",".",".","4","1","9",".",".","5"] +,[".",".",".",".","8",".",".","7","9"]]) + self.assertTrue(output) + + def test_second_pattern(self): + solution = Solution() + output = solution.isValidSudoku(board = +[["8","3",".",".","7",".",".",".","."] +,["6",".",".","1","9","5",".",".","."] +,[".","9","8",".",".",".",".","6","."] +,["8",".",".",".","6",".",".",".","3"] +,["4",".",".","8",".","3",".",".","1"] +,["7",".",".",".","2",".",".",".","6"] +,[".","6",".",".",".",".","2","8","."] +,[".",".",".","4","1","9",".",".","5"] +,[".",".",".",".","8",".",".","7","9"]]) + self.assertFalse(output)