Skip to content

Commit 6cf010a

Browse files
authored
Merge pull request #1679 from ivanpenaloza/june25
addin updates
2 parents 91494eb + 20415a8 commit 6cf010a

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from collections import deque
3+
4+
class Solution:
5+
def numIslands(self, grid: List[List[str]]) -> int:
6+
"""BFS approach - explores island level by level"""
7+
8+
if not grid:
9+
return 0
10+
11+
rows, cols = len(grid), len(grid[0])
12+
islands = 0
13+
14+
def bfs(r: int, c: int):
15+
queue = deque([(r,c)])
16+
grid[r][c] = '0'
17+
18+
while queue:
19+
row, col = queue.popleft()
20+
21+
for dr, dc in [(1,0),(-1,0),(0,1),(0,-1)]:
22+
nr, nc = row + dr, col + dc
23+
if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'):
24+
grid[nr][nc] = '0'
25+
queue.append((nr, nc))
26+
27+
for r in range(rows):
28+
for c in range(cols):
29+
if grid[r][c] == '1':
30+
islands += 1
31+
bfs(r,c)
32+
33+
return islands
34+
35+
36+
def numIslands_DFS(grid: List[List[str]]) -> int:
37+
"""DFS approach - explores island depth-first"""
38+
39+
pass
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True

0 commit comments

Comments
 (0)