diff --git a/alien-dictionary/yhkee0404.kt b/alien-dictionary/yhkee0404.kt new file mode 100644 index 000000000..071e09c96 --- /dev/null +++ b/alien-dictionary/yhkee0404.kt @@ -0,0 +1,63 @@ +class Solution { + /** + * @param words: a list of words + * @return: a string which is correct order + */ + fun alienOrder(words: Array): String { + // Write your code here + val adj = List>('z'.code - 'a'.code + 1) {mutableListOf()} + for (i in 1 until words.size) { + var j = 0 + while (j != words[i - 1].length && j != words[i].length && words[i - 1][j] == words[i][j]) { + j++ + } + if (j == words[i - 1].length) { + continue + } + if (j == words[i].length) { + return "" + } + adj[words[i - 1][j].code - 'a'.code].add(words[i][j].code - 'a'.code) + } + val visited = MutableList(adj.size) {false} + words.forEach { + it.map { it.code - 'a'.code } + .forEach { visited[it] = true } + } + val inDegrees = MutableList(adj.size) {0} // Kahn T(V, E) = S(V, E) = O(V + E) + adj.forEach { + it.forEach { inDegrees[it]++ } + } + val ans = mutableListOf() + val stack = mutableListOf() + (0 until adj.size).filter { visited[it] && inDegrees[it] == 0 } + .forEach { + stack.add(it) + ans.add(it) + } + while (! stack.isEmpty()) { + val u = stack.removeLast() + adj[u].forEach { + if (--inDegrees[it] == 0) { + stack.add(it) + ans.add(it) + } + } + } + if ((0 until adj.size).any { visited[it] && inDegrees[it] != 0 }) { + return "" + } + return ans.map { (it + 'a'.code).toChar() } + .joinToString("") + /*val decoder = MutableList(adj.size) {0} + (0 until ans.size).forEach { decoder[ans[it]] = it } + val decoded = words.map { + it.map { (decoder[it.code - 'a'.code] + 'a'.code).toChar() } + .joinToString("") + } + val sortedWords = decoded.sorted() + return if (decoded == sortedWords) ans.map { (it + 'a'.code).toChar() } + .joinToString("") + else ""*/ + } +} diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/yhkee0404.dart b/construct-binary-tree-from-preorder-and-inorder-traversal/yhkee0404.dart new file mode 100644 index 000000000..e1e09286f --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/yhkee0404.dart @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * int val; + * TreeNode? left; + * TreeNode? right; + * TreeNode([this.val = 0, this.left, this.right]); + * } + */ +class Solution { + TreeNode? buildTree(List preorder, List inorder) { + int i = 1, j = 0; + final root = TreeNode(preorder[0]); + final stack = [root]; // T(n) = S(n) = O(n) + while (i != preorder.length) { + var u = stack.last; + if (u.val != inorder[j]) { + u.left = TreeNode(preorder[i++]); + stack.add(u.left!); + continue; + } + while (! stack.isEmpty && stack.last.val == inorder[j]) { + u = stack.removeLast(); + j++; + } + u.right = TreeNode(preorder[i++]); + stack.add(u.right!); + } + return root; + } +} diff --git a/longest-palindromic-substring/yhkee0404.scala b/longest-palindromic-substring/yhkee0404.scala new file mode 100644 index 000000000..92c977f36 --- /dev/null +++ b/longest-palindromic-substring/yhkee0404.scala @@ -0,0 +1,44 @@ +import scala.collection.mutable.ArrayBuffer + +object Solution { + def longestPalindrome(s: String): String = { + val list = ArrayBuffer[Char]() + for (c <- s) { + list += c + list += '\u0000' + } + val dp = Array.fill(list.size - 1)(0) + var lastR = -1 + var lastMid = -1 + var maxD = 0 + var ansL = 0 + var ansR = 0 + var i = 1 + for (i <- 0 until dp.size) { // Manacher T(n) = S(n) = O(n) + val diff = lastR - i + var d = if (diff <= 0) 0 else diff min dp((lastMid << 1) - i) + var l = i - d + var r = i + d + while (l != 0 && r != list.size - 1 && list(l - 1) == list(r + 1)) { + d += 1 + l -= 1 + r += 1 + } + if (maxD < d) { + maxD = d + ansL = l + ansR = r + } + dp(i) = d + if (lastR < r) { + lastR = r + lastMid = i + } + } + val sb = StringBuilder() + for (i <- ansL to ansR if list(i) != '\u0000') { + sb.append(list(i)) + } + sb.toString + } +} diff --git a/rotate-image/yhkee0404.go b/rotate-image/yhkee0404.go new file mode 100644 index 000000000..2115cabd1 --- /dev/null +++ b/rotate-image/yhkee0404.go @@ -0,0 +1,14 @@ +func rotate(matrix [][]int) { + for i := range len(matrix) { + for j := i + 1; j != len(matrix[i]); j++ { // T(n) = O(n) + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + } + } + for _, row := range matrix { + for i, j := 0, len(row) - 1; i < j; { + row[i], row[j] = row[j], row[i] + i++ + j-- + } + } +} diff --git a/subtree-of-another-tree/yhkee0404.rs b/subtree-of-another-tree/yhkee0404.rs new file mode 100644 index 000000000..24527f6ca --- /dev/null +++ b/subtree-of-another-tree/yhkee0404.rs @@ -0,0 +1,56 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + pub fn is_subtree(root: Option>>, sub_root: Option>>) -> bool { + match &root { + Some(u) => { + let u = u.borrow(); + match &sub_root { + Some(v) => { + let v = v.borrow(); + Self::solve(root.clone(), sub_root.clone()) + || Self::is_subtree(u.left.clone(), sub_root.clone()) + || Self::is_subtree(u.right.clone(), sub_root.clone()) + }, + None => false, + } + }, + None => sub_root.is_none(), + } + } + fn solve(root: Option>>, sub_root: Option>>) -> bool { + match root { + Some(u) => { + let u = u.borrow(); + match &sub_root { + Some(v) => { + let v = v.borrow(); + u.val == v.val + && Self::solve(u.left.clone(), v.left.clone()) + && Self::solve(u.right.clone(), v.right.clone()) + }, + None => false, + } + }, + None => sub_root.is_none(), + } + } +}