diff --git a/longest-palindromic-substring/ppxyn1.py b/longest-palindromic-substring/ppxyn1.py new file mode 100644 index 0000000000..d64787c6df --- /dev/null +++ b/longest-palindromic-substring/ppxyn1.py @@ -0,0 +1,27 @@ +#idea: - +#Time Complexity: O(n^2) +class Solution: + def longestPalindrome(self, s: str) -> str: + ans = '' + def check_func(l, r): + while l >= 0 and r < len(s): + if s[l] != s[r]: + break + l -= 1 + r += 1 + return s[l+1:r] + + for i in range(len(s)): + odd = check_func(i, i) + even = check_func(i, i+1) + if len(odd) > len(even): + longer = odd + else: + longer = even + + if len(longer) > len(ans): + ans = longer + + return ans + + diff --git a/subtree-of-another-tree/ppxyn1.py b/subtree-of-another-tree/ppxyn1.py new file mode 100644 index 0000000000..2e45936fb7 --- /dev/null +++ b/subtree-of-another-tree/ppxyn1.py @@ -0,0 +1,36 @@ +# 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 + + +# idea : BFS +# Time Complexity : O(size of root * size of subRoot) +from collections import deque + +class Solution: + def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + queue = deque([root]) + + while queue: + node = queue.popleft() + + if node: + if self.isSame(node, subRoot): + return True + queue.append(node.left) + queue.append(node.right) + + return False + + def isSame(self, r, s): + if not r and not s: + return True + if not r or not s: + return False + if r.val != s.val: + return False + return self.isSame(r.left, s.left) and self.isSame(r.right, s.right) +