From 82329a0ee67f960465c7f013ea0ae2026e9028be Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 6 Dec 2025 12:36:45 +0900 Subject: [PATCH 1/5] counting-bits --- counting-bits/chjung99.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 counting-bits/chjung99.java diff --git a/counting-bits/chjung99.java b/counting-bits/chjung99.java new file mode 100644 index 0000000000..40113cb393 --- /dev/null +++ b/counting-bits/chjung99.java @@ -0,0 +1,31 @@ +// time: O(n) +// space: O(n) + +class Solution { + public int[] countBits(int n) { + int[] ans = new int[n+1]; + + if (n >= 1){ + ans[0] = 0; + ans[1] = 1; + } + int fac = 2; + int prev = 1; + + for (int i = 2; i <= n; i++){ + if (i == fac){ + prev = fac; + fac *= 2; + + ans[i] = 1; + continue; + } + + ans[i] = 1+ ans[i-prev]; + } + + return ans; + } +} + + From 3cfa003ab6ec82c4980653ff91acd77c5d43adc3 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 6 Dec 2025 12:36:56 +0900 Subject: [PATCH 2/5] longest-palindromic-substring --- longest-palindromic-substring/chjung99.java | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 longest-palindromic-substring/chjung99.java diff --git a/longest-palindromic-substring/chjung99.java b/longest-palindromic-substring/chjung99.java new file mode 100644 index 0000000000..6148b22022 --- /dev/null +++ b/longest-palindromic-substring/chjung99.java @@ -0,0 +1,43 @@ +// brute force +// time: O(N^3) +// space: O(N) + +class Solution { + public String longestPalindrome(String s) { + String answer = ""; + int maxLen = 0; + int sLen = s.length(); + + for (int i = 0; i < sLen; i++){ + for (int j = i ; j < sLen; j++){ + String substring = s.substring(i, j+1); + if (isPalindrom(substring) && maxLen < (j-i+1)){ + maxLen = (j-i+1); + answer = substring; + } + } + } + + return answer; + } + + public boolean isPalindrom(String s){ + int start = 0; + int end = s.length()-1; + + + while(start < end) { + if (s.charAt(start) == s.charAt(end)){ + start++; + end--; + } else { + return false; + } + } + + return true; + } + +} + + From 917d3a128178d97afdeb230e8568571cae07249f Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 6 Dec 2025 12:37:11 +0900 Subject: [PATCH 3/5] longest-substring-without-repeating-characters --- .../chjung99.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-substring-without-repeating-characters/chjung99.java diff --git a/longest-substring-without-repeating-characters/chjung99.java b/longest-substring-without-repeating-characters/chjung99.java new file mode 100644 index 0000000000..9d78efad08 --- /dev/null +++ b/longest-substring-without-repeating-characters/chjung99.java @@ -0,0 +1,23 @@ +// two pointer +// time: O(N) +// space: O(N) +class Solution { + public int lengthOfLongestSubstring(String s) { + int answer = 0; + Set charSet = new HashSet<>(); + int head = 0; + int tail = 0; + + for (int i = 0; i < s.length(); i++){ + Character ch = Character.valueOf(s.charAt(i)); + while (charSet.contains(ch)) { + charSet.remove(Character.valueOf(s.charAt(head++))); + } + charSet.add(ch); + tail += 1; + answer = Math.max(answer, charSet.size()); + } + return answer; + } +} + From 8fc63d3fa90b9c6b487fa5340550f163add83991 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 6 Dec 2025 12:37:24 +0900 Subject: [PATCH 4/5] missing-number --- missing-number/chjung99.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 missing-number/chjung99.java diff --git a/missing-number/chjung99.java b/missing-number/chjung99.java new file mode 100644 index 0000000000..e7636acb8b --- /dev/null +++ b/missing-number/chjung99.java @@ -0,0 +1,17 @@ +// time: O(N) +// space: O(1) + +class Solution { + public int missingNumber(int[] nums) { + int maxValue = 0; + int sum = 0; + for (int i = 0; i < nums.length; i++){ + if (maxValue < nums[i]) maxValue = nums[i]; + sum += nums[i]; + } + int n = Math.max(nums.length, maxValue); + + return (int) (n * (n + 1))/2 - sum; + } +} + From 25ab8898038f2ad3cc6e051f645f595744832c73 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 6 Dec 2025 12:37:35 +0900 Subject: [PATCH 5/5] subtree-of-another-tree --- subtree-of-another-tree/chjung99.java | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 subtree-of-another-tree/chjung99.java diff --git a/subtree-of-another-tree/chjung99.java b/subtree-of-another-tree/chjung99.java new file mode 100644 index 0000000000..c0b131ea33 --- /dev/null +++ b/subtree-of-another-tree/chjung99.java @@ -0,0 +1,70 @@ +// time: O(N^2) +// space: O(N) + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSubtree(TreeNode root, TreeNode subRoot) { + Deque deque = new ArrayDeque<>(); + deque.add(root); + + while (!deque.isEmpty()){ + TreeNode cur = deque.poll(); + + if (isEqual(cur, subRoot)) { + return true; + } + + if (cur.left != null) deque.add(cur.left); + if (cur.right != null) deque.add(cur.right); + } + return false; + } + + public boolean isEqual(TreeNode x, TreeNode y){ + Deque dequeX = new ArrayDeque<>(); + Deque dequeY = new ArrayDeque<>(); + + dequeX.add(x); + dequeY.add(y); + + while (!dequeX.isEmpty()||!dequeY.isEmpty()){ + TreeNode curX = dequeX.poll(); + TreeNode curY = dequeY.poll(); + + if (curX.val != curY.val) return false; + + if (curX.left != null && curY.left != null){ + dequeX.add(curX.left); + dequeY.add(curY.left); + } else if (curX.left == null && curY.left == null){ + ; + } else{ + return false; + } + if (curX.right != null && curY.right != null){ + dequeX.add(curX.right); + dequeY.add(curY.right); + } else if (curX.right == null && curY.right == null){ + ; + } else{ + return false; + } + } + return (dequeX.isEmpty() && dequeY.isEmpty()); + } +} +