diff --git a/clone-graph/daehyun99.py b/clone-graph/daehyun99.py new file mode 100644 index 0000000000..197c5c28d3 --- /dev/null +++ b/clone-graph/daehyun99.py @@ -0,0 +1,33 @@ +# Time: O(n) +# Space: O(n) +""" +# Definition for a Node. +class Node: + def __init__(self, val = 0, neighbors = None): + self.val = val + self.neighbors = neighbors if neighbors is not None else [] +""" +from typing import Optional +class Solution: + def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: + have_to_look = set() + seen = set() + copied = {} + + have_to_look.add(node) + + while len(have_to_look) > 0 : + curr = have_to_look.pop() + if curr is not None: + if curr.val not in copied: + copied[curr.val] = Node(curr.val, None) + for neighbor in curr.neighbors: + if neighbor.val not in copied: + copied[neighbor.val] = Node(neighbor.val, None) + if neighbor.val not in seen: + have_to_look.add(neighbor) + copied[curr.val].neighbors.append(copied[neighbor.val]) + seen.add(curr.val) + + return copied.get(1, None) + diff --git a/longest-repeating-character-replacement/daehyun99.py b/longest-repeating-character-replacement/daehyun99.py new file mode 100644 index 0000000000..e23a855a5c --- /dev/null +++ b/longest-repeating-character-replacement/daehyun99.py @@ -0,0 +1,59 @@ +# Time: O(s) +# Space: O(s) +from collections import defaultdict +class Solution: + def characterReplacement(self, s: str, k: int) -> int: + count = defaultdict(int) + + l = 0 + maxf = 0 + res = 0 + for r in range(len(s)): + count[s[r]] += 1 + maxf = max(maxf, count[s[r]]) + + while (r - l + 1) - maxf > k: + count[s[l]] -= 1 + l += 1 + res = max(res, r - l + 1) + return res + +""" +# Time: O(s) +# Space: O(s) +class Solution: + def characterReplacement(self, s: str, k: int) -> int: + # find_bunch() + bunch = [] + start_idx = 0 + start_word = s[0] + for i in range(1, len(s)): + if s[i] != start_word: + bunch.append([start_word, i- start_idx]) + start_word = s[i] + start_idx = i + bunch.append([start_word, len(s) - start_idx]) + + # find_LRCR() + unique = set([c for c in s]) + result = 0 + + for base in unique: + changed_num = 0 + left = 0 + right = 0 + length = 0 + while right < len(bunch): + if bunch[right][0] != base: + changed_num += bunch[right][1] + length += bunch[right][1] + right += 1 + + while changed_num > k: + if bunch[left][0] != base: + changed_num -= bunch[left][1] + length -= bunch[left][1] + left += 1 + result = max(result, min(length + k - changed_num, len(s))) + return result +""" diff --git a/palindromic-substrings/daehyun99.py b/palindromic-substrings/daehyun99.py new file mode 100644 index 0000000000..7177202082 --- /dev/null +++ b/palindromic-substrings/daehyun99.py @@ -0,0 +1,23 @@ +class Solution: + def countSubstrings(self, s: str) -> int: + result = 0 + + # odd + for i in range(0, len(s)): + m, n = i, i + while 0 <= m and n < len(s) and s[m] == s[n]: + result += 1 + m -= 1 + n += 1 + + # even + for i in range(0, len(s)-1): + m, n = i, i+1 + while 0 <= m and n < len(s) and s[m] == s[n]: + result += 1 + m -= 1 + n += 1 + return result + + + diff --git a/reverse-bits/daehyun99.py b/reverse-bits/daehyun99.py new file mode 100644 index 0000000000..21ef934a10 --- /dev/null +++ b/reverse-bits/daehyun99.py @@ -0,0 +1,7 @@ +class Solution: + def reverseBits(self, n: int) -> int: + res = 0 + for i in range(32): + bit = (n >> i) & 1 + res += (bit << (31 - i)) + return res