diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py index 534c7ec3..c7f907c0 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py @@ -12,5 +12,10 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: return [answer[v], k] else: answer[target - v] = k + + return [] + + + - return [] \ No newline at end of file + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.py deleted file mode 100644 index dc28488b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py deleted file mode 100644 index a7ecea5f..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py +++ /dev/null @@ -1,19 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py deleted file mode 100644 index dd1b1232..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py deleted file mode 100644 index e03fe46f..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py deleted file mode 100644 index e03fe46f..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py deleted file mode 100644 index edaea7a4..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py +++ /dev/null @@ -1,17 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py deleted file mode 100644 index fbc8a524..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py deleted file mode 100644 index dc28488b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py deleted file mode 100644 index 0f997fd2..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py +++ /dev/null @@ -1,19 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py deleted file mode 100644 index 6dda1665..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] - - - - - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py index 749791eb..fa21021b 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py @@ -17,7 +17,6 @@ def isPalindrome(self, s: str) -> bool: for i in range(len_s//2): if s[i] != s[len_s - 1 - i]: - return False + return False return True - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.py deleted file mode 100644 index 49e72b81..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py deleted file mode 100644 index 197be03f..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py deleted file mode 100644 index 63acf7c5..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py deleted file mode 100644 index 276891bf..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py deleted file mode 100644 index 8bfb7f9e..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py deleted file mode 100644 index c21f9762..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py deleted file mode 100644 index f1320ee0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py deleted file mode 100644 index cf306d3e..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py deleted file mode 100644 index d2f5d34b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True - diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py deleted file mode 100644 index 49e72b81..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - - # Determine if s is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True - diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_85_surrounded_regions.py b/src/my_project/interviews/top_150_questions_round_22/ex_85_surrounded_regions.py new file mode 100644 index 00000000..ce8a6ad6 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_85_surrounded_regions.py @@ -0,0 +1,51 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque + + +class Solution: + def solve(self, board: List[List[str]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + if not board or not board[0]: + return + + m, n = len(board), len(board[0]) + + # BFS to mark all 'O's connected to border + def bfs(row: int, col: int) -> None: + queue = deque([(row, col)]) + board[row][col] = 'T' # Temporary marker for safe 'O's + + while queue: + r, c = queue.popleft() + # Check all 4 directions + for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]: + nr, nc = r + dr, c + dc + if 0 <= nr < m and 0 <= nc < n and board[nr][nc] == 'O': + board[nr][nc] = 'T' + queue.append((nr, nc)) + + # Step 1: Mark all border-connected 'O's + # Check first and last row + for col in range(n): + if board[0][col] == 'O': + bfs(0, col) + if board[m-1][col] == 'O': + bfs(m-1, col) + + # Check first and last column + for row in range(m): + if board[row][0] == 'O': + bfs(row, 0) + if board[row][n-1] == 'O': + bfs(row, n-1) + + # Step 2: Flip all remaining 'O's to 'X' and restore 'T' back to 'O' + for i in range(m): + for j in range(n): + if board[i][j] == 'O': + board[i][j] = 'X' # Surrounded region + elif board[i][j] == 'T': + board[i][j] = 'O' # Border-connected, restore \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_85_surrounded_regions.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_85_surrounded_regions.ts new file mode 100644 index 00000000..d0dcf830 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_85_surrounded_regions.ts @@ -0,0 +1,63 @@ +function solve(board: string[][]): void { + /** + * Do not return anything, modify board in-place instead. + */ + if (!board || !board[0]) { + return; + } + + const m = board.length; + const n = board[0].length; + + // BFS to mark all 'O's connected to border + function bfs(row: number, col: number): void { + const queue: [number, number][] = [[row, col]]; + board[row][col] = 'T'; // Temporary marker for safe 'O's + + while (queue.length > 0) { + const [r, c] = queue.shift()!; + // Check all 4 directions + const directions: [number, number][] = [[0, 1], [0, -1], [1, 0], [-1, 0]]; + for (const [dr, dc] of directions) { + const nr = r + dr; + const nc = c + dc; + if (nr >= 0 && nr < m && nc >= 0 && nc < n && board[nr][nc] === 'O') { + board[nr][nc] = 'T'; + queue.push([nr, nc]); + } + } + } + } + + // Step 1: Mark all border-connected 'O's + // Check first and last row + for (let col = 0; col < n; col++) { + if (board[0][col] === 'O') { + bfs(0, col); + } + if (board[m - 1][col] === 'O') { + bfs(m - 1, col); + } + } + + // Check first and last column + for (let row = 0; row < m; row++) { + if (board[row][0] === 'O') { + bfs(row, 0); + } + if (board[row][n - 1] === 'O') { + bfs(row, n - 1); + } + } + + // Step 2: Flip all remaining 'O's to 'X' and restore 'T' back to 'O' + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === 'O') { + board[i][j] = 'X'; // Surrounded region + } else if (board[i][j] === 'T') { + board[i][j] = 'O'; // Border-connected, restore + } + } + } +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_85_surrounded_regions_round_22.py b/tests/test_150_questions_round_22/test_85_surrounded_regions_round_22.py new file mode 100644 index 00000000..6e255ae8 --- /dev/null +++ b/tests/test_150_questions_round_22/test_85_surrounded_regions_round_22.py @@ -0,0 +1,34 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_85_surrounded_regions import Solution + +class SurroundedRegionsTestCase(unittest.TestCase): + + def test_example_1(self): + # Example 1: Board with surrounded regions + # The O's in the center should be captured (converted to X) + # The O on the bottom edge should not be captured + solution = Solution() + board = [ + ["X", "X", "X", "X"], + ["X", "O", "O", "X"], + ["X", "X", "O", "X"], + ["X", "O", "X", "X"] + ] + expected = [ + ["X", "X", "X", "X"], + ["X", "X", "X", "X"], + ["X", "X", "X", "X"], + ["X", "O", "X", "X"] + ] + solution.solve(board) + self.assertEqual(board, expected) + + def test_example_2(self): + # Example 2: Single cell board + solution = Solution() + board = [["X"]] + expected = [["X"]] + solution.solve(board) + self.assertEqual(board, expected) +