diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/TomyKim9401.java b/construct-binary-tree-from-preorder-and-inorder-traversal/TomyKim9401.java new file mode 100644 index 000000000..afd4d86ee --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/TomyKim9401.java @@ -0,0 +1,23 @@ +class Solution { + private int i, p; + public TreeNode buildTree(int[] preorder, int[] inorder) { + // Time complexity: O(n) + // Space complexity: O(n) + return builder(preorder, inorder, Integer.MIN_VALUE); + } + + private TreeNode builder(int[] preorder, int[] inorder, int stop) { + if (p >= preorder.length) return null; + if (inorder[i] == stop) { + i += 1; + return null; + } + + TreeNode node = new TreeNode(preorder[p]); + p += 1; + + node.left = builder(preorder, inorder, node.val); + node.right = builder(preorder, inorder, stop); + return node; + } +} diff --git a/counting-bits/TonyKim9401.java b/counting-bits/TonyKim9401.java new file mode 100644 index 000000000..003e9c798 --- /dev/null +++ b/counting-bits/TonyKim9401.java @@ -0,0 +1,10 @@ +class Solution { + public int[] countBits(int n) { + // time complexity: O(n) + // space complexity: O(n) + int[] output = new int[n+1]; + int num = 0; + while (num <= n) output[num] = Integer.bitCount(num++); + return output; + } +} diff --git a/decode-ways/TonyKim9401.java b/decode-ways/TonyKim9401.java new file mode 100644 index 000000000..c6083e41c --- /dev/null +++ b/decode-ways/TonyKim9401.java @@ -0,0 +1,21 @@ +class Solution { + public int numDecodings(String s) { + // time complexity: O(n) + // space complexity: O(n) + if (s.charAt(0) == '0') return 0; + + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + dp[1] = 1; + + for (int i = 2; i <= s.length(); i++) { + int oneDigit = Integer.parseInt(s.substring(i-1, i)); + int twoDigits = Integer.parseInt(s.substring(i-2, i)); + + if (oneDigit > 0 && oneDigit < 10) dp[i] += dp[i-1]; + if (twoDigits >= 10 && twoDigits <= 26) dp[i] += dp[i-2]; + } + + return dp[s.length()]; + } +} diff --git a/encode-and-decode-strings/TonyKim9401.java b/encode-and-decode-strings/TonyKim9401.java new file mode 100644 index 000000000..93d16d4de --- /dev/null +++ b/encode-and-decode-strings/TonyKim9401.java @@ -0,0 +1,32 @@ +public class Solution { + /* + * @param strs: a list of strings + * @return: encodes a list of strings to a single string. + */ + public String encode(List strs) { + // write your code here + StringBuilder sb = new StringBuilder(); + for (String str : strs) { + sb.append(str.length()).append("#").append(str); + } + return sb.toString(); + } + + /* + * @param str: A string + * @return: decodes a single string to a list of strings + */ + public List decode(String str) { + // write your code here + List output = new ArrayList<>(); + int i = 0; + while (i < str.length()) { + int idx = str.indexOf('#', i); + int length = Integer.parseInt(str.substring(i, idx)); + String s = str.substring(idx + 1, idx + 1 + length); + output.add(s); + i = idx + 1 + length; + } + return output; + } +} diff --git a/valid-anagram/TonyKim9401.java b/valid-anagram/TonyKim9401.java new file mode 100644 index 000000000..18a05577c --- /dev/null +++ b/valid-anagram/TonyKim9401.java @@ -0,0 +1,15 @@ +class Solution { + public boolean isAnagram(String s, String t) { + // time complexity: O(n log n) + // space complexity: O(n) + if (s.length() != t.length()) return false; + + char[] sArr = s.toCharArray(); + char[] tArr = t.toCharArray(); + + Arrays.sort(sArr); + Arrays.sort(tArr); + + return Arrays.equals(sArr, tArr); + } +}