Skip to content

Commit 0974a26

Browse files
authored
Merge pull request #2337 from ppxyn1/main
[ppxyn1] WEEK 15 solutions
2 parents 42340af + c25ac8a commit 0974a26

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#idea: -
2+
#Time Complexity: O(n^2)
3+
class Solution:
4+
def longestPalindrome(self, s: str) -> str:
5+
ans = ''
6+
def check_func(l, r):
7+
while l >= 0 and r < len(s):
8+
if s[l] != s[r]:
9+
break
10+
l -= 1
11+
r += 1
12+
return s[l+1:r]
13+
14+
for i in range(len(s)):
15+
odd = check_func(i, i)
16+
even = check_func(i, i+1)
17+
if len(odd) > len(even):
18+
longer = odd
19+
else:
20+
longer = even
21+
22+
if len(longer) > len(ans):
23+
ans = longer
24+
25+
return ans
26+
27+

rotate-image/ppxyn1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# idea: -
2+
# Time Complexity: O(n^2)
3+
4+
'''
5+
1 2 3
6+
4 5 6
7+
7 8 9
8+
'''
9+
class Solution:
10+
def rotate(self, matrix: List[List[int]]) -> None:
11+
"""
12+
Do not return anything, modify matrix in-place instead.
13+
"""
14+
n = len(matrix)
15+
# Transpose (diagonal elements are fixed)
16+
'''
17+
1 4 7
18+
2 5 8
19+
3 6 9
20+
'''
21+
for i in range(n):
22+
for j in range(i+1,n):
23+
matrix[i][j] , matrix[j][i] = matrix[j][i], matrix[i][j]
24+
25+
# Reverse each row
26+
'''
27+
7 4 1
28+
8 5 2
29+
9 6 3
30+
'''
31+
for r in matrix:
32+
r.reverse()

subtree-of-another-tree/ppxyn1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
9+
# idea : BFS
10+
# Time Complexity : O(size of root * size of subRoot)
11+
from collections import deque
12+
13+
class Solution:
14+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
15+
queue = deque([root])
16+
17+
while queue:
18+
node = queue.popleft()
19+
20+
if node:
21+
if self.isSame(node, subRoot):
22+
return True
23+
queue.append(node.left)
24+
queue.append(node.right)
25+
26+
return False
27+
28+
def isSame(self, r, s):
29+
if not r and not s:
30+
return True
31+
if not r or not s:
32+
return False
33+
if r.val != s.val:
34+
return False
35+
return self.isSame(r.left, s.left) and self.isSame(r.right, s.right)
36+

0 commit comments

Comments
 (0)