Skip to content

[Chaedie] Week 15 #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
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
40 changes: 40 additions & 0 deletions longest-palindromic-substring/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Solution:
1) 포문을 돌면서 좌우 투포인터로 벌려주며 같은 문자인지 확인한다. 같으면 팰린드롬, 아니면 break
2) 홀수, 짝수를 별도로 순회한다.

Time: O(n^2)
Space: O(1)
"""

class Solution:
def longestPalindrome(self, s: str) -> str:
result = s[0]

for i in range(len(s)):
# check odd
word = s[i]
left, right = i - 1, i + 1
while left >= 0 and right < len(s):
if s[left] == s[right]:
word = s[left] + word + s[right]
if len(result) < len(word):
result = word
left -= 1
right += 1
else:
break

word = ""
left, right = i, i + 1
while left >= 0 and right < len(s):
if s[left] == s[right]:
word = s[left] + word + s[right]
if len(result) < len(word):
result = word
left -= 1
right += 1
else:
break

return result
32 changes: 32 additions & 0 deletions rotate-image/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Solution: 1) 가장 바깥에서부터 안쪽으로 들어오며 4 원소의 자리를 바꿔준다.
Time: O(n^2)
Space: O(1)
"""


class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
ROWS, COLS = len(matrix), len(matrix[0])


l, r = 0, COLS - 1
t, b = 0, ROWS - 1
while l < r and t < b:
for i in range(r - l):
[
matrix[t][l + i],
matrix[t + i][r],
matrix[b][r - i],
matrix[b - i][l]
] = [
matrix[b - i][l],
matrix[t][l + i],
matrix[t + i][r],
matrix[b][r - i]
]

l += 1
r -= 1
t += 1
b -= 1
25 changes: 25 additions & 0 deletions subtree-of-another-tree/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not subRoot: return True
if not root: return False

if self.sameTree(root, subRoot):
return True

return (self.isSubtree(root.left, subRoot) or
self.isSubtree(root.right, subRoot))

def sameTree(self, s, t):
if not s and not t:
return True

if s and t and s.val == t.val:
return (self.sameTree(s.left, t.left) and
self.sameTree(s.right, t.right))
return False