Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 []



Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque

class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
"""BFS approach - explores island level by level"""

if not grid:
return 0

rows, cols = len(grid), len(grid[0])
islands = 0

def bfs(r: int, c: int):
queue = deque([(r,c)])
grid[r][c] = '0'

while queue:
row, col = queue.popleft()

for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]:
nr, nc = row + dr, col + dc
if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'):
grid[nr][nc] = '0'
queue.append((nr,nc))

for r in range(rows):
for c in range(cols):
if grid[r][c] == '1':
islands += 1
bfs(r,c)

return islands


def numIslands_DFS(self, grid: List[List[str]]) -> int:
"""DFS approach - explores island depth-first"""

if not grid:
return 0

rows, cols = len(grid), len(grid[0])
islands = 0

def dfs(r: int, c: int):
if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'):
return

grid[r][c] = '0'

dfs(r-1,c)
dfs(r+1,c)
dfs(r,c-1)
dfs(r,c+1)

for r in range(rows):
for c in range(cols):
if grid[r][c] == '1':
islands += 1
dfs(r,c)

return islands

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let m: number, n: number
function numIslands(grid: string[][]): number {
let count: number = 0
if (!grid || !grid[0]) return count
m = grid.length
n = grid[0].length
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === "1") {
count++
dfs(grid, i, j)
}
}
}
return count
};

function dfs(grid: string[][], i: number, j: number) {
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] === '0') return
grid[i][j] = '0'
dfs(grid, i + 1, j)
dfs(grid, i - 1, j)
dfs(grid, i, j - 1)
dfs(grid, i, j + 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_84_number_of_islands import Solution

class NumberOfIslandsTescaseCase(unittest.TestCase):

def test_example_1(self):
# Example 1: Single island
# Input: grid = [
# ["1","1","1","1","0"],
# ["1","1","0","1","0"],
# ["1","1","0","0","0"],
# ["0","0","0","0","0"]
# ]
# Output: 1
solution = Solution()
grid = [
["1", "1", "1", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"]
]
result = solution.numIslands(grid)
self.assertEqual(result, 1)

def test_example_2(self):
# Example 2: Three islands
# Input: grid = [
# ["1","1","0","0","0"],
# ["1","1","0","0","0"],
# ["0","0","1","0","0"],
# ["0","0","0","1","1"]
# ]
# Output: 3
solution = Solution()
grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"]
]
result = solution.numIslands(grid)
self.assertEqual(result, 3)

def test_example_1_dfs(self):
# Example 1: Single island (DFS approach)
# Input: grid = [
# ["1","1","1","1","0"],
# ["1","1","0","1","0"],
# ["1","1","0","0","0"],
# ["0","0","0","0","0"]
# ]
# Output: 1
solution = Solution()
grid = [
["1", "1", "1", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"]
]
result = solution.numIslands_DFS(grid)
self.assertEqual(result, 1)

def test_example_2_dfs(self):
# Example 2: Three islands (DFS approach)
# Input: grid = [
# ["1","1","0","0","0"],
# ["1","1","0","0","0"],
# ["0","0","1","0","0"],
# ["0","0","0","1","1"]
# ]
# Output: 3
solution = Solution()
grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"]
]
result = solution.numIslands_DFS(grid)
self.assertEqual(result, 3)